Skip to content

Commit b4d6cc3

Browse files
author
Michael Ho
committed
Added LRU and level order traversal
1 parent 574de55 commit b4d6cc3

File tree

14 files changed

+644
-67
lines changed

14 files changed

+644
-67
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// LRU Cache
2+
// Design and implement a data structure for Least Recently Used (LRU) cache.
3+
// It should support the following operations: get and put.
4+
5+
import XCTest
6+
7+
class LRU {
8+
var capacity: Int
9+
var count = 0
10+
var head: Node?
11+
var tail: Node?
12+
var map = [Int:Node]()
13+
14+
init(_ capacity: Int) {
15+
self.capacity = capacity
16+
tail = Node(-1, -1)
17+
head = Node(-1, -1)
18+
head?.next = tail
19+
tail?.prev = head
20+
}
21+
22+
func get(_ key: Int) -> Int {
23+
if let node = map[key] {
24+
deleteNode(node)
25+
moveToHead(node)
26+
return node.val
27+
} else {
28+
return -1
29+
}
30+
}
31+
32+
// Method to manipulate linked list
33+
func moveToHead(_ node: Node?) {
34+
node?.prev = head
35+
head?.next = node
36+
node?.next = head?.next
37+
head?.next?.prev = node
38+
}
39+
40+
// Method to delete a node in linked list
41+
func deleteNode(_ node: Node?) {
42+
node?.prev?.next = node?.next
43+
node?.next?.prev = node?.prev
44+
}
45+
46+
func put(_ key: Int, _ val: Int) {
47+
if let node = map[key] {
48+
node.key = key
49+
node.val = val
50+
deleteNode(node)
51+
moveToHead(node)
52+
} else {
53+
if count == capacity, let last = tail?.prev {
54+
deleteNode(last)
55+
map[last.key] = nil
56+
count -= 1
57+
}
58+
let new = Node(key, val)
59+
map[key] = new
60+
moveToHead(new)
61+
count += 1
62+
}
63+
}
64+
}
65+
66+
// Node for doubly linked list
67+
class Node {
68+
var key: Int
69+
var val: Int
70+
var next: Node?
71+
var prev: Node?
72+
73+
init(_ key: Int, _ val: Int) {
74+
self.key = key
75+
self.val = val
76+
}
77+
}
78+
79+
class Tests: XCTestCase {
80+
81+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Data Structures/TreeTraversals.playground/Contents.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,34 @@ func inorderTraversal(_ root: TreeNode?) -> [Int] {
8484
return results
8585
}
8686

87+
// level order traversal
88+
// Result should be: [1, 2, 3, 4, 5, 6, 7]
89+
func levelOrder(_ root: TreeNode?) -> [[Int]] {
90+
var output = [[Int]]()
91+
helper(0, root, &output)
92+
return output
93+
}
94+
95+
private func helper(_ level: Int, _ root: TreeNode?, _ output: inout [[Int]]) {
96+
guard let root = root else {
97+
return
98+
}
99+
100+
if output.count - 1 < level {
101+
output.append([root.val])
102+
} else {
103+
output[level].append(root.val)
104+
}
105+
106+
if let left = root.left {
107+
helper(level+1, left, &output)
108+
}
109+
110+
if let right = root.right {
111+
helper(level+1, right, &output)
112+
}
113+
}
114+
87115
class Tests: XCTestCase {
88116
func testPreOrderTraversal() {
89117
let expected = [1, 2, 4, 5, 3, 6, 7]
@@ -99,6 +127,19 @@ class Tests: XCTestCase {
99127
let expected = [4, 2, 5, 1, 6, 3, 7]
100128
XCTAssertEqual(inorderTraversal(root), expected)
101129
}
130+
131+
func testLevelOrderTraversal() {
132+
let expected = [
133+
[1],
134+
[2, 3],
135+
[4, 5, 6, 7]
136+
]
137+
let output = levelOrder(root)
138+
XCTAssertEqual(output.count, expected.count)
139+
for i in 0..<output.count {
140+
XCTAssertEqual(output[i], expected[i])
141+
}
142+
}
102143
}
103144

104145
Tests.defaultTestSuite.run()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Leetcode: https://leetcode.com/problems/multiply-strings/
2+
3+
import XCTest
4+
5+
class Solution {
6+
func multiply(_ num1: String, _ num2: String) -> String {
7+
8+
return "String(output)"
9+
}
10+
}
11+
12+
class Tests: XCTestCase {
13+
let s = Solution()
14+
15+
func testSample1() {
16+
let input = ("2", "3")
17+
let expected = "6"
18+
XCTAssertEqual(s.multiply(input.0, input.1), expected)
19+
}
20+
21+
func testSample2() {
22+
let input = ("123", "456")
23+
let expected = "56088"
24+
XCTAssertEqual(s.multiply(input.0, input.1), expected)
25+
}
26+
}
27+
28+
Tests.defaultTestSuite.run()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 15 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,28 @@
11
// LeetCode: https://leetcode.com/problems/combinations/description/
2+
// Current - Runtime: 184 ms, faster than 100.00% of Swift online submissions for Combinations.
23

34
import XCTest
45

56
class Solution {
67
func combine(_ n: Int, _ k: Int) -> [[Int]] {
7-
if n == 0 {
8-
return []
9-
}
10-
var output: [[Int]] = []
11-
var current: [Int] = []
12-
if k < n/2 {
13-
combine(n, k, current, &output)
14-
} else {
15-
for i in 1...n {
16-
current.append(i)
17-
}
18-
guard n-k > 0 else {
19-
return [current]
20-
}
21-
remove(n-k, 0, current, &output)
8+
var output = [[Int]]()
9+
guard n > 0, k > 0, n >= k else {
10+
return output
2211
}
2312

24-
return output
25-
}
26-
27-
private func combine(_ n: Int, _ k: Int, _ current: [Int], _ output: inout [[Int]]) {
28-
if current.count == k {
29-
if !output.contains(current) {
30-
output.append(current)
31-
}
32-
return
33-
}
34-
let last = current.last ?? 1
35-
for i in last...n {
36-
if current.contains(i) {
37-
continue
13+
var i = n
14+
while i > 0 {
15+
if k == 1 {
16+
output.append([i])
17+
} else {
18+
for o in combine(i-1, k-1) {
19+
output.append([i] + o)
20+
}
3821
}
39-
var temp = current
40-
temp.append(i)
41-
combine(n, k, temp, &output)
42-
}
43-
}
44-
45-
private func remove(_ toRemove: Int, _ cursor: Int, _ current: [Int], _ output: inout [[Int]]) {
46-
if toRemove == 0 {
47-
if !output.contains(current) {
48-
output.append(current)
49-
}
50-
return
51-
}
52-
var idx = 0
53-
for (idx, i) in current.enumerated() {
54-
if i <= cursor {
55-
continue
56-
}
57-
var temp = current
58-
temp.remove(at: idx)
59-
remove(toRemove-1, i, temp, &output)
22+
23+
i -= 1
6024
}
25+
return output
6126
}
6227
}
6328

@@ -80,23 +45,6 @@ class Tests: XCTestCase {
8045
XCTAssertTrue(expected.contains(o))
8146
}
8247
}
83-
84-
func testSample2() {
85-
let input = (20, 16)
86-
let expected = [
87-
[2,4],
88-
[3,4],
89-
[2,3],
90-
[1,2],
91-
[1,3],
92-
[1,4],
93-
]
94-
let output = s.combine(input.0, input.1)
95-
XCTAssertEqual(output.count, expected.count)
96-
for o in output {
97-
XCTAssertTrue(expected.contains(o))
98-
}
99-
}
10048
}
10149

10250
Tests.defaultTestSuite.run()

0 commit comments

Comments
 (0)