-
-
Notifications
You must be signed in to change notification settings - Fork 195
[HISEHOONAN] WEEK 01 solutions #1147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// Contains_Duplicate.swift | ||
// Algorithm | ||
// | ||
// Created by 안세훈 on 3/31/25. | ||
// | ||
|
||
import Foundation | ||
|
||
class Solution { | ||
func containsDuplicate(_ nums: [Int]) -> Bool { | ||
return nums.count != Set(nums).count | ||
//Set : 중복된 값을 갖지 않음. | ||
//문제로 주어진 배열의 개수와 중복을 갖지않는 Set연산의 개수의 차이 비교 | ||
//비교 후 다르다면 true 같다면 false | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// | ||
// House_Robber .swift | ||
// Algorithm | ||
// | ||
// Created by 안세훈 on 3/31/25. | ||
// | ||
///https://leetcode.com/problems/house-robber/description/ | ||
|
||
class Solution { | ||
//Dynamic Programming | ||
func rob(_ nums: [Int]) -> Int { | ||
|
||
if nums.count == 1 {return nums[0]} //배열이 1개인 경우 | ||
if nums.count == 2 {return max(nums[0],nums[1])} //배열이 2개인 경우 | ||
|
||
var dp = [nums[0], max(nums[0],nums[1])] | ||
//제일 base가 되는 두 값을 찾는게 제일 중요함. | ||
//nums[0]은 nums[1]보다 무조건 작아야함. | ||
for i in 2..<nums.count{ | ||
dp.append(max(dp[i-2]+nums[i],dp[i-1])) | ||
//dp배열의 i-2번째 배열 + nums의 i번째 배열 vs dp의 i-1번째 배열 중 큰걸 append | ||
// | ||
} | ||
|
||
return dp[dp.count-1] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// Longest_Consecutive_Sequence.swift | ||
// Algorithm | ||
// | ||
// Created by 안세훈 on 3/31/25. | ||
// | ||
|
||
///https://leetcode.com/problems/longest-consecutive-sequence/ | ||
|
||
|
||
class Solution { | ||
func longestConsecutive(_ nums: [Int]) -> Int { | ||
guard nums.count != 0 else { return 0 } | ||
|
||
var sortednums = Array(Set(nums)).sorted() //중복제거 + 오름차순 정렬 | ||
var maxcount = 1 //최대 카운드 | ||
var count = 1 // 연산용 카운트 | ||
|
||
for i in 0..<sortednums.count - 1 { | ||
if sortednums[i] == sortednums[i+1]-1 { | ||
//i번째 정렬된 리스트 와 i+1번째 리스트의 값 - 1이 같을 경우 | ||
count += 1 //카운트에 1+ | ||
maxcount = max(maxcount, count) //maxcount는 연산과, max중 큰 것 | ||
} else { | ||
count = 1 //아니면 1로 초기화. | ||
} | ||
} | ||
|
||
return maxcount | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// Top_K_Frequent_Elements .swift | ||
// Algorithm | ||
// | ||
// Created by 안세훈 on 3/31/25. | ||
// | ||
|
||
class Solution { | ||
func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] { | ||
var dic : [Int : Int] = [:] | ||
|
||
for i in nums { | ||
dic[i] = dic[i , default : 0] + 1 | ||
} | ||
|
||
var sortarray = dic.sorted{$0.value > $1.value} | ||
var answer : [Int] = [] | ||
|
||
for i in 0..<k{ | ||
answer.append(sortarray[i].key) | ||
} | ||
print(sortarray) | ||
print(answer) | ||
return answer | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// Two_Sum.swift | ||
// Algorithm | ||
// | ||
// Created by 안세훈 on 3/31/25. | ||
// | ||
|
||
class Solution { | ||
func twoSum(_ nums: [Int], _ target: Int) -> [Int] { | ||
for i in 0...nums.count-1{ | ||
for j in i+1...nums.count-1{ | ||
if nums[i] + nums[j] == target{ | ||
return [i,j] | ||
} | ||
} | ||
} | ||
return [] | ||
} | ||
//i번째 인덱스의 값과, j번째 인덱스의 값이 target과 같으면 i,j를 리턴 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
문제에 주석으로 해설 달아놓으신거 맛도리네요~ 나이쓰합니다~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
기억하기 편한거 같아요~!