Skip to content

Commit

Permalink
[Design] Add solutions to Max Stack and Design HashMap
Browse files Browse the repository at this point in the history
  • Loading branch information
soapyigu committed Dec 28, 2019
1 parent 9dcedb2 commit aea497c
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
62 changes: 62 additions & 0 deletions Design/DesignHashMap.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Question Link: https://leetcode.com/problems/design-hashmap/
* Primary idea: Use modulo and array of list / linked list to handle handle function and collision.
* Time Complexity: O(n), Space Complexity: O(n)
*/

class MyHashMap {

let keySpace = 2069
var buckets: [[(Int, Int)]]

/** Initialize your data structure here. */
init() {
buckets = Array(repeating: [(Int, Int)](), count: keySpace)
}

/** value will always be non-negative. */
func put(_ key: Int, _ value: Int) {
var bucket = buckets[key % keySpace]

if let index = find(key, bucket) {
bucket[index].1 = value
} else {
bucket.append((key, value))
}

buckets[key % keySpace] = bucket
}

/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
func get(_ key: Int) -> Int {
let bucket = buckets[key % keySpace]

if let index = find(key, bucket) {
return bucket[index].1
} else {
return -1
}
}

/** Removes the mapping of the specified value key if this map contains a mapping for the key */
func remove(_ key: Int) {
var bucket = buckets[key % keySpace]

guard let index = find(key, bucket) else {
return
}

bucket.swapAt(index, bucket.count - 1)
bucket.removeLast()

buckets[key % keySpace] = bucket
}

private func find(_ key: Int, _ bucket: [(Int, Int)]) -> Int? {
for (i, pair) in bucket.enumerated() where pair.0 == key {
return i
}

return nil
}
}
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![Leetcode](./logo.png?style=centerme)

## Progress
[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.
[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.

## Contributors

Expand Down Expand Up @@ -160,6 +160,7 @@
| Title | Solution | Difficulty | Time | Space |
| ----- | -------- | ---------- | ---- | ----- |
[Min Stack](https://leetcode.com/problems/min-stack/)| [Swift](./Stack/MinStack.swift)| Easy| O(1)| O(n)|
[Max Stack](https://leetcode.com/problems/max-stack/)| [Swift](./Stack/MaxStack.swift)| Easy| O(n)| O(n)|
[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)| [Swift](./Stack/ValidParentheses.swift)| Easy| O(n)| O(n)|
[Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/)| [Swift](./Stack/LongestValidParentheses.swift)| Hard| O(n)| O(n)|
[Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/)| [Swift](./Stack/EvaluateReversePolishNotation.swift)| Medium| O(n)| O(n)|
Expand Down Expand Up @@ -387,6 +388,7 @@
| Title | Solution | Difficulty | Time | Space |
| ----- | -------- | ---------- | ---- | ----- |
[Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/)| [Swift](./Design/ShuffleAnArray.swift)| Easy| O(n)| O(1)|
[Design HashMap](https://leetcode.com/problems/design-hashmap/)| [Swift](./Design/DesignHashMap.swift)| Easy| O(n)| O(n)|
[Design Tic-Tac-Toe](https://leetcode.com/problems/design-tic-tac-toe/)| [Swift](./Design/DesignTicTacToe.swift)| Medium| O(1)| O(n)|
[Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator)| [Swift](./Design/FlattenNestedListIterator.swift)| Medium| O(n)| O(n)|
[Flatten 2D Vector](https://leetcode.com/problems/flatten-2d-vector/)| [Swift](./Design/Vector2D.swift)| Medium | O(n)| O(n)|
Expand Down
54 changes: 54 additions & 0 deletions Stack/MaxStack.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Question Link: https://leetcode.com/problems/max-stack/
* Primary idea: Use a helper stack to keep track of the max value overall when a new
* element is pushed and also updates the helper stack when pop API is called
*
* Time Complexity: push - O(1), pop - O(1), top - O(1), peekMax - O(1), popMax - O(n)
* Space Complexity: O(n)
*/

class MaxStack {

var stack: [Int]
var maxStack: [Int]

/** initialize your data structure here. */
init() {
stack = [Int]()
maxStack = [Int]()
}

func push(_ x: Int) {
stack.append(x)
maxStack.append(maxStack.isEmpty ? x : max(x, maxStack.last!))
}

func pop() -> Int {
maxStack.removeLast()
return stack.removeLast()
}

func top() -> Int {
return stack.last!
}

func peekMax() -> Int {
return maxStack.last!
}

func popMax() -> Int {
let maxVal = peekMax()

// remove max from stack
var buffer = [Int]()
while top() != maxVal {
buffer.append(pop())
}
pop()
while !buffer.isEmpty {
push(buffer.removeLast())
}

return maxVal
}
}

0 comments on commit aea497c

Please sign in to comment.