File tree Expand file tree Collapse file tree 5 files changed +138
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +138
-0
lines changed Original file line number Diff line number Diff line change
1
+ /// Time, Space Complexity O(N)
2
+
3
+ class Solution {
4
+ func containsDuplicate( _ nums: [ Int ] ) -> Bool {
5
+ var seen = Set < Int > ( )
6
+ for num in nums {
7
+ if seen. contains ( num) {
8
+ return true
9
+ }
10
+ seen. insert ( num)
11
+ }
12
+ return false
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func rob( _ nums: [ Int ] ) -> Int {
3
+ let n = nums. count
4
+ if n == 0 { return 0 }
5
+ if n == 1 { return nums [ 0 ] }
6
+ if n == 2 { return max ( nums [ 0 ] , nums [ 1 ] ) }
7
+
8
+ //dp[i]는 i번까지 고려했을 때 가능한 최대 금액
9
+ var dp = [ Int] ( repeating: 0 , count: n)
10
+ dp [ 0 ] = nums [ 0 ]
11
+ dp [ 1 ] = max ( nums [ 0 ] , nums [ 1 ] ) //첫째 or 둘째 집 중 더 비싼 집 털기
12
+
13
+ //각 dp의 자리에는 바로 전 값을 그대로 가져오거나(i를 안털기), 전전집까지 턴거+i턴 값 중에서 비싼쪽 저장
14
+ for i in 2 ..< n {
15
+ dp [ i] = max ( dp [ i- 1 ] , dp [ i- 2 ] + nums[ i] )
16
+ }
17
+
18
+ //마지막 집까지 고려한 최대 이익 반환하기
19
+ return dp [ n - 1 ]
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func longestConsecutive( _ nums: [ Int ] ) -> Int {
3
+ let numSet = Set ( nums)
4
+ var longest = 0
5
+
6
+ for num in numSet {
7
+ // 연속 수열의 시작점인지 확인
8
+ if !numSet. contains ( num - 1 ) {
9
+ var currentNum = num
10
+ var currentStreak = 1
11
+
12
+ while numSet. contains ( currentNum + 1 ) {
13
+ currentNum += 1
14
+ currentStreak += 1
15
+ }
16
+
17
+ longest = max ( longest, currentStreak)
18
+ }
19
+ }
20
+
21
+ return longest
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ /*
3
+ 1차 시도
4
+ 복잡도 - O(n log n)
5
+ func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] {
6
+ var list: [Int:Int] = [:]
7
+ var result: [Int] = []
8
+ if nums.count == 1 { return nums }
9
+
10
+ for i in nums {
11
+ list[i, default: 0] += 1
12
+ }
13
+
14
+ var sortedList = list.sorted { $0.value > $1.value }
15
+ print(sortedList)
16
+
17
+ for i in 0..<k {
18
+ let target = sortedList[i].key
19
+ result.append(target)
20
+ // sortedList[target] = 0
21
+ }
22
+ return result
23
+ } */
24
+
25
+ // 2차 시도 : 복잡도 - O(n)
26
+ class Solution {
27
+ func topKFrequent( _ nums: [ Int ] , _ k: Int ) -> [ Int ] {
28
+ var freqMap = [ Int: Int] ( )
29
+
30
+ for num in nums {
31
+ freqMap [ num, default: 0 ] += 1
32
+ }
33
+
34
+ // 버켓에 빈도수를 인덱스로 값을 저장
35
+ var buckets = Array ( repeating: [ Int] ( ) , count: nums. count + 1 )
36
+ for (num, freq) in freqMap {
37
+ buckets [ freq] . append ( num)
38
+ }
39
+
40
+ var result = [ Int] ( )
41
+ for i in ( 0 ..< buckets. count) . reversed ( ) {
42
+ result += buckets [ i]
43
+ if result. count == k {
44
+ break
45
+ }
46
+ }
47
+
48
+ return result
49
+ }
50
+ }
51
+ }
Original file line number Diff line number Diff line change
1
+
2
+
3
+ class Solution {
4
+ // 복잡도 O(n^2)
5
+ // func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
6
+ // for (i, num) in nums.enumerated() {
7
+ // guard let tempIndex = nums.firstIndex(of: target - num) else {continue}
8
+ // if tempIndex == i { continue }
9
+ // return [i, tempIndex]
10
+ // }
11
+ // return [0]
12
+ // }
13
+
14
+ // 시간, 공간 복잡도 O(n)
15
+ // 해쉬맵 사용하기!
16
+ func twoSum( _ nums: [ Int ] , _ target: Int ) -> [ Int ] {
17
+ var list : [ Int : Int ] = [ : ]
18
+ for (i, num) in nums. enumerated ( ) {
19
+ if let exist = list [ target- num] {
20
+ return [ exist, i]
21
+ } else {
22
+ list [ num] = i
23
+ }
24
+ }
25
+ return [ 0 ]
26
+
27
+
28
+ }
29
+ }
You can’t perform that action at this time.
0 commit comments