-
-
Notifications
You must be signed in to change notification settings - Fork 246
[SeongA] WEEK 01 solutions #1696
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
12 commits
Select commit
Hold shift + click to select a range
5ed99e8
two sum solution
751a881
Add line Two Sum solution
delight010 98bc154
Update SeongA.swift
delight010 c520c84
Update SeongA.swift
delight010 c317868
Rename SeongA.swift to delight010.swift
delight010 33aca2b
contains duplicate
delight010 b2b7792
top k frequent elements
delight010 ffaa6d8
Add comments to top-k-frequent-element
delight010 0d7e8da
Add comment to two-sum
delight010 ad607f4
Add comments to contains-duplicate
delight010 d7bf9b6
solve longest consecutive sequence
delight010 37b336c
solve house robber
delight010 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,16 @@ | ||
class Solution { | ||
func containsDuplicate(_ nums: [Int]) -> Bool { | ||
var dictionary: [Int: Int] = [:] | ||
for (index, num) in nums.enumerated() { | ||
// 배열 nums의 개수만큼 반복합니다. 시간복잡도 O(n) | ||
if let value = dictionary[num], value != index { | ||
return true | ||
// 만일 동일한 숫자를 일찍 찾게 된다면 | ||
// 시간복잡도는 O(n)보다 더 빨라질 수 있습니다. | ||
} | ||
dictionary[num] = index | ||
} | ||
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,15 @@ | ||
class Solution { | ||
func rob(_ nums: [Int]) -> Int { | ||
var sum1 = 0 | ||
var sum2 = 0 | ||
for num in nums { | ||
// for문을 통해 루프. | ||
// 시간복잡도는 O(n) | ||
let temp = max(num + sum1, sum2) | ||
sum1 = sum2 | ||
sum2 = temp | ||
} | ||
return sum2 | ||
} | ||
} | ||
|
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 @@ | ||
class Solution { | ||
func longestConsecutive(_ nums: [Int]) -> Int { | ||
if nums.isEmpty { return 0 } | ||
// .isEmpty 메서드를 사용하여 배열의 개수가 0일 시 바로 0을 리턴합니다. | ||
// 시간복잡도 O(1) | ||
var maxCount = 0 | ||
var count = 1 | ||
var prefixNumber = nums.sorted().first ?? 0 | ||
for num in nums.sorted(by: <) { | ||
// nums 배열을 오름차순 정리 후 반복문을 루프합니다. | ||
// .sorted 시간복잡도 O(n log n) | ||
// for문 시간복잡도 O(n) | ||
if prefixNumber == num { | ||
continue | ||
} else if prefixNumber + 1 == num { | ||
count += 1 | ||
prefixNumber = num | ||
} else { | ||
maxCount = max(maxCount, count) | ||
count = 1 | ||
prefixNumber = num | ||
} | ||
} | ||
return max(maxCount, count) | ||
} | ||
} |
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,19 @@ | ||
class Solution { | ||
func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] { | ||
var dictionary: [Int: Int] = [:] | ||
for num in nums { // for loop를 돌면서 O(n)의 시간복잡도 | ||
dictionary[num, default: 0] += 1 | ||
// Swift에서 dictionary 검색의 시간복잡도는 O(1) | ||
} | ||
|
||
return dictionary | ||
.sorted(by: { $0.value > $1.value }) | ||
// dictionary sorted()의 시간복잡도 O(n log n) | ||
.prefix(k) | ||
// k의 개수만큼 탐색합니다. 고로 시간복잡도는 O(n) | ||
.map(\.key) | ||
// prefix에서 k만큼 탐색하였습니다. | ||
// .map의 시간복잡도는 O(n)입니다. | ||
} | ||
} | ||
|
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,15 @@ | ||
class Solution { | ||
func twoSum(_ nums: [Int], _ target: Int) -> [Int] { | ||
var dictionary: [Int: Int] = [:] | ||
for (index, value) in nums.enumerated() { | ||
// nums배열의 개수만큼 반복합니다. O(n) | ||
let difference = target - value | ||
if let otherIndex = dictionary[difference] { | ||
return [otherIndex, index] | ||
} | ||
dictionary[value] = index | ||
} | ||
return [] | ||
} | ||
} | ||
|
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.
dictionary를 이용하여 간단하고 직관적으로 문제 풀이를 하여 저도 많은 참고가 되었습니다~!