Skip to content

Instantly share code, notes, and snippets.

@joannaylin
joannaylin / validateSubsequence.js
Last active July 18, 2022 03:04
Easy: Given two non-empty arrays of integers, write a function that determines whether the second array is a subsequence of the first one.
/*
Notes
- subsequence doesn't necessarily mean that they are adjacent in the array
- a single number in the array and the array itself count as a subsequence
- run through main array
- set counter to 0
- check if number equals number in second array
- if yes:
@joannaylin
joannaylin / asteroidCollision.js
Last active February 14, 2023 07:06
LeetCode 735: Asteroid Collision (JavaScript)
var asteroidCollision = function(asteroids) {
let stack = [];
for (let i = 0; i < asteroids.length; i++) {
let add = true;
if (asteroids[i] < 0 && stack[stack.length-1] > 0) {
while (stack.length !== 0 && stack[stack.length-1] > 0) {
if (Math.abs(asteroids[i]) > Math.abs(stack[stack.length-1])) {
stack.pop();
@joannaylin
joannaylin / longestPalindrome.js
Last active September 21, 2020 04:25
LeetCode 409: Longest Palindrome (JavaScript)
/**
* @param {string} s
* @return {number}
*/
var longestPalindrome = function (s) {
const counts = {};
let sum = 0;
let oddPresent = false;
for (let i = 0; i < s.length; i++) {
@joannaylin
joannaylin / reverseSinglyLinkedList.js
Created September 14, 2020 16:50
Reversing a singly linked list iteratively.
class SinglyLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
// check out this gist: https://gist.github.com/joannaylin/d3dfc926ba73833138e4571c8a0f9a15
// for the full implementation of the singly linked list.
@joannaylin
joannaylin / singlyLinkedList.js
Last active September 7, 2020 20:39
singly linked list implementation in js
class Node {
constructor(val) {
this.val = val;
this.next = null;
}
}
class SinglyLinkedList {
constructor() {
this.head = null;