Skip to content

Commit be6c125

Browse files
committed
push hard
1 parent 09142a9 commit be6c125

11 files changed

+163
-26
lines changed

Easy/121. Best Time to Buy and Sell Stock.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ var maxProfit2 = function (prices) {
3131
return maxProfit;
3232
}
3333

34+
// [3,3,5,0,0,3,1,4]
35+
3436
/*Method 2 */
3537
/* TIME COMPLEXITY O(N2) */
3638

@@ -48,4 +50,4 @@ var maxProfit1 = function (prices) {
4850
return max;
4951

5052
}
51-
console.log(maxProfit2([7, 1, 5, 3, 6, 4]));
53+
console.log(maxProfit2([3, 3, 5, 0, 0, 3, 1, 4]));

Easy/496. Next Greater Element I.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
496. Next Greater Element I
3+
https://leetcode.com/problems/next-greater-element-i/
4+
*/
5+
6+
/**
7+
* @param {number[]} nums1
8+
* @param {number[]} nums2
9+
* @return {number[]}
10+
*/
11+
var nextGreaterElement = function (nums1, nums2) {
12+
let i = 0, j = 0, numToPush;
13+
for (i = 0; i < nums1.length; i++) {
14+
j = nums2.indexOf(nums1[i]); // Find the nums1 current value index in nums2 array
15+
numToPush = -1;
16+
for (j = j + 1; j < nums2.length; j++) {
17+
if (nums2[j] > nums1[i]) {
18+
numToPush = nums2[j];
19+
break;
20+
}
21+
}
22+
nums1[i] = numToPush;
23+
}
24+
return nums1;
25+
};
26+
console.log(nextGreaterElement([4, 1, 2], [1, 3, 4, 2]));
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
3+
public int maxProfit(int[] prices) {
4+
int fb = Integer.MIN_VALUE, sb = Integer.MIN_VALUE;
5+
int fs = 0, ss = 0;
6+
7+
for (int i = 0; i < prices.length; i++) {
8+
fb = Math.max(fb, -prices[i]);
9+
fs = Math.max(fs, fb + prices[i]);
10+
11+
sb = Math.max(sb, fs - prices[i]);
12+
ss = Math.max(ss, sb + prices[i]);
13+
}
14+
return ss;
15+
}
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* This question is solved but by mathmatically */
2+
3+
/**
4+
* @param {number[]} prices
5+
* @return {number}
6+
*/
7+
var maxProfit = function (prices) {
8+
var fb = Number.MIN_VALUE, sb = Number.MIN_VALUE;
9+
var fs = 0, ss = 0;
10+
for (let i = 0; i < prices.length; i++) {
11+
fb = Math.max(fb, -prices[i]);
12+
fs = Math.max(fs, parseInt(fb) + parseInt(prices[i]));
13+
14+
sb = Math.max(sb, parseInt(fs) - parseInt(prices[i]));
15+
ss = Math.max(ss, parseInt(sb) + parseInt(prices[i]));
16+
}
17+
return ss;
18+
};
19+
20+
console.log(maxProfit([3, 3, 5, 0, 0, 3, 1, 4]));
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* This question is solved but TLE */
2+
3+
/**
4+
* @param {number[]} heights
5+
* @return {number}
6+
*/
7+
function reverse(arr, target) {
8+
var count = 0;
9+
for (let i = 0; i < arr.length; i++) {
10+
if (arr[i] >= target) {
11+
count++;
12+
} else {
13+
return count;
14+
}
15+
}
16+
return count;
17+
}
18+
function forward(arr, target) {
19+
var count = 1;
20+
for (let i = 0; i < arr.length; i++) {
21+
if (arr[i] >= target) {
22+
count++;
23+
} else {
24+
return count;
25+
}
26+
}
27+
return count;
28+
}
29+
30+
var largestRectangleArea = function (heights) {
31+
var map = new Map();
32+
var max = 0;
33+
for (let i = 0; i < heights.length; i++) {
34+
var forwardCount = forward(heights.slice(i + 1), heights[i]);
35+
var reverseCount = reverse(heights.slice(0, i).reverse(), heights[i]);
36+
var totalWidth = (parseInt(forwardCount) + parseInt(reverseCount)) * heights[i];
37+
// console.log(`${heights[i]} ${totalWidth}`);
38+
if (max < totalWidth) {
39+
max = totalWidth;
40+
}
41+
// console.log(forward(heights.slice(i + 1), heights[i]));
42+
}
43+
return max;
44+
45+
};
46+
console.log(largestRectangleArea([2, 1, 5, 6, 2, 3]));
47+
48+
// [2,1,5,6,9,2,3]
49+
// [2,4]
50+
// [1]
51+
// [4,2]

