Skip to content

Commit ed15e40

Browse files
authored
Merge pull request #1696 from delight010/main
[SeongA] WEEK 01 solutions
2 parents b1fb402 + 37b336c commit ed15e40

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
func containsDuplicate(_ nums: [Int]) -> Bool {
3+
var dictionary: [Int: Int] = [:]
4+
for (index, num) in nums.enumerated() {
5+
// ๋ฐฐ์—ด nums์˜ ๊ฐœ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์‹œ๊ฐ„๋ณต์žก๋„ O(n)
6+
if let value = dictionary[num], value != index {
7+
return true
8+
// ๋งŒ์ผ ๋™์ผํ•œ ์ˆซ์ž๋ฅผ ์ผ์ฐ ์ฐพ๊ฒŒ ๋œ๋‹ค๋ฉด
9+
// ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(n)๋ณด๋‹ค ๋” ๋นจ๋ผ์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
10+
}
11+
dictionary[num] = index
12+
}
13+
return false
14+
}
15+
}
16+

โ€Žhouse-robber/delight010.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
func rob(_ nums: [Int]) -> Int {
3+
var sum1 = 0
4+
var sum2 = 0
5+
for num in nums {
6+
// for๋ฌธ์„ ํ†ตํ•ด ๋ฃจํ”„.
7+
// ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(n)
8+
let temp = max(num + sum1, sum2)
9+
sum1 = sum2
10+
sum2 = temp
11+
}
12+
return sum2
13+
}
14+
}
15+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
func longestConsecutive(_ nums: [Int]) -> Int {
3+
if nums.isEmpty { return 0 }
4+
// .isEmpty ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์˜ ๊ฐœ์ˆ˜๊ฐ€ 0์ผ ์‹œ ๋ฐ”๋กœ 0์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
5+
// ์‹œ๊ฐ„๋ณต์žก๋„ O(1)
6+
var maxCount = 0
7+
var count = 1
8+
var prefixNumber = nums.sorted().first ?? 0
9+
for num in nums.sorted(by: <) {
10+
// nums ๋ฐฐ์—ด์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ฆฌ ํ›„ ๋ฐ˜๋ณต๋ฌธ์„ ๋ฃจํ”„ํ•ฉ๋‹ˆ๋‹ค.
11+
// .sorted ์‹œ๊ฐ„๋ณต์žก๋„ O(n log n)
12+
// for๋ฌธ ์‹œ๊ฐ„๋ณต์žก๋„ O(n)
13+
if prefixNumber == num {
14+
continue
15+
} else if prefixNumber + 1 == num {
16+
count += 1
17+
prefixNumber = num
18+
} else {
19+
maxCount = max(maxCount, count)
20+
count = 1
21+
prefixNumber = num
22+
}
23+
}
24+
return max(maxCount, count)
25+
}
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] {
3+
var dictionary: [Int: Int] = [:]
4+
for num in nums { // for loop๋ฅผ ๋Œ๋ฉด์„œ O(n)์˜ ์‹œ๊ฐ„๋ณต์žก๋„
5+
dictionary[num, default: 0] += 1
6+
// Swift์—์„œ dictionary ๊ฒ€์ƒ‰์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(1)
7+
}
8+
9+
return dictionary
10+
.sorted(by: { $0.value > $1.value })
11+
// dictionary sorted()์˜ ์‹œ๊ฐ„๋ณต์žก๋„ O(n log n)
12+
.prefix(k)
13+
// k์˜ ๊ฐœ์ˆ˜๋งŒํผ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. ๊ณ ๋กœ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(n)
14+
.map(\.key)
15+
// prefix์—์„œ k๋งŒํผ ํƒ์ƒ‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
16+
// .map์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(n)์ž…๋‹ˆ๋‹ค.
17+
}
18+
}
19+

โ€Žtwo-sum/delight010.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
3+
var dictionary: [Int: Int] = [:]
4+
for (index, value) in nums.enumerated() {
5+
// nums๋ฐฐ์—ด์˜ ๊ฐœ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. O(n)
6+
let difference = target - value
7+
if let otherIndex = dictionary[difference] {
8+
return [otherIndex, index]
9+
}
10+
dictionary[value] = index
11+
}
12+
return []
13+
}
14+
}
15+

0 commit comments

Comments
ย (0)