-
-
Notifications
You must be signed in to change notification settings - Fork 248
[kut7728] WEEK01 Solutions #1148
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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,14 @@ | ||
/// Time, Space Complexity O(N) | ||
|
||
class Solution { | ||
func containsDuplicate(_ nums: [Int]) -> Bool { | ||
var seen = Set<Int>() | ||
for num in nums { | ||
if seen.contains(num) { | ||
return true | ||
} | ||
seen.insert(num) | ||
} | ||
return 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,21 @@ | ||
class Solution { | ||
func rob(_ nums: [Int]) -> Int { | ||
let n = nums.count | ||
if n == 0 { return 0 } | ||
if n == 1 { return nums[0] } | ||
if n == 2 { return max(nums[0], nums[1]) } | ||
|
||
//dp[i]는 i번까지 고려했을 때 가능한 최대 금액 | ||
var dp = [Int](repeating: 0, count: n) | ||
dp[0] = nums[0] | ||
dp[1] = max(nums[0], nums[1]) //첫째 or 둘째 집 중 더 비싼 집 털기 | ||
|
||
//각 dp의 자리에는 바로 전 값을 그대로 가져오거나(i를 안털기), 전전집까지 턴거+i턴 값 중에서 비싼쪽 저장 | ||
for i in 2..<n { | ||
dp[i] = max(dp[i-1], dp[i-2] + nums[i]) | ||
} | ||
|
||
//마지막 집까지 고려한 최대 이익 반환하기 | ||
return dp[n - 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,23 @@ | ||
class Solution { | ||
func longestConsecutive(_ nums: [Int]) -> Int { | ||
let numSet = Set(nums) | ||
var longest = 0 | ||
|
||
for num in numSet { | ||
// 연속 수열의 시작점인지 확인 | ||
if !numSet.contains(num - 1) { | ||
var currentNum = num | ||
var currentStreak = 1 | ||
|
||
while numSet.contains(currentNum + 1) { | ||
currentNum += 1 | ||
currentStreak += 1 | ||
} | ||
|
||
longest = max(longest, currentStreak) | ||
} | ||
} | ||
|
||
return longest | ||
} | ||
} |
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,51 @@ | ||
class Solution { | ||
/* | ||
1차 시도 | ||
복잡도 - O(n log n) | ||
func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] { | ||
var list: [Int:Int] = [:] | ||
var result: [Int] = [] | ||
if nums.count == 1 { return nums } | ||
|
||
for i in nums { | ||
list[i, default: 0] += 1 | ||
} | ||
|
||
var sortedList = list.sorted { $0.value > $1.value } | ||
print(sortedList) | ||
|
||
for i in 0..<k { | ||
let target = sortedList[i].key | ||
result.append(target) | ||
// sortedList[target] = 0 | ||
} | ||
return result | ||
} */ | ||
|
||
// 2차 시도 : 복잡도 - O(n) | ||
class Solution { | ||
func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] { | ||
var freqMap = [Int: Int]() | ||
|
||
for num in nums { | ||
freqMap[num, default: 0] += 1 | ||
} | ||
|
||
// 버켓에 빈도수를 인덱스로 값을 저장 | ||
var buckets = Array(repeating: [Int](), count: nums.count + 1) | ||
for (num, freq) in freqMap { | ||
buckets[freq].append(num) | ||
} | ||
|
||
var result = [Int]() | ||
for i in (0 ..< buckets.count).reversed() { | ||
result += buckets[i] | ||
if result.count == k { | ||
break | ||
} | ||
} | ||
|
||
return result | ||
} | ||
} | ||
} |
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,29 @@ | ||
|
||
|
||
class Solution { | ||
// 복잡도 O(n^2) | ||
// func twoSum(_ nums: [Int], _ target: Int) -> [Int] { | ||
// for (i, num) in nums.enumerated() { | ||
// guard let tempIndex = nums.firstIndex(of: target - num) else {continue} | ||
// if tempIndex == i { continue } | ||
// return [i, tempIndex] | ||
// } | ||
// return [0] | ||
// } | ||
|
||
// 시간, 공간 복잡도 O(n) | ||
// 해쉬맵 사용하기! | ||
func twoSum(_ nums: [Int], _ target: Int) -> [Int] { | ||
var list: [Int:Int] = [:] | ||
for (i, num) in nums.enumerated() { | ||
if let exist = list[target-num] { | ||
return [exist, i] | ||
} else { | ||
list[num] = i | ||
} | ||
} | ||
return [0] | ||
|
||
|
||
} | ||
} |
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.
저는 unordered_map 으로 풀었는데 set 으로 푸는게 더 간결해 보이네요