We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 09142a9 commit be6c125Copy full SHA for be6c125
Easy/121. Best Time to Buy and Sell Stock.js
@@ -31,6 +31,8 @@ var maxProfit2 = function (prices) {
31
return maxProfit;
32
}
33
34
+// [3,3,5,0,0,3,1,4]
35
+
36
/*Method 2 */
37
/* TIME COMPLEXITY O(N2) */
38
@@ -48,4 +50,4 @@ var maxProfit1 = function (prices) {
48
50
return max;
49
51
52
-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
@@ -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]));
Hard/123. Best Time to Buy and Sell Stock III.java
@@ -0,0 +1,16 @@
+class Solution {
+ public int maxProfit(int[] prices) {
+ int fb = Integer.MIN_VALUE, sb = Integer.MIN_VALUE;
+ int fs = 0, ss = 0;
+ for (int i = 0; i < prices.length; i++) {
+ fb = Math.max(fb, -prices[i]);
+ fs = Math.max(fs, fb + prices[i]);
+ sb = Math.max(sb, fs - prices[i]);
+ ss = Math.max(ss, sb + prices[i]);
+ return ss;
+}
Hard/123. Best Time to Buy and Sell Stock III.js
@@ -0,0 +1,20 @@
+/* This question is solved but by mathmatically */
+ * @param {number[]} prices
+ * @return {number}
+var maxProfit = function (prices) {
+ var fb = Number.MIN_VALUE, sb = Number.MIN_VALUE;
+ var fs = 0, ss = 0;
+ for (let i = 0; i < prices.length; i++) {
+ fs = Math.max(fs, parseInt(fb) + parseInt(prices[i]));
+ sb = Math.max(sb, parseInt(fs) - parseInt(prices[i]));
+ ss = Math.max(ss, parseInt(sb) + parseInt(prices[i]));
+console.log(maxProfit([3, 3, 5, 0, 0, 3, 1, 4]));
Hard/84. Largest Rectangle in Histogram.js
@@ -0,0 +1,51 @@
+/* This question is solved but TLE */
+ * @param {number[]} heights
+function reverse(arr, target) {
+ var count = 0;
+ for (let i = 0; i < arr.length; i++) {
+ if (arr[i] >= target) {
+ count++;
+ } else {
+ return count;
+function forward(arr, target) {
+ var count = 1;
27
28
29
30
+var largestRectangleArea = function (heights) {
+ var map = new Map();
+ var max = 0;
+ for (let i = 0; i < heights.length; i++) {
+ var forwardCount = forward(heights.slice(i + 1), heights[i]);
+ var reverseCount = reverse(heights.slice(0, i).reverse(), heights[i]);
+ var totalWidth = (parseInt(forwardCount) + parseInt(reverseCount)) * heights[i];
+ // console.log(`${heights[i]} ${totalWidth}`);
+ 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
+// [2,1,5,6,9,2,3]
+// [2,4]
+// [1]
+// [4,2]
Medium/189. Rotate Array.js
@@ -1,13 +1,18 @@
+ 189. Rotate Array
+ https://leetcode.com/problems/rotate-array/
/**
* @param {number[]} nums
* @param {number} k
* @return {void} Do not return anything, modify nums in-place instead.
*/
var rotate = function (nums, k) {
- var ansArray = [];
+ var ansArray = []; // Create an array
for (let i = 0; i < nums.length; i++) {
- ansArray[((i + k) % nums.length)] = nums[i];
+ ansArray[((i + k) % nums.length)] = nums[i]; // Math Login
- return nums = ansArray
+ return nums = ansArray;
};
console.log(rotate([1, 2, 3, 4, 5, 6, 7], 3));
Medium/503. Next Greater Element II.js
@@ -4,25 +4,21 @@
-
-function findMax(currentElement, arr) {
- for (let i = 0; i < arr.length; i++) {
- if (currentElement < arr[i]) {
- return arr[i];
- }
- return -1;
-}
var nextGreaterElements = function (nums) {
- let ansArr = [];
+ var stack = [];
+ for (let i = nums.length - 1; i > 0; i--) {
+ stack.push(nums[i]);
- ansArr.push(findMax(nums[i], nums));
+ while (stack[stack.length - 1] <= nums[i]) {
+ stack.pop();
+ if (stack.length <= 0) {
+ nums[i] = -1;
+ nums[i] = stack[stack.length - 1];
- return ansArr;
+ console.log(nums);
-console.log(nextGreaterElements([1, 5, 3, 6, 8]));
-// [1, 2, 3, 5,4, 3]=[1, 2, 3, 4, 3 1, 2, 3, 4, 3]
-// [1, 2, 1] = [1, 2, 1 1, 2, 1]
-// [1, 2, 3, 2, 1] = [1, 2, 3, 2, 1 1, 2, 3, 2, 1]
-// [5,4,3,2,1] = [5,4,3,2,1 5,4,3,2,1]
+nextGreaterElements([1, 2, 1]);
Medium/61. Rotate List.js
@@ -0,0 +1,7 @@
+/* This question is not solved */
+function ListNode(val, next) {
+ this.val = (val === undefined ? 0 : val)
+ this.next = (next === undefined ? null : next)
+ListNode(20);
Practice Problems/Next Greater Element (NGE).js
@@ -52,4 +52,4 @@ function NextGreaterElement(arr) {
console.log(map);
54
55
-NextGreaterElement([1, 2, 1]);
+NextGreaterElement([1, 3, 4, 2]);
Practice Problems/stack/insertElementAtBottom.js
@@ -1,15 +1,16 @@
+/* Insert Element At Bottom Of the Stack */
function insertElementAtBotton(stack, count, size, desiredElement) {
if (count === size) {
- stack.push(desiredElement);
return;
var num = stack[stack.length - 1];
+ console.log(stack.join(" "));
stack.pop();
- console.log(stack);
insertElementAtBotton(stack, count + 1, size, desiredElement);
stack.push(num);
var stack = [1, 2, 3, 4];
0 commit comments