Skip to content

Commit b2e7678

Browse files
authored
Merge pull request soapyigu#235 from iHackSubhodip/master
Optimize solution to Decode String & LRU Cache
2 parents 587e642 + 111a510 commit b2e7678

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

Design/LRUCache.swift

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/lru-cache/
3+
* Primary idea: Use Doubly linked list and hash table to build the LRU cache.
4+
* Time Complexity: O(1), Space Complexity: O(n)
5+
*
6+
*/
7+
8+
class DoublyLinkedList{
9+
var key: Int
10+
var value: Int
11+
var previous: DoublyLinkedList?
12+
var next: DoublyLinkedList?
13+
var hashValue: Int
14+
15+
init(_ key: Int, _ value: Int) {
16+
self.key = key
17+
self.value = value
18+
self.hashValue = key
19+
}
20+
}
21+
22+
class LRUCache{
23+
var maxCapacity: Int
24+
var head: DoublyLinkedList
25+
var tail: DoublyLinkedList
26+
var cache: [Int: DoublyLinkedList]
27+
28+
init(_ maxCapacity: Int) {
29+
self.maxCapacity = maxCapacity
30+
self.cache = [Int: DoublyLinkedList]()
31+
self.head = DoublyLinkedList(Int.min, Int.min)
32+
self.tail = DoublyLinkedList(Int.max, Int.max)
33+
self.head.next = self.tail
34+
self.tail.previous = self.head
35+
}
36+
37+
func add(_ node: DoublyLinkedList){
38+
let next = head.next
39+
head.next = node
40+
node.previous = head
41+
node.next = next
42+
next?.previous = node
43+
}
44+
45+
func remove(_ node: DoublyLinkedList){
46+
let previous = node.previous
47+
let next = node.next
48+
previous?.next = next
49+
next?.previous = previous
50+
}
51+
52+
func get(_ key: Int) -> Int{
53+
if let node = cache[key]{
54+
remove(node)
55+
add(node)
56+
return node.value
57+
}
58+
return -1
59+
}
60+
61+
func put(_ key: Int, _ value: Int){
62+
if let node = cache[key]{
63+
remove(node)
64+
cache.removeValue(forKey: key)
65+
}else if cache.keys.count >= maxCapacity{
66+
if let leastNode = tail.previous{
67+
remove(leastNode)
68+
cache.removeValue(forKey: leastNode.key)
69+
}
70+
}
71+
let newNode = DoublyLinkedList(key, value)
72+
cache[key] = newNode
73+
add(newNode)
74+
}
75+
}

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
[Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/)| [Swift](./Stack/PreorderTraversal.swift)| Medium| O(n)| O(n)|
158158
[Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/)| [Swift](./Stack/InorderTraversal.swift)| Medium| O(n)| O(n)|
159159
[Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/)| [Swift](./Stack/PostorderTraversal.swift)| Hard| O(n)| O(n)|
160+
[Decode String](https://leetcode.com/problems/decode-string/)| [Swift](./Stack/DecodeString.swift)| Medium| O(n)| O(n)|
160161

161162

162163
## Tree
@@ -350,6 +351,11 @@
350351
[Graph Valid Tree](https://leetcode.com/problems/graph-valid-tree/)| [Swift](./UnionFind/GraphValidTree.swift)| Medium| O(nlogn)| O(n)|
351352
[Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/)| [Swift](./UnionFind/NumberIslandsII.swift)| Hard| O(klogmn)| O(mn)|
352353

354+
## Design
355+
| Title | Solution | Difficulty | Time | Space |
356+
| ----- | -------- | ---------- | ---- | ----- |
357+
[LRU Cache](https://leetcode.com/problems/lru-cache/)| [Swift](./Design/LRUCache.swift)| Hard| O(1)| O(n)|
358+
353359
## Google
354360
| Title | Solution | Difficulty | Frequency |
355361
| ----- | -------- | ---------- | --------- |

Stack/DecodeString.swift

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/decode-string/
3+
* Primary idea: Primary idea is to maintain two stacks[i.e. countStack and characterStack].
4+
* Traverse the given string and process the elements into the respective stacks, and accordingly update the result.
5+
* Time Complexity: O(n), Space Complexity: O(n)
6+
*
7+
*/
8+
9+
10+
class Solution {
11+
func decodeString(_ s: String) -> String {
12+
var result = ""
13+
var countStack = [Int]()
14+
var characterStack = [String]()
15+
let allowedDigits = Set(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"])
16+
var arrayString = Array(s), i = 0
17+
18+
while i < arrayString.count{
19+
20+
if allowedDigits.contains(String(arrayString[i])){
21+
22+
var count = 0
23+
while allowedDigits.contains(String(arrayString[i])){
24+
count = 10 * count + Int(String(arrayString[i]))!
25+
i += 1
26+
}
27+
countStack.append(count)
28+
}else if arrayString[i] == "["{
29+
30+
characterStack.append(result)
31+
result = ""
32+
i += 1
33+
}else if arrayString[i] == "]"{
34+
35+
if var temp = characterStack.popLast(), let repeatTime = countStack.popLast(){
36+
37+
for _ in 0..<repeatTime{
38+
temp.append(result)
39+
}
40+
result = temp
41+
}
42+
i += 1
43+
}else{
44+
45+
result.append(arrayString[i])
46+
i += 1
47+
}
48+
}
49+
50+
return result
51+
}
52+
}

0 commit comments

Comments
 (0)