Skip to content

Commit aea497c

Browse files
committed
[Design] Add solutions to Max Stack and Design HashMap
1 parent 9dcedb2 commit aea497c

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed

Design/DesignHashMap.swift

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/design-hashmap/
3+
* Primary idea: Use modulo and array of list / linked list to handle handle function and collision.
4+
* Time Complexity: O(n), Space Complexity: O(n)
5+
*/
6+
7+
class MyHashMap {
8+
9+
let keySpace = 2069
10+
var buckets: [[(Int, Int)]]
11+
12+
/** Initialize your data structure here. */
13+
init() {
14+
buckets = Array(repeating: [(Int, Int)](), count: keySpace)
15+
}
16+
17+
/** value will always be non-negative. */
18+
func put(_ key: Int, _ value: Int) {
19+
var bucket = buckets[key % keySpace]
20+
21+
if let index = find(key, bucket) {
22+
bucket[index].1 = value
23+
} else {
24+
bucket.append((key, value))
25+
}
26+
27+
buckets[key % keySpace] = bucket
28+
}
29+
30+
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
31+
func get(_ key: Int) -> Int {
32+
let bucket = buckets[key % keySpace]
33+
34+
if let index = find(key, bucket) {
35+
return bucket[index].1
36+
} else {
37+
return -1
38+
}
39+
}
40+
41+
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
42+
func remove(_ key: Int) {
43+
var bucket = buckets[key % keySpace]
44+
45+
guard let index = find(key, bucket) else {
46+
return
47+
}
48+
49+
bucket.swapAt(index, bucket.count - 1)
50+
bucket.removeLast()
51+
52+
buckets[key % keySpace] = bucket
53+
}
54+
55+
private func find(_ key: Int, _ bucket: [(Int, Int)]) -> Int? {
56+
for (i, pair) in bucket.enumerated() where pair.0 == key {
57+
return i
58+
}
59+
60+
return nil
61+
}
62+
}

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![Leetcode](./logo.png?style=centerme)
55

66
## Progress
7-
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 308 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
7+
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 310 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
88

99
## Contributors
1010

@@ -160,6 +160,7 @@
160160
| Title | Solution | Difficulty | Time | Space |
161161
| ----- | -------- | ---------- | ---- | ----- |
162162
[Min Stack](https://leetcode.com/problems/min-stack/)| [Swift](./Stack/MinStack.swift)| Easy| O(1)| O(n)|
163+
[Max Stack](https://leetcode.com/problems/max-stack/)| [Swift](./Stack/MaxStack.swift)| Easy| O(n)| O(n)|
163164
[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)| [Swift](./Stack/ValidParentheses.swift)| Easy| O(n)| O(n)|
164165
[Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/)| [Swift](./Stack/LongestValidParentheses.swift)| Hard| O(n)| O(n)|
165166
[Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/)| [Swift](./Stack/EvaluateReversePolishNotation.swift)| Medium| O(n)| O(n)|
@@ -387,6 +388,7 @@
387388
| Title | Solution | Difficulty | Time | Space |
388389
| ----- | -------- | ---------- | ---- | ----- |
389390
[Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/)| [Swift](./Design/ShuffleAnArray.swift)| Easy| O(n)| O(1)|
391+
[Design HashMap](https://leetcode.com/problems/design-hashmap/)| [Swift](./Design/DesignHashMap.swift)| Easy| O(n)| O(n)|
390392
[Design Tic-Tac-Toe](https://leetcode.com/problems/design-tic-tac-toe/)| [Swift](./Design/DesignTicTacToe.swift)| Medium| O(1)| O(n)|
391393
[Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator)| [Swift](./Design/FlattenNestedListIterator.swift)| Medium| O(n)| O(n)|
392394
[Flatten 2D Vector](https://leetcode.com/problems/flatten-2d-vector/)| [Swift](./Design/Vector2D.swift)| Medium | O(n)| O(n)|

Stack/MaxStack.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/max-stack/
3+
* Primary idea: Use a helper stack to keep track of the max value overall when a new
4+
* element is pushed and also updates the helper stack when pop API is called
5+
*
6+
* Time Complexity: push - O(1), pop - O(1), top - O(1), peekMax - O(1), popMax - O(n)
7+
* Space Complexity: O(n)
8+
*/
9+
10+
class MaxStack {
11+
12+
var stack: [Int]
13+
var maxStack: [Int]
14+
15+
/** initialize your data structure here. */
16+
init() {
17+
stack = [Int]()
18+
maxStack = [Int]()
19+
}
20+
21+
func push(_ x: Int) {
22+
stack.append(x)
23+
maxStack.append(maxStack.isEmpty ? x : max(x, maxStack.last!))
24+
}
25+
26+
func pop() -> Int {
27+
maxStack.removeLast()
28+
return stack.removeLast()
29+
}
30+
31+
func top() -> Int {
32+
return stack.last!
33+
}
34+
35+
func peekMax() -> Int {
36+
return maxStack.last!
37+
}
38+
39+
func popMax() -> Int {
40+
let maxVal = peekMax()
41+
42+
// remove max from stack
43+
var buffer = [Int]()
44+
while top() != maxVal {
45+
buffer.append(pop())
46+
}
47+
pop()
48+
while !buffer.isEmpty {
49+
push(buffer.removeLast())
50+
}
51+
52+
return maxVal
53+
}
54+
}

0 commit comments

Comments
 (0)