forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Design] Add solutions to Max Stack and Design HashMap
- Loading branch information
Showing
3 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |