Skip to content

Commit 628e065

Browse files
author
changmuk.im
committed
solve : week 1 with swift
1 parent d190725 commit 628e065

File tree

5 files changed

+317
-0
lines changed

5 files changed

+317
-0
lines changed

contains-duplicate/EGON.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Foundation
2+
3+
class Solution {
4+
func containsDuplicate(_ nums: [Int]) -> Bool {
5+
return solve_2(nums)
6+
}
7+
8+
/*
9+
Runtime: 246 ms (Beats 68.44%)
10+
Analyze Complexity: O(n)
11+
Memory: 19.94 MB (Beats 76.01%)
12+
*/
13+
func solve_1(_ nums: [Int]) -> Bool {
14+
return nums.count != Set(nums).count
15+
}
16+
17+
/*
18+
Runtime: 240 ms (Beats 90.56%)
19+
Analyze Complexity: O(n)
20+
Memory: 22.56 MB (Beats 33.43%)
21+
*/
22+
func solve_2(_ nums: [Int]) -> Bool {
23+
var counter: [Int: Bool] = [:]
24+
for num in nums {
25+
if counter[num] != nil {
26+
return true
27+
} else {
28+
counter[num] = true
29+
}
30+
}
31+
32+
return false
33+
}
34+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import Foundation
2+
3+
public class TreeNode {
4+
public var val: Int
5+
public var left: TreeNode?
6+
public var right: TreeNode?
7+
public init() { self.val = 0; self.left = nil; self.right = nil; }
8+
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
9+
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
10+
self.val = val
11+
self.left = left
12+
self.right = right
13+
}
14+
}
15+
16+
class Solution {
17+
18+
/*
19+
Runtime: 28 ms (Beats 46.95%)
20+
Analyze Complexity: O(n)
21+
Memory: 16.52 MB (Beats 53.05%)
22+
*/
23+
func kthSmallest(_ root: TreeNode?, _ k: Int) -> Int {
24+
func inorderTraverse(_ root: TreeNode?, _ k: Int) {
25+
guard let root = root, visited.count < k else {
26+
return
27+
}
28+
29+
inorderTraverse(root.left, k)
30+
if visited.count < k {
31+
visited.append(root.val)
32+
}
33+
inorderTraverse(root.right, k)
34+
}
35+
36+
var visited: [Int] = []
37+
inorderTraverse(root, k)
38+
39+
return visited[k - 1]
40+
}
41+
}

number-of-1-bits/EGON.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Foundation
2+
3+
class Solution {
4+
func hammingWeight(_ n: Int) -> Int {
5+
return solve_2(n)
6+
}
7+
8+
/*
9+
Runtime: 5 ms (Beats 38.73%)
10+
Analyze Complexity: O(k), k는 2진수의 1의 갯수
11+
Memory: 14.88 MB (Beats 98.27%)
12+
*/
13+
func solve_1(_ n: Int) -> Int {
14+
return n.nonzeroBitCount
15+
}
16+
17+
/*
18+
Runtime: 3 ms (Beats 65.90%)
19+
Analyze Complexity: O(k), k는 2진수의 1의 갯수
20+
Memory: 15.08 MB (Beats 89.02%)
21+
*/
22+
func solve_2(_ n: Int) -> Int {
23+
var num = n
24+
var hammingWeight = 0
25+
while 0 < num {
26+
num &= num - 1
27+
hammingWeight += 1
28+
}
29+
30+
return hammingWeight
31+
}
32+
}

palindromic-substrings/EGON.swift

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import Foundation
2+
3+
class Solution {
4+
func countSubstrings(_ s: String) -> Int {
5+
return solve_2(s)
6+
}
7+
8+
/*
9+
Runtime: 507 ms (Beats 5.07%)
10+
Analyze Complexity: O(n ** 3)
11+
Memory: 16.07 MB (Beats 80.43%)
12+
*/
13+
func solve_1(_ s: String) -> Int {
14+
let sArray = Array(s)
15+
var count = 0
16+
for left in 0..<s.count {
17+
for right in left..<s.count {
18+
var isPalindrome = false
19+
for step in 0...(right-left) {
20+
if sArray[left + step] != sArray[right - step] {
21+
isPalindrome = true
22+
break
23+
}
24+
}
25+
26+
count += isPalindrome ? 1 : 0
27+
}
28+
}
29+
30+
return count
31+
}
32+
33+
/*
34+
Runtime: 8 ms (Beats 37.68%)
35+
Analyze Complexity: O(n ** 2)
36+
Memory: 16.14 MB (Beats 72.46%)
37+
*/
38+
func solve_2(_ s: String) -> Int {
39+
let sArray = Array(s)
40+
let n = sArray.count
41+
var count = 0
42+
43+
for center in 0..<(2 * n - 1) {
44+
var left = center / 2
45+
var right = left + (center % 2)
46+
47+
while 0 <= left && right < n && sArray[left] == sArray[right] {
48+
count += 1
49+
left -= 1
50+
right += 1
51+
}
52+
}
53+
54+
return count
55+
}
56+
57+
/*
58+
Runtime: 14 ms (Beats 30.43%)
59+
Analyze Complexity: O(n ** 2)
60+
Memory: 16.85 MB (Beats 13.04%)
61+
*/
62+
func solve_3(_ s: String) -> Int {
63+
let sArray = Array(s)
64+
let n = sArray.count
65+
var dp = Array(repeating: Array(repeating: false, count: n), count: n)
66+
var count = 0
67+
68+
for i in 0..<n {
69+
dp[i][i] = true
70+
count += 1
71+
}
72+
73+
for i in 0..<(n - 1) {
74+
dp[i][i + 1] = sArray[i] == sArray[i + 1]
75+
count += dp[i][i + 1] == true ? 1 : 0
76+
}
77+
78+
if n < 3 {
79+
return count
80+
}
81+
82+
for length in 3..<(n + 1) {
83+
for left in 0..<(n - length + 1) {
84+
let right = left + length - 1
85+
dp[left][right] = dp[left + 1][right - 1] == true && sArray[left] == sArray[right]
86+
count += dp[left][right] == true ? 1 : 0
87+
}
88+
}
89+
90+
return count
91+
}
92+
}

