File tree 5 files changed +121
-0
lines changed
longest-consecutive-sequence
5 files changed +121
-0
lines changed Original file line number Diff line number Diff line change
1
+ //
2
+ // Contains_Duplicate.swift
3
+ // Algorithm
4
+ //
5
+ // Created by 안세훈 on 3/31/25.
6
+ //
7
+
8
+ import Foundation
9
+
10
+ class Solution {
11
+ func containsDuplicate( _ nums: [ Int ] ) -> Bool {
12
+ return nums. count != Set ( nums) . count
13
+ //Set : 중복된 값을 갖지 않음.
14
+ //문제로 주어진 배열의 개수와 중복을 갖지않는 Set연산의 개수의 차이 비교
15
+ //비교 후 다르다면 true 같다면 false
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ //
2
+ // House_Robber .swift
3
+ // Algorithm
4
+ //
5
+ // Created by 안세훈 on 3/31/25.
6
+ //
7
+ ///https://leetcode.com/problems/house-robber/description/
8
+
9
+ class Solution {
10
+ //Dynamic Programming
11
+ func rob( _ nums: [ Int ] ) -> Int {
12
+
13
+ if nums. count == 1 { return nums [ 0 ] } //배열이 1개인 경우
14
+ if nums. count == 2 { return max ( nums [ 0 ] , nums [ 1 ] ) } //배열이 2개인 경우
15
+
16
+ var dp = [ nums [ 0 ] , max ( nums [ 0 ] , nums [ 1 ] ) ]
17
+ //제일 base가 되는 두 값을 찾는게 제일 중요함.
18
+ //nums[0]은 nums[1]보다 무조건 작아야함.
19
+ for i in 2 ..< nums. count{
20
+ dp. append ( max ( dp [ i- 2 ] + nums[ i] , dp [ i- 1 ] ) )
21
+ //dp배열의 i-2번째 배열 + nums의 i번째 배열 vs dp의 i-1번째 배열 중 큰걸 append
22
+ //
23
+ }
24
+
25
+ return dp [ dp. count- 1 ]
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ //
2
+ // Longest_Consecutive_Sequence.swift
3
+ // Algorithm
4
+ //
5
+ // Created by 안세훈 on 3/31/25.
6
+ //
7
+
8
+ ///https://leetcode.com/problems/longest-consecutive-sequence/
9
+
10
+
11
+ class Solution {
12
+ func longestConsecutive( _ nums: [ Int ] ) -> Int {
13
+ guard nums. count != 0 else { return 0 }
14
+
15
+ var sortednums = Array ( Set ( nums) ) . sorted ( ) //중복제거 + 오름차순 정렬
16
+ var maxcount = 1 //최대 카운드
17
+ var count = 1 // 연산용 카운트
18
+
19
+ for i in 0 ..< sortednums. count - 1 {
20
+ if sortednums [ i] == sortednums [ i+ 1 ] - 1 {
21
+ //i번째 정렬된 리스트 와 i+1번째 리스트의 값 - 1이 같을 경우
22
+ count += 1 //카운트에 1+
23
+ maxcount = max ( maxcount, count) //maxcount는 연산과, max중 큰 것
24
+ } else {
25
+ count = 1 //아니면 1로 초기화.
26
+ }
27
+ }
28
+
29
+ return maxcount
30
+ }
31
+ }
Original file line number Diff line number Diff line change
1
+ //
2
+ // Top_K_Frequent_Elements .swift
3
+ // Algorithm
4
+ //
5
+ // Created by 안세훈 on 3/31/25.
6
+ //
7
+
8
+ class Solution {
9
+ func topKFrequent( _ nums: [ Int ] , _ k: Int ) -> [ Int ] {
10
+ var dic : [ Int : Int ] = [ : ]
11
+
12
+ for i in nums {
13
+ dic [ i] = dic [ i , default : 0 ] + 1
14
+ }
15
+
16
+ var sortarray = dic. sorted { $0. value > $1. value}
17
+ var answer : [ Int ] = [ ]
18
+
19
+ for i in 0 ..< k{
20
+ answer. append ( sortarray [ i] . key)
21
+ }
22
+ print ( sortarray)
23
+ print ( answer)
24
+ return answer
25
+ }
26
+ }
Original file line number Diff line number Diff line change
1
+ //
2
+ // Two_Sum.swift
3
+ // Algorithm
4
+ //
5
+ // Created by 안세훈 on 3/31/25.
6
+ //
7
+
8
+ class Solution {
9
+ func twoSum( _ nums: [ Int ] , _ target: Int ) -> [ Int ] {
10
+ for i in 0 ... nums. count- 1 {
11
+ for j in i+ 1 ... nums. count- 1 {
12
+ if nums [ i] + nums[ j] == target{
13
+ return [ i, j]
14
+ }
15
+ }
16
+ }
17
+ return [ ]
18
+ }
19
+ //i번째 인덱스의 값과, j번째 인덱스의 값이 target과 같으면 i,j를 리턴
20
+ }
You can’t perform that action at this time.
0 commit comments