Medium/189. Rotate Array.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
/*
2+
189. Rotate Array
3+
https://leetcode.com/problems/rotate-array/
4+
*/
5+
16
/**
27
* @param {number[]} nums
38
* @param {number} k
49
* @return {void} Do not return anything, modify nums in-place instead.
510
*/
611
var rotate = function (nums, k) {
7-
var ansArray = [];
12+
var ansArray = []; // Create an array
813
for (let i = 0; i < nums.length; i++) {
9-
ansArray[((i + k) % nums.length)] = nums[i];
14+
ansArray[((i + k) % nums.length)] = nums[i]; // Math Login
1015
}
11-
return nums = ansArray
16+
return nums = ansArray;
1217
};
1318
console.log(rotate([1, 2, 3, 4, 5, 6, 7], 3));

Medium/503. Next Greater Element II.js

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,21 @@
44
*/
55

66

7-
8-
function findMax(currentElement, arr) {
9-
for (let i = 0; i < arr.length; i++) {
10-
if (currentElement < arr[i]) {
11-
return arr[i];
12-
}
13-
}
14-
return -1;
15-
}
167
var nextGreaterElements = function (nums) {
17-
let ansArr = [];
8+
var stack = [];
9+
for (let i = nums.length - 1; i > 0; i--) {
10+
stack.push(nums[i]);
11+
}
1812
for (let i = 0; i < nums.length; i++) {
19-
ansArr.push(findMax(nums[i], nums));
13+
while (stack[stack.length - 1] <= nums[i]) {
14+
stack.pop();
15+
}
16+
if (stack.length <= 0) {
17+
nums[i] = -1;
18+
} else {
19+
nums[i] = stack[stack.length - 1];
20+
}
2021
}
21-
return ansArr;
22+
console.log(nums);
2223
};
23-
console.log(nextGreaterElements([1, 5, 3, 6, 8]));
24-
25-
// [1, 2, 3, 5,4, 3]=[1, 2, 3, 4, 3 1, 2, 3, 4, 3]
26-
// [1, 2, 1] = [1, 2, 1 1, 2, 1]
27-
// [1, 2, 3, 2, 1] = [1, 2, 3, 2, 1 1, 2, 3, 2, 1]
28-
// [5,4,3,2,1] = [5,4,3,2,1 5,4,3,2,1]
24+
nextGreaterElements([1, 2, 1]);

Medium/61. Rotate List.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* This question is not solved */
2+
3+
function ListNode(val, next) {
4+
this.val = (val === undefined ? 0 : val)
5+
this.next = (next === undefined ? null : next)
6+
}
7+
ListNode(20);

Practice Problems/Next Greater Element (NGE).js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ function NextGreaterElement(arr) {
5252
}
5353
console.log(map);
5454
}
55-
NextGreaterElement([1, 2, 1]);
55+
NextGreaterElement([1, 3, 4, 2]);

Practice Problems/stack/insertElementAtBottom.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
/* Insert Element At Bottom Of the Stack */
2+
13
function insertElementAtBotton(stack, count, size, desiredElement) {
24

35
if (count === size) {
4-
stack.push(desiredElement);
56
return;
67
}
78
var num = stack[stack.length - 1];
9+
console.log(stack.join(" "));
810
stack.pop();
9-
console.log(stack);
1011
insertElementAtBotton(stack, count + 1, size, desiredElement);
1112
stack.push(num);
12-
console.log(stack);
13+
console.log(stack.join(" "));
1314
}
1415

1516
var stack = [1, 2, 3, 4];

0 commit comments

Comments
 (0)