top-k-frequent-elements/EGON.swift

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import Foundation
2+
3+
final class MaxHeap<T: Comparable> {
4+
private var heap: [T] = []
5+
6+
func insert(_ value: T) {
7+
heap.append(value)
8+
heapifyUp(from: heap.count - 1)
9+
}
10+
11+
@discardableResult
12+
func pop() -> T? {
13+
guard !heap.isEmpty else {
14+
return nil
15+
}
16+
17+
if heap.count == 1 {
18+
return heap.removeLast()
19+
}
20+
21+
let removedValue = heap[0]
22+
heap[0] = heap.removeLast()
23+
heapifyDown(from: 0)
24+
25+
return removedValue
26+
}
27+
}
28+
29+
private extension MaxHeap {
30+
private func parentIndex(of index: Int) -> Int {
31+
return (index - 1) / 2
32+
}
33+
34+
private func leftChildIndex(of index: Int) -> Int {
35+
return 2 * index + 1
36+
}
37+
38+
private func rightChildIndex(of index: Int) -> Int {
39+
return 2 * index + 2
40+
}
41+
42+
private func heapifyUp(from index: Int) {
43+
var currentIndex = index
44+
while currentIndex > 0 {
45+
let parentIdx = parentIndex(of: currentIndex)
46+
if heap[currentIndex] > heap[parentIdx] {
47+
heap.swapAt(currentIndex, parentIdx)
48+
currentIndex = parentIdx
49+
} else {
50+
break
51+
}
52+
}
53+
}
54+
55+
private func heapifyDown(from index: Int) {
56+
var oldIndex = index
57+
let count = heap.count
58+
59+
while true {
60+
let leftIndex = leftChildIndex(of: oldIndex)
61+
let rightIndex = rightChildIndex(of: oldIndex)
62+
var newIndex = oldIndex
63+
64+
if leftIndex < count && heap[leftIndex] > heap[newIndex] {
65+
newIndex = leftIndex
66+
}
67+
68+
if rightIndex < count && heap[rightIndex] > heap[newIndex] {
69+
newIndex = rightIndex
70+
}
71+
72+
if newIndex == oldIndex {
73+
break
74+
}
75+
76+
heap.swapAt(oldIndex, newIndex)
77+
oldIndex = newIndex
78+
}
79+
}
80+
}
81+
82+
struct Item: Comparable {
83+
let num: Int
84+
let count: Int
85+
86+
static func < (lhs: Item, rhs: Item) -> Bool {
87+
return lhs.count <= rhs.count
88+
}
89+
}
90+
91+
class Solution {
92+
93+
/*
94+
Runtime: 30 ms (Beats 65.79%)
95+
Analyze Complexity: O(n log n), n개의 숫자에 대해 heappush, k개의 item에 대해 heappop
96+
Memory: 17.63 MB (Beats 64.66%)
97+
*/
98+
func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] {
99+
var countDict: [Int: Int] = [:]
100+
for num in nums {
101+
countDict[num] = (countDict[num] ?? 0) + 1
102+
}
103+
104+
var maxHeap = MaxHeap<Item>()
105+
for (num, count) in countDict {
106+
maxHeap.insert(Item(num: num, count: count))
107+
}
108+
109+
var result: [Int] = []
110+
for _ in 0..<k {
111+
if let maxItem = maxHeap.pop() {
112+
result.append(maxItem.num)
113+
}
114+
}
115+
116+
return result
117+
}
118+
}

0 commit comments

Comments
 (0)