Skip to content

Commit e334569

Browse files
author
Michael Ho
committed
Update solutions
1 parent b45a7dd commit e334569

File tree

160 files changed

+748
-5668
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+748
-5668
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// LeetCode: https://leetcode.com/problems/sqrtx/
2+
3+
import XCTest
4+
5+
class Solution {
6+
func mySqrt(_ x: Int) -> Int {
7+
var output = 0
8+
while output*output <= x {
9+
output += 1
10+
}
11+
return output - 1
12+
}
13+
}
14+
15+
class Tests: XCTestCase {
16+
let s = Solution()
17+
18+
func testSample1() {
19+
let input = 8
20+
let expected = 2
21+
XCTAssertEqual(expected, s.mySqrt(input))
22+
}
23+
24+
func testSample2() {
25+
let input = 4
26+
let expected = 2
27+
XCTAssertEqual(expected, s.mySqrt(input))
28+
}
29+
}
30+
31+
Tests.defaultTestSuite.run()

Problems/1-100/Easy/0070-ClimbingStairs.playground/Contents.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,22 @@ import XCTest
55

66
class Solution {
77
func climbStairs(_ n: Int) -> Int {
8+
guard n > 0 else { return 0 }
89
var arr = Array(repeating: 0, count: n)
910
arr[0] = 1
10-
guard n > 1 else { return arr[n-1] }
11-
arr[1] = 2
12-
guard n > 2 else { return arr[n-1] }
13-
for i in 2..<n {
14-
arr[i] = arr[i-1] + arr[i-2]
11+
if n > 1 {
12+
arr[1] = 2
13+
}
14+
15+
var idx = 2
16+
while idx < n {
17+
if idx - 1 >= 0 {
18+
arr[idx] += arr[idx-1]
19+
}
20+
if idx - 2 >= 0 {
21+
arr[idx] += arr[idx-2]
22+
}
23+
idx += 1
1524
}
1625
return arr[n-1]
1726
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
2+
<playground version='5.0' target-platform='macos'>
33
<timeline fileName='timeline.xctimeline'/>
44
</playground>

Problems/1-100/Hard/0057-InsertInterval.playground/Contents.swift

Lines changed: 33 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,93 +2,61 @@
22

33
import XCTest
44

5-
public class Interval {
6-
public var start: Int
7-
public var end: Int
8-
9-
public init(_ start: Int, _ end: Int) {
10-
self.start = start
11-
self.end = end
12-
}
13-
14-
public init(_ array: [Int]) {
15-
self.start = array[0]
16-
self.end = array[1]
17-
}
18-
19-
public func toArray() -> [Int] {
20-
return [self.start, self.end]
21-
}
22-
}
23-
245
class Solution {
25-
func insert(_ intervals: [Interval], _ newInterval: Interval) -> [Interval] {
26-
guard intervals.count > 0 else {
27-
return [newInterval]
28-
}
29-
var i = 0
30-
var output: [Interval] = []
6+
func insert(_ intervals: [[Int]], _ newInterval: [Int]) -> [[Int]] {
7+
guard intervals.count > 0 else { return [newInterval] }
8+
9+
var output = [[Int]]()
10+
var idx = 0
11+
var insertIdx = 0
3112
var newInterval = newInterval
32-
var appendNew = true
33-
while i < intervals.count {
34-
if (intervals[i].end < newInterval.start) || (intervals[i].start > newInterval.end) {
35-
output.append(intervals[i])
36-
i += 1
37-
continue
13+
while idx < intervals.count {
14+
let start = intervals[idx][0]
15+
let end = intervals[idx][1]
16+
if (end >= newInterval[0] && end <= newInterval[1]) || (start >= newInterval[0] && start <= newInterval[1]) {
17+
newInterval = [min(start, newInterval[0]), max(end, newInterval[1])]
18+
} else {
19+
insertIdx += 1
20+
output.append(intervals[idx])
3821
}
39-
var j = i
40-
while j < intervals.count, !((intervals[j].end < newInterval.start) || (intervals[j].start > newInterval.end)) {
41-
appendNew = false
42-
i = j
43-
newInterval = Interval(min(intervals[j].start, newInterval.start), max(intervals[j].end, newInterval.end))
44-
j += 1
45-
}
46-
i += 1
47-
output.append(newInterval)
48-
}
49-
if appendNew {
50-
output.append(newInterval)
51-
output.sort(by: { $0.start < $1.start })
22+
idx += 1
5223
}
53-
return output
24+
output.append(newInterval)
25+
return output.sorted(by: { $0[0] < $1[0] })
5426
}
5527
}
5628

5729
class Tests: XCTestCase {
5830
let s = Solution()
5931

6032
func testSample1() {
61-
let input = ([Interval(1,3), Interval(6,9)], Interval(2,5))
33+
let input = ([[1,3], [6,9]], [2,5])
6234
let expected = [[1,5],[6,9]]
63-
var output: [[Int]] = []
64-
for i in s.insert(input.0, input.1) {
65-
output.append(i.toArray())
35+
let output = s.insert(input.0, input.1)
36+
for o in output {
37+
expected.contains(o)
6638
}
67-
XCTAssertEqual(output, expected)
39+
XCTAssertEqual(output.count, expected.count)
6840
}
6941

7042
func testSample2() {
71-
let input = ([Interval(1,2), Interval(3,5), Interval(6,7), Interval(8,10), Interval(12,16)], Interval(4,8))
43+
let input = ([[1,2], [3,5], [6,7], [8,10], [12,16]], [4,8])
7244
let expected = [[1,2],[3,10],[12,16]]
73-
var output: [[Int]] = []
74-
for i in s.insert(input.0, input.1) {
75-
output.append(i.toArray())
45+
let output = s.insert(input.0, input.1)
46+
for o in output {
47+
expected.contains(o)
7648
}
77-
XCTAssertEqual(output, expected)
49+
XCTAssertEqual(output.count, expected.count)
7850
}
7951

8052
func testSample3() {
81-
let input = ([Interval(1,5)], Interval(6,8))
82-
let expected = [[1,5],[6,8]]
83-
var output: [[Int]] = []
84-
for i in s.insert(input.0, input.1) {
85-
output.append(i.toArray())
53+
let input = ([[1,5]], [6,8])
54+
let expected = [[1,5], [6,8]]
55+
let output = s.insert(input.0, input.1)
56+
for o in output {
57+
expected.contains(o)
8658
}
87-
XCTAssertEqual(output, expected)
88-
}
89-
90-
func testSample4() {
91-
59+
XCTAssertEqual(output.count, expected.count)
9260
}
9361
}
9462

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
2+
<playground version='5.0' target-platform='macos'>
33
<timeline fileName='timeline.xctimeline'/>
44
</playground>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// LeetCode: https://leetcode.com/problems/minimum-window-substring/
2+
3+
import XCTest
4+
5+
class Solution {
6+
func minWindow(_ s: String, _ t: String) -> String {
7+
guard s.count >= t.count else { return "" }
8+
9+
var tDict = [Character: Int]()
10+
for ch in t {
11+
tDict[ch] = tDict[ch] == nil ? 1 : tDict[ch]! + 1
12+
}
13+
14+
var sDict = [Character: Int]()
15+
for ch in s {
16+
if tDict[ch] == nil {
17+
continue
18+
}
19+
sDict[ch] = sDict[ch] == nil ? 1 : sDict[ch]! + 1
20+
}
21+
22+
let sArr = Array(s)
23+
var start = 0
24+
var end = sArr.count-1
25+
while start < end {
26+
if tDict[sArr[start]] == nil {
27+
start += 1
28+
} else if (tDict[sArr[start]] != nil && sDict[sArr[start]]! - 1 >= tDict[sArr[start]]!) {
29+
sDict[sArr[start]]! -= 1
30+
start += 1
31+
} else if tDict[sArr[end]] == nil {
32+
end -= 1
33+
} else if (tDict[sArr[end]] != nil && sDict[sArr[end]]! - 1 >= tDict[sArr[end]]!) {
34+
sDict[sArr[end]]! -= 1
35+
end -= 1
36+
} else {
37+
break
38+
}
39+
}
40+
return String(sArr[start...end])
41+
}
42+
}
43+
44+
class Tests: XCTestCase {
45+
let s = Solution()
46+
47+
func testSample1() {
48+
let input = ("ADOBECODEBANC", "ABC")
49+
let expected = "BANC"
50+
XCTAssertEqual(expected, s.minWindow(input.0, input.1))
51+
}
52+
}
53+
54+
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'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// LeetCode: https://leetcode.com/problems/maximal-rectangle/
2+
3+
import XCTest
4+
5+
class Solution {
6+
func maximalRectangle(_ matrix: [[Character]]) -> Int {
7+
return 0
8+
}
9+
}
10+
11+
class Tests: XCTestCase {
12+
let s = Solution()
13+
14+
func testSample1() {
15+
let input = [
16+
[Character("1"),Character("0"),Character("1"),Character("0"),Character("0")],
17+
[Character("1"),Character("0"),Character("1"),Character("1"),Character("1")],
18+
[Character("1"),Character("1"),Character("1"),Character("1"),Character("1")],
19+
[Character("1"),Character("0"),Character("0"),Character("1"),Character("0")]
20+
]
21+
let expected = 6
22+
XCTAssertEqual(expected, s.maximalRectangle(input))
23+
}
24+
}
25+
26+
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'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
 (0)