Skip to content

Commit 234952a

Browse files
authored
Merge pull request neetcode-gh#3 from veer-p/main
JavaScript Solutions for 1, 11, 15, 19, 20, 21, 33, 48, 49, 53, 54, 70, 73, 98, 100, 104, 121, 125, 141, 143, 152, 153, 206, 217, 226, 238, 242, 268, 300, 572
2 parents f0b7e5e + 3a6c767 commit 234952a

29 files changed

+734
-0
lines changed

javascript/1-Two-Sum.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number[]}
5+
*/
6+
var twoSum = function(nums, target) {
7+
let map = {};
8+
for (let i = 0; i < nums.length; i++) {
9+
if (target - nums[i] in map) {
10+
return [map[target-nums[i]], i];
11+
} else {
12+
map[nums[i]] = i;
13+
}
14+
}
15+
};

javascript/100-Same-Tree.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} p
11+
* @param {TreeNode} q
12+
* @return {boolean}
13+
*/
14+
var isSameTree = function(p, q) {
15+
if (!p && !q) return true;
16+
if (!p || !q || p.val !== q.val) return false;
17+
return isSameTree(p.right,q.right) && isSameTree(p.left, q.left);
18+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number}
12+
*/
13+
var maxDepth = (root) => {
14+
let maxDepth = 0;
15+
let DFS = (node, depth) => {
16+
if (!node) return maxDepth;
17+
if (depth > maxDepth) maxDepth = depth;
18+
DFS(node.right, depth + 1);
19+
DFS(node.left, depth + 1);
20+
}
21+
DFS(root, 1);
22+
return maxDepth;
23+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*/
5+
var maxArea = function(height) {
6+
let max = 0;
7+
let i = 0;
8+
let j = height.length - 1;
9+
10+
while (i < j) {
11+
curr = (j - i) * Math.min(height[i], height[j]);
12+
max = Math.max(curr, max);
13+
if (height[i] > height[j]) {
14+
j--;
15+
} else {
16+
i++;
17+
}
18+
}
19+
return max;
20+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} prices
3+
* @return {number}
4+
*/
5+
var maxProfit = function(prices) {
6+
let buy = prices[0];
7+
let profit = 0;
8+
for (let i = 0; i < prices.length; i++) {
9+
if (prices[i] < buy) {
10+
buy = prices[i];
11+
} else {
12+
profit = Math.max(prices[i] - buy, profit);
13+
}
14+
}
15+
return profit;
16+
};

javascript/125-Valid-Palindrome.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isPalindrome = function(s) {
6+
s = s.toLowerCase();
7+
s = s.replace(/[^A-Z0-9]/gi, '');
8+
9+
let i = 0
10+
let j = s.length - 1;
11+
12+
while (i < j) {
13+
if (s[i] !== s[j]) return false;
14+
15+
i++;
16+
j--
17+
}
18+
return true;
19+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
9+
/**
10+
* @param {ListNode} head
11+
* @return {boolean}
12+
*/
13+
var hasCycle = function(head) {
14+
let set = new Set();
15+
while (head) {
16+
if (set.has(head)) {
17+
return true;
18+
} else {
19+
set.add(head);
20+
head = head.next;
21+
}
22+
}
23+
24+
return false;
25+
};

javascript/143-Reorder-List.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {void} Do not return anything, modify head in-place instead.
11+
*/
12+
var reorderList = function(head) {
13+
if (!head) { return };
14+
15+
let slow = head;
16+
let fast = head;
17+
18+
19+
// finding the middle of the linked list using 2 pters
20+
while (fast && fast.next) {
21+
slow = slow.next;
22+
fast = fast.next.next;
23+
}
24+
25+
// reverse the second part of the list starting at slow
26+
let prev = null
27+
let curr = slow;
28+
while (curr) {
29+
let next = curr.next;
30+
curr.next = prev;
31+
prev = curr;
32+
curr = next;
33+
} // here prev is the head
34+
35+
// merge two sorted lists (first one starts at head, second at prev)
36+
let first = head;
37+
let second = prev;
38+
39+
while(second.next) {
40+
temp = first.next;
41+
first.next = second;
42+
first = temp;
43+
44+
temp = second.next;
45+
second.next = first;
46+
second = temp;
47+
}
48+
49+
50+
};

javascript/15-3Sum.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
*/
5+
var threeSum = function(nums) {
6+
let res = [];
7+
let left = 0;
8+
let right = nums.length - 1;
9+
nums.sort((a,b) => { return a - b })
10+
11+
for (let i = 0; i < nums.length - 1; i++) {
12+
if (nums[i] > 0) return res;
13+
if (nums[i] === nums[i - 1]) continue;
14+
15+
left = i + 1;
16+
right = nums.length - 1;
17+
let temp = 0;
18+
19+
while (left < right) {
20+
temp = nums[left] + nums[right] + nums[i];
21+
if (temp === 0) {
22+
res.push([nums[i], nums[left], nums[right]]);
23+
left++;
24+
right--;
25+
26+
while(nums[left] == nums[left - 1]) { left++ };
27+
28+
while (nums[right] == nums[right - 1]) { right-- };
29+
30+
} else if (temp > 0) { right-- }
31+
else if (temp < 0) { left++ }
32+
}
33+
}
34+
return res;
35+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var maxProduct = function(nums) {
6+
let result = nums[0];
7+
let prevMax = nums[0];
8+
let prevMin = nums[0];
9+
for(let i = 1; i < nums.length; i++) {
10+
currMax = Math.max(nums[i], prevMax * nums[i], prevMin * nums[i]);
11+
currMin = Math.min(nums[i], prevMax * nums[i], prevMin * nums[i]);
12+
13+
prevMax = currMax;
14+
prevMin = currMin;
15+
16+
result = Math.max(currMax, result);
17+
}
18+
return result;
19+
20+
};

0 commit comments

Comments
 (0)