Skip to content

Commit fa129f3

Browse files
committed
31 oct
1 parent 30882b1 commit fa129f3

File tree

10 files changed

+201
-204
lines changed

10 files changed

+201
-204
lines changed

JavaScript/DynamicProg.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
var maxProfit = function (prices) { // Best time to buy and sell stocks
3+
let maxProfit = 0; // no profit
4+
let minPrice = Infinity; // highest price
5+
for (let price=0; price < prices.length; price++) { // look for every price
6+
minPrice = Math.min(minPrice, price); // compare cuurent price with minimum price
7+
const profit = price - minPrice; // find current profit
8+
maxProfit = Math.max(maxProfit, profit); // compare current profit with maximum profit
9+
}
10+
return maxProfit;
11+
};

JavaScript/FlipEquivBinaryT.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

JavaScript/HashMap.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
var twoSum = function (nums, target) { // Find two numbers which are two-sum of target in nums
3+
const map = new Map(); // create a new map
4+
for (let i = 0; i < nums.length; i++) { // now iterate over nums
5+
const complement = target - nums[i] // find complement of target to current
6+
if (map.has(complement)) { // if there's complement in map
7+
return [map.get(complement), i] // return complement and current index
8+
}
9+
map.set(nums[i], i) // map current number to current index otherwise
10+
}
11+
};

JavaScript/LinkedList.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
var reverseList = function(head) {
3+
let prev = null
4+
let curr = head
5+
while (curr !== null){
6+
let next = curr.next;
7+
curr.next = prev;
8+
prev = curr;
9+
curr = next;
10+
}
11+
return prev;
12+
};

JavaScript/String.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var isPalindrome = function(s) {
2+
const cleaned = s.toLowerCase().replace(/[^a-z0-9]/g,'');
3+
return cleaned === cleaned.split('').reverse().join('');
4+
};

JavaScript/Tree.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
var flipEquiv = function(root1, root2) { // determine if a tree is flip equivalent of another tree.
3+
// base case
4+
if (root1 === null && root2 === null ) return true
5+
if (root1 === null || root2 === null) return false
6+
if (root1.val !== root2.val) return false;
7+
// recursively check no-flip/ flip
8+
return ( ( flipEquiv(root1.left, root2.left) && flipEquiv(root1.right, root2.right) ) || ( flipEquiv(root1.left, root2.right) && flipEquiv(root1.right, root2.left) ) )
9+
};

JavaScript/TwoPointer.js

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// subarrays
2+
var searchMatrix = function (matrix, target) {
3+
if (!matrix.length || !matrix[0].length) return false;
4+
let row = 0;
5+
let col = matrix[0].length - 1;
6+
while (row < matrix.length && col >= 0) {
7+
if (matrix[row][col] === target) {
8+
return true;
9+
} else if (matrix[row][col] > target) {
10+
col--
11+
} else row++
12+
}
13+
return false
14+
};
15+
16+
var maxArea = function (height) { // container with most water
17+
let left = 0; // two pointers
18+
let right = height.length - 1;
19+
let maxWater = 0;
20+
while (left < right) {
21+
const area = Math.min(height[left], height[right]) * (right - left) // calculate area enclosed by left and right pillar
22+
maxWater = Math.max(maxWater, area) // if current area is maximum, update it
23+
if (height[left] < height[right]) { // continue with next larger pillar whether it is left or right
24+
left++;
25+
} else {
26+
right--;
27+
}
28+
}
29+
return maxWater;
30+
};
31+
32+
var searchInsert = function (nums, target) { // search insert position
33+
let left = 0,
34+
right = nums.length - 1;
35+
while (left <= right) {
36+
let mid = Math.floor((left + right) / 2);
37+
if (nums[mid] === target) {
38+
return mid;
39+
} else if (nums[mid] < target) {
40+
left = mid + 1;
41+
} else {
42+
right = mid - 1;
43+
}
44+
}
45+
return left;
46+
};
47+
48+
var spiralOrder = function (matrix) {
49+
const result = [];
50+
if (matrix.length == 0) return result;
51+
let top = 0,
52+
bottom = matrix.length - 1;
53+
let left = 0,
54+
right = matrix[0].length - 1;
55+
while (top <= bottom && left <= right) {
56+
for (let i = left; i <= right; i++) {
57+
result.push(matrix[top][i]);
58+
}
59+
top++;
60+
for (let i = top; i <= bottom; i++) {
61+
result.push(matrix[i][right]);
62+
}
63+
right--;
64+
if (top <= bottom) {
65+
for (let i = right; i >= left; i--) {
66+
result.push(matrix[bottom][i]);
67+
}
68+
}
69+
bottom--;
70+
if (left <= right) {
71+
for (let i = bottom; i >= top; i--) {
72+
result.push(matrix[i][left]);
73+
}
74+
}
75+
left++;
76+
}
77+
return result;
78+
};
79+
80+
var trap = function (height) { // trapping rain water
81+
if (height.length === 0) return 0;
82+
let left = 0, // two pointer and correspoding variables
83+
leftMax = 0;
84+
let right = height.length - 1,
85+
rightMax = 0;
86+
let waterTrapped = 0;
87+
while (left < right) {
88+
if (height[left] < height[right]) {
89+
if (height[left] >= leftMax) {
90+
leftMax = height[left];
91+
} else {
92+
waterTrapped += leftMax - height[left]
93+
}
94+
left++;
95+
} else {
96+
if (height[right] >= rightMax) {
97+
rightMax = height[right];
98+
} else {
99+
waterTrapped += rightMax - height[right]
100+
}
101+
right--;
102+
}
103+
}
104+
return waterTrapped;
105+
};
106+
107+
108+
var searchRange = function (nums, target) { // Find first and last position of an elemnet in a sorted array
109+
const binarySearch = (isleft) => {
110+
let left = 0,
111+
right = nums.length - 1;
112+
let index = -1;
113+
while (left <= right) {
114+
let mid = Math.floor((left + right) / 2);
115+
if (nums[mid] === target) {
116+
index = mid;
117+
if (isleft) {
118+
right = mid - 1;
119+
} else {
120+
left = mid + 1;
121+
}
122+
} else if (nums[mid] < target) {
123+
left = mid + 1;
124+
} else {
125+
right = mid - 1;
126+
}
127+
}
128+
return index;
129+
}
130+
const start = binarySearch(true);
131+
if (start === -1) {
132+
return [-1, -1];
133+
}
134+
const end = binarySearch(false);
135+
return [start, end];
136+
};

0 commit comments

Comments
 (0)