Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions stack/DailyTemperatures.js
Original file line number Diff line number Diff line change
@@ -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/
21 changes: 12 additions & 9 deletions stack/MinStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

var MinStack = function () {
this.array = [];
this.stack = [];
this.minStack = [];
};

Expand All @@ -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);
}
};
Expand All @@ -41,15 +44,15 @@ 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);
};

/**
* @return {number}
*/
MinStack.prototype.top = function () {
return this.array[this.array.length - 1];
return this.stack[this.stack.length - 1];
};

/**
Expand Down
27 changes: 27 additions & 0 deletions stack/ValidParentheses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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 v = stack[stack.length - 1];
const word = x + v;

if (word == a || word == b || word == c) {
stack.pop();
} else {
stack.push(x);
}
}

return stack.length ? true : false;
};

const s = "()";
const n = "()";
const x = "(]";

console.log(isValid(s));