Skip to content

[doitduri] Week 05 Solution #1410

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 4 commits into from
May 3, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 18 additions & 0 deletions best-time-to-buy-and-sell-stock/doitduri.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
func maxProfit(_ prices: [Int]) -> Int {
guard var anchor = prices.first, prices.count > 1 else {
return 0
}

var result = 0
for price in prices {
Copy link
Contributor

Choose a reason for hiding this comment

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

약간의 최적화를 위해 for price in prices[1...]와 같은 구문을 사용하여 이미 anchor로 설정한 첫 번째 요소를 다시 확인하지 않아도 좋을 것 같습니다!

그 외에는 로직이 명확하고 시간복잡도 O(n)과 공간복잡도 O(1)로 효율적인 접근법으로 보입니다 👍

if price < anchor {
anchor = price
} else if price - anchor > result {
result = price - anchor
}
}

return result
}
}
16 changes: 16 additions & 0 deletions group-anagrams/doitduri.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
func groupAnagrams(_ strs: [String]) -> [[String]] {
Copy link
Contributor

Choose a reason for hiding this comment

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

애너그램 문제를 효율적으로 해결했고, 딕셔너리와 옵셔널 체이닝을 활용해서 깔끔하게 작성하신 것 같습니다. 정렬 방식으로 애너그램을 찾는 접근법도 명확하고 직관적인 것 같아요 👍

var groups: [String: [String]] = [:]

for str in strs {
let sortedStr = String(str.sorted())

var values = groups[sortedStr] ?? []
values.append(str)

groups[sortedStr] = values
}

return Array(groups.values)
}
}