-
-
Notifications
You must be signed in to change notification settings - Fork 195
[doitduri] Week01 Solutions #1154
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Solution { | ||
func containsDuplicate(_ nums: [Int]) -> Bool { | ||
let numbericSet = Set(nums) | ||
return numbericSet.count < nums.count | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DP로 풀이해서 직관적으로 잘 풀으신 것 같아요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네! 한번 살펴보겠습니다 😁👀 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution { | ||
func rob(_ nums: [Int]) -> Int { | ||
let length = nums.count | ||
|
||
if length == 1 { | ||
return nums[0] | ||
} | ||
|
||
if length == 2 { | ||
return max(nums[0], nums[1]) | ||
} | ||
|
||
var tables = Array(repeating: 0, count: length) | ||
tables[0] = nums[0] | ||
tables[1] = max(nums[0], nums[1]) | ||
|
||
for i in 2..<length { | ||
tables[i] = max(nums[i]+tables[i-2], tables[i-1]) | ||
} | ||
|
||
return tables[length-1] | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class Solution { | ||
func longestConsecutive(_ nums: [Int]) -> Int { | ||
var result = 1 | ||
var maxResult = 1 | ||
let sortedNums = nums.sorted() | ||
|
||
guard var previous = sortedNums.first else { | ||
return 0 | ||
} | ||
|
||
for index in 1..<sortedNums.count { | ||
let next = sortedNums[index] | ||
|
||
if previous == next { // 숫자가 같으면 카운팅하지 않는다. | ||
continue | ||
} | ||
|
||
if previous + 1 == next { | ||
result += 1 | ||
maxResult = max(maxResult, result) | ||
} else { | ||
result = 1 | ||
} | ||
|
||
previous = next | ||
} | ||
|
||
return maxResult | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Solution { | ||
func topKFrequent(_ nums: [Int], _ k: Int) -> [Int] { | ||
var tables: [Int: Int] = [:] | ||
|
||
for num in nums { | ||
tables[num] = (tables[num] ?? 0) + 1 | ||
} | ||
|
||
let values = tables.values.sorted().reversed().prefix(k) // k개의 frequent | ||
let result = tables.compactMap { (key: Int, value: Int) -> Int? in | ||
if values.contains(value) { | ||
return key | ||
} | ||
return nil | ||
} | ||
return result | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. 딕셔너리는 key-value 자료구조로 조회 시 O(1)인 장점을 이용해서 풀었는데요~, |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Solution { | ||
func twoSum(_ nums: [Int], _ target: Int) -> [Int] { | ||
let dict = nums.enumerated().reduce(into: [Int: Int]()) { initialValue, num in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reduce 를 사용해서 Dictionary를 만들 수 있는건 처음 알았네요! 새로운거 배워갑니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 허허.. 저는 반복문이 그렇게 싫더라구요(?) |
||
initialValue[num.element] = num.offset | ||
} | ||
|
||
for startIndex in 0..<nums.count { | ||
let findValue = target - nums[startIndex] | ||
if let endIndex = dict[findValue], startIndex != endIndex { | ||
return [startIndex, endIndex] | ||
} | ||
} | ||
|
||
return [] | ||
} | ||
} |
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.
Set에 nums를 먼저 담고, 길이를 비교하는 솔루션은 생각 못해 봤는데, 좋은 방법인 것 같습니다.
저는 Set에 nums를 하나씩 담으면서 이미 담긴 num이 있다면, 그 때 return하도록 했는데요!
시간복잡도는 O(N)으로 같겠지만, 처음에 거를 수도 있다는 부분도 참고해 보시면 좋을 것 같습니다!
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.
오오 early return 할 수도 있겠네요 ㅎㅎ 좋은 의견 감사합니다 🥰
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.
early return 명칭 너무 좋네요.
새로운거 알아갑니다.ㅎㅎ