From 37aa16314536efec6dd07e6d4d1dd1e8317b37d0 Mon Sep 17 00:00:00 2001 From: tree Date: Sat, 2 Mar 2024 00:26:08 +0900 Subject: [PATCH 1/5] dailyTemperatures v1 --- stack/DailyTemperatures.js | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 stack/DailyTemperatures.js diff --git a/stack/DailyTemperatures.js b/stack/DailyTemperatures.js new file mode 100644 index 0000000..71a6146 --- /dev/null +++ b/stack/DailyTemperatures.js @@ -0,0 +1,42 @@ +// Given an array of integers temperatures represents the daily temperatures, +// return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. +// If there is no future day for which this is possible, keep answer[i] == 0 instead. + +const temperatures = [73, 74, 75, 71, 69, 72, 76, 73]; + +const dailyTemperatures = function (temperatures) { + const minStack = []; + + temperatures.forEach((tem, idx) => { + let count = 0; + console.log("tem" + tem); + for (let i = idx + 1; i < temperatures.length; i++) { + const afeterNum = temperatures[i]; + + if (idx === temperatures.length - 1) { + count = 0; + break; + } + + if (tem < afeterNum) { + count += 1; + break; + } + + if (tem > afeterNum && idx + 1 === temperatures.length - 1) { + count = 0; + break; + } + count += 1; + } + + minStack.push(count); + }); + + return minStack; +}; + +const x = dailyTemperatures(temperatures); +console.log(x); + +// https://leetcode.com/problems/daily-temperatures/description/ From ea64505b3621723e3e068fb9634c5a9d10e6306a Mon Sep 17 00:00:00 2001 From: tree Date: Tue, 5 Mar 2024 19:13:11 +0900 Subject: [PATCH 2/5] add annotation --- stack/MinStack.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/stack/MinStack.js b/stack/MinStack.js index ce97345..25549fd 100644 --- a/stack/MinStack.js +++ b/stack/MinStack.js @@ -15,7 +15,7 @@ */ var MinStack = function () { - this.array = []; + this.stack = []; this.minStack = []; }; @@ -25,14 +25,17 @@ var MinStack = function () { */ MinStack.prototype.push = function (val) { - this.array.push(val); + const minStackTop = this.minStack.length - 1; + const stackTop = this.stack.length - 1; - if ( - this.minStack[this.minStack.length - 1] > - this.array[this.minStack.length - 1] - ) { - this.minStack.push(this.minStack[this.minStack.length - 1]); + this.stack.push(val); // By default, the stack has a push + + // Compare the numbers in the Top of stack and minStack. + if (this.minStack[minStackTop] > this.stack[stackTop]) { + // If the number in the minStack is smaller, then push + this.minStack.push(this.minStack[minStackTop]); } else { + // If the number in the minStack is larger, push the value currently coming into the minStak. this.minStack.push(val); } }; @@ -41,7 +44,7 @@ MinStack.prototype.push = function (val) { * @return {void} */ MinStack.prototype.pop = function () { - this.array = this.array.slice(0, this.array.length - 1); + this.stack = this.stack.slice(0, this.stack.length - 1); this.minStack = this.minStack.slice(0, this.minStack.length - 1); }; @@ -49,7 +52,7 @@ MinStack.prototype.pop = function () { * @return {number} */ MinStack.prototype.top = function () { - return this.array[this.array.length - 1]; + return this.stack[this.stack.length - 1]; }; /** From 3737c6e292b5c00107fd40344b148de5586651a0 Mon Sep 17 00:00:00 2001 From: tree Date: Tue, 5 Mar 2024 20:16:11 +0900 Subject: [PATCH 3/5] solve ValidParentheses --- stack/ValidParentheses.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 stack/ValidParentheses.js diff --git a/stack/ValidParentheses.js b/stack/ValidParentheses.js new file mode 100644 index 0000000..ce160d3 --- /dev/null +++ b/stack/ValidParentheses.js @@ -0,0 +1,27 @@ +const isValid = function (s) { + const stack = []; + + for (let i = 0; i < s.length; i++) { + const x = s[i]; + const a = stack[stack.length - 1]; + const word = a + x; + + if (word == "()" || word == "[]" || word == "{}") { + stack.pop(); + } else { + stack.push(x); + } + } + + if (stack.length) { + return false; + } else { + return true; + } +}; + +const s = "()"; +const n = "()"; +const x = "(]"; + +console.log(isValid(s)); From d15b45c08feea8370d6e4610fad65a16db6d1435 Mon Sep 17 00:00:00 2001 From: tree Date: Tue, 5 Mar 2024 20:21:45 +0900 Subject: [PATCH 4/5] refactor validate Parentheses --- stack/ValidParentheses.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/stack/ValidParentheses.js b/stack/ValidParentheses.js index ce160d3..d0f0d6a 100644 --- a/stack/ValidParentheses.js +++ b/stack/ValidParentheses.js @@ -1,23 +1,23 @@ const isValid = function (s) { const stack = []; + const a = "()"; + const b = "[]"; + const c = "{}"; + for (let i = 0; i < s.length; i++) { const x = s[i]; - const a = stack[stack.length - 1]; - const word = a + x; + const v = stack[stack.length - 1]; + const word = a + v; - if (word == "()" || word == "[]" || word == "{}") { + if (word == a || word == b || word == c) { stack.pop(); } else { stack.push(x); } } - if (stack.length) { - return false; - } else { - return true; - } + return stack.length ? true : false; }; const s = "()"; From 217279b0698a1ac94ae102fa1352011ed427c6f8 Mon Sep 17 00:00:00 2001 From: tree Date: Tue, 5 Mar 2024 20:27:43 +0900 Subject: [PATCH 5/5] refactor validate Parentheses v2 --- stack/ValidParentheses.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stack/ValidParentheses.js b/stack/ValidParentheses.js index d0f0d6a..661c937 100644 --- a/stack/ValidParentheses.js +++ b/stack/ValidParentheses.js @@ -8,7 +8,7 @@ const isValid = function (s) { for (let i = 0; i < s.length; i++) { const x = s[i]; const v = stack[stack.length - 1]; - const word = a + v; + const word = x + v; if (word == a || word == b || word == c) { stack.pop();