Skip to content

Commit 54a9987

Browse files
committed
Update notes of stack/queue problems
1 parent ecaade9 commit 54a9987

25 files changed

+485
-26
lines changed

leetcode/1047.remove-all-adjacent-duplicates-in-string.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
## [1047. Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/)
22

3+
## Clarification Questions
4+
* No, it's clear from problem description.
5+
6+
## Test Cases
7+
### Normal Cases
8+
```
9+
Input:
10+
Output:
11+
```
12+
### Edge / Corner Cases
13+
*
14+
```
15+
Input:
16+
Output:
17+
```
18+
19+
## Stack
320
```kotlin
421
fun removeDuplicates(s: String): String {
522
val stack = Stack<Char>()

leetcode/1249.minimum-remove-to-make-valid-parentheses.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
## [1249. Minimum Remove to Make Valid Parentheses](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/)
22

3+
## Clarification Questions
4+
* No, it's clear from problem description.
5+
6+
## Test Cases
7+
### Normal Cases
8+
```
9+
Input:
10+
Output:
11+
```
12+
### Edge / Corner Cases
13+
*
14+
```
15+
Input:
16+
Output:
17+
```
18+
319
```kotlin
420
fun minRemoveToMakeValid(s: String): String {
521
if (s.isEmpty()) return ""

leetcode/155.min-stack.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
## [155. Min Stack](https://leetcode.com/problems/min-stack/)
2+
3+
## Clarification Questions
4+
* No, it's clear from problem description.
5+
6+
## Test Cases
7+
### Normal Cases
8+
```
9+
Input:
10+
Output:
11+
```
12+
### Edge / Corner Cases
13+
*
14+
```
15+
Input:
16+
Output:
17+
```
18+
219
We keep the peek value and current min via using two stack, and manipulate at the same time.
320

421
```js

leetcode/1823.find-the-winner-of-the-circular-game.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
## [1823. Find the Winner of the Circular Game](https://leetcode.com/problems/find-the-winner-of-the-circular-game)
22

3+
## Clarification Questions
4+
* No, it's clear from problem description.
5+
6+
## Test Cases
7+
### Normal Cases
8+
```
9+
Input:
10+
Output:
11+
```
12+
### Edge / Corner Cases
13+
*
14+
```
15+
Input:
16+
Output:
17+
```
18+
319
### Simulation
420
```kotlin
521
fun findTheWinner(n: Int, k: Int): Int {

leetcode/20.valid-parentheses.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
## [20. Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)
22

3+
## Clarification Questions
4+
* No, it's clear from problem description.
5+
6+
## Test Cases
7+
### Normal Cases
8+
```
9+
Input:
10+
Output:
11+
```
12+
### Edge / Corner Cases
13+
*
14+
```
15+
Input:
16+
Output:
17+
```
18+
319
```kotlin
420
fun isValid(s: String): Boolean {
521
val mapping = mapOf(')' to '(', ']' to '[', '}' to '{')

leetcode/224.basic-calculator.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
1-
## [224. Basic Calculator](https://leetcode.com/problems/basic-calculator/)
1+
# [224. Basic Calculator](https://leetcode.com/problems/basic-calculator/)
2+
3+
## Clarification Questions
4+
* Is the input string always valid? (i.e. no invalid characters, the parentheses are always balanced, etc.)
5+
6+
## Test Cases
7+
### Normal Cases
8+
```
9+
Input:
10+
Output:
11+
```
12+
### Edge / Corner Cases
13+
* There are multiple nested parentheses.
14+
```
15+
Input:
16+
Output:
17+
```
18+
19+
## Stack
20+
For this basic calculator, there are several cases we need to consider:
21+
1. The number: We need to build the number from digits. Mind the number can be more than one digit.
22+
2. The operator `+/-`: We need to calculate the result based on the operator.
23+
3. `(`: We start to calculate the result inside the parenthese. We have parenthese that we have to calculate first, then sum up the results before and inside the parenthese. Since we need to cache the result and sign, we can use a stack to store the previous result and sign before the parenthese.
24+
4. `)`: We finish calculating the result inside the parenthese, we need to sum up the previous result (cached in the stack) and the current result inside the parenthese.
25+
26+
Here we use a variable `result` for the current evaluation, and we push the previous result and sign before the parenthese to the stack when we meet `(`, then we reset `result` for calculating the result inside the parenthese.
227

328
Let's use `9+1-(3+2)-5` as example, we add/substract the number one by one from the beginning, 0 + 9 at first and keep adding (`sign * number`) if we meet operator.
429

5-
* Nice explanation: https://leetcode.com/problems/basic-calculator/discuss/62361/Iterative-Java-solution-with-stack
30+
> Nice explanation: https://leetcode.com/problems/basic-calculator/discuss/62361/Iterative-Java-solution-with-stack
631
732
```kotlin
833
fun calculate(s: String): Int {
@@ -19,8 +44,8 @@ fun calculate(s: String): Int {
1944
number = number * 10 + (s[i] - '0')
2045
}
2146
else if (s[i] == '(') {
22-
// XXX +/- (
23-
// Cache the result XXX and sign before parenthese
47+
// XYZ +/- (
48+
// Cache the current result XYZ and sign before parenthese
2449
stack.push(result)
2550
stack.push(sign)
2651

leetcode/225.implement-stack-using-queues.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
## [225. Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues/)
22

3+
## Clarification Questions
4+
* No, it's clear from problem description.
5+
6+
## Test Cases
7+
### Normal Cases
8+
```
9+
Input:
10+
Output:
11+
```
12+
### Edge / Corner Cases
13+
*
14+
```
15+
Input:
16+
Output:
17+
```
18+
319
```kotlin
420
class MyStack() {
521

leetcode/232.implement-queue-using-stacks.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
## [232. Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/)
22

3+
## Clarification Questions
4+
* No, it's clear from problem description.
5+
6+
## Test Cases
7+
### Normal Cases
8+
```
9+
Input:
10+
Output:
11+
```
12+
### Edge / Corner Cases
13+
*
14+
```
15+
Input:
16+
Output:
17+
```
18+
319
We will keep the value stack to be the "right" order in queue.
420

521
```kotlin

leetcode/2866.beautiful-towers-ii.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
## [2866. Beautiful Towers II](https://leetcode.com/problems/beautiful-towers-ii/description/)
22

3+
## Clarification Questions
4+
* No, it's clear from problem description.
5+
6+
## Test Cases
7+
### Normal Cases
8+
```
9+
Input:
10+
Output:
11+
```
12+
### Edge / Corner Cases
13+
*
14+
```
15+
Input:
16+
Output:
17+
```
18+
319
**Idea!!** We iterate `i` from left to right and use `i` as peak to build the highest tower subject to the max height from left to `i` (peak).
420

521
```js

leetcode/316.remove-duplicate-letters.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
## [316. Remove Duplicate Letters](https://leetcode.com/problems/remove-duplicate-letters/description/)
22

3+
## Clarification Questions
4+
* No, it's clear from problem description.
5+
6+
## Test Cases
7+
### Normal Cases
8+
```
9+
Input:
10+
Output:
11+
```
12+
### Edge / Corner Cases
13+
*
14+
```
15+
Input:
16+
Output:
17+
```
18+
319
### Stack
420
```kotlin
521
fun removeDuplicateLetters(s: String): String {

leetcode/394.decode-string.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
## [394. Decode String](https://leetcode.com/problems/decode-string/)
22

3+
## Clarification Questions
4+
* No, it's clear from problem description.
5+
6+
## Test Cases
7+
### Normal Cases
8+
```
9+
Input:
10+
Output:
11+
```
12+
### Edge / Corner Cases
13+
*
14+
```
15+
Input:
16+
Output:
17+
```
18+
319
Let's use `30[a20[bc]d]efg` as example:
420
* We iterate all character and determine what the character is.
521
* For number digit, we will iterate until we meet `[`. and push the number, for example `123[`, we will parse to be `123` and push `123`.

leetcode/402.remove-k-digits.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
## [402. Remove K Digits](https://leetcode.com/problems/remove-k-digits/description/)
22

3+
## Clarification Questions
4+
* No, it's clear from problem description.
5+
6+
## Test Cases
7+
### Normal Cases
8+
```
9+
Input:
10+
Output:
11+
```
12+
### Edge / Corner Cases
13+
*
14+
```
15+
Input:
16+
Output:
17+
```
18+
319
### Monotonic Stack
420
Given sequence of digits, how can we build the smallest number? Suppose we have digits = `[1, 3, 2]`, then the smallest number would be `123` which is the digits in ascending order.
521

leetcode/496.next-greater-element-i.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
## [496. Next Greater Element I](https://leetcode.com/problems/next-greater-element-i/)
22

3+
## Clarification Questions
4+
* No, it's clear from problem description.
5+
6+
## Test Cases
7+
### Normal Cases
8+
```
9+
Input:
10+
Output:
11+
```
12+
### Edge / Corner Cases
13+
*
14+
```
15+
Input:
16+
Output:
17+
```
18+
319
* We can use monotonic stack (decreasing) to track the next greater element, for example, `[3, 2, 1, 6, 5]` we will push `3`, `2`, `1` but pop `3`, `2`, `1` when encountering `6`, means that the next greater element for `3`, `2`, `1` is `6`.
420
* We use hash table to track the item and its next greater element.
521

leetcode/503.next-greater-element-ii.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
## [503. Next Greater Element II](https://leetcode.com/problems/next-greater-element-ii/)
22

3+
## Clarification Questions
4+
* No, it's clear from problem description.
5+
6+
## Test Cases
7+
### Normal Cases
8+
```
9+
Input:
10+
Output:
11+
```
12+
### Edge / Corner Cases
13+
*
14+
```
15+
Input:
16+
Output:
17+
```
318
```kotlin
419
fun nextGreaterElements(nums: IntArray): IntArray {
520
val results = IntArray(nums.size) { _ -> -1 }

leetcode/622.design-circular-queue.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
## [622. Design Circular Queue](https://leetcode.com/problems/design-circular-queue/)
22

3+
## Clarification Questions
4+
* No, it's clear from problem description.
5+
6+
## Test Cases
7+
### Normal Cases
8+
```
9+
Input:
10+
Output:
11+
```
12+
### Edge / Corner Cases
13+
*
14+
```
15+
Input:
16+
Output:
17+
```
18+
319
```kotlin
420
class MyCircularQueue(k: Int) {
521

0 commit comments

Comments
 (0)