Skip to content

[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

Merged
merged 5 commits into from
Apr 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions contains-duplicate/doitduri.swift
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
Copy link
Contributor

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)으로 같겠지만, 처음에 거를 수도 있다는 부분도 참고해 보시면 좋을 것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오오 early return 할 수도 있겠네요 ㅎㅎ 좋은 의견 감사합니다 🥰

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

early return 명칭 너무 좋네요.
새로운거 알아갑니다.ㅎㅎ

}
}
23 changes: 23 additions & 0 deletions house-robber/doitduri.swift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DP로 풀이해서 직관적으로 잘 풀으신 것 같아요!
직관적인 풀이법으로 풀면 시간/공간 복잡도는 O(N)/O(N) 인데, 공간 복잡도를 더 최적화하는 풀이법이 있더라구요!
공간 복잡도를 더 최적화한 솔루션은 코드가 직관적이진 않지만, 이 방법도 보시고 고민해보는 것도 좋을 것 같아요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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]
}
}
30 changes: 30 additions & 0 deletions longest-consecutive-sequence/doitduri.swift
Copy link
Contributor

Choose a reason for hiding this comment

The 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
}
}
18 changes: 18 additions & 0 deletions top-k-frequent-elements/doitduri.swift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍
(heap을 사용해서 시간복잡도를 좀 더 최적화한 방법도 풀어보시면 좋을 것 같습니다.)

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
}
}
16 changes: 16 additions & 0 deletions two-sum/doitduri.swift
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍
(dict 없이 더 최적화 할 수 있는 방법은 없을까요??)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

딕셔너리는 key-value 자료구조로 조회 시 O(1)인 장점을 이용해서 풀었는데요~,
지금 당장 생각나는 다른 풀이는. .. nums의 최대값 만큼의 array를 생성해서 동일한 로직으로 접근해볼 것 같습니다🙄 ㅠ
대신 공간복잡도가 증가한다는 단점이 있을 것 같습니다. 다양한 관점에서 의견 주셔서 감사합니다 ㅎㅎ

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reduce 를 사용해서 Dictionary를 만들 수 있는건 처음 알았네요!
기억이 맞다면 map으로는 불가능 했어서 무조건 반복문 돌리면서 dictionary를 만들었는데,,,

새로운거 배워갑니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 []
}
}