File tree Expand file tree Collapse file tree 5 files changed +91
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +91
-0
lines changed Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+
You canโt perform that action at this time.
0 commit comments