Skip to content
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
18 changes: 18 additions & 0 deletions best-time-to-buy-and-sell-stock/sonjh1217.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
func maxProfit(_ prices: [Int]) -> Int {
var maxProfit = 0
var minPrice = prices[0]

for i in (1..<prices.count) {
let profit = prices[i] - minPrice
maxProfit = max(profit, maxProfit)
Comment on lines +7 to +8
Copy link
Contributor

Choose a reason for hiding this comment

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

minPrice가 prices[i]보다 큰 경우에만 minPrice를 업데이트 하면 어떨까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If minPrice > prices[I] {
minPrice = prices[I]
}

요런 방식이 되겠네요! 사실 그러면 maxProfit = max(profit, maxProfit) 이 부분 역시
if profit > maxProfit {
maxProfit = profit
}

이렇게 될텐데요! 이 변경이 복잡도를 개선하는 것은 아니고, 업데이트를 덜 치기 때문에 약간 더 효율적이지만 min과 max가 주는 가독성이 저는 더 좋게 느껴져서 min, max 방식을 유지하려고 합니다. 디테일한 피드백 넘 감사합니다!

minPrice = min(prices[i], minPrice)
}

return maxProfit

//시간복잡도 O(n)
//공간복잡도 O(1)
}
}

18 changes: 18 additions & 0 deletions group-anagrams/sonjh1217.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
func groupAnagrams(_ strs: [String]) -> [[String]] {
var stringsByCount = [[Int]: [String]]()

strs.map { str in
Copy link
Contributor

Choose a reason for hiding this comment

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

map을 사용하셨는데, 반환값을 사용하지 않는 경우라면 forEach를 쓰면 의도가 더 잘 드러날 것 같습니다. 🙂

Copy link
Contributor Author

@sonjh1217 sonjh1217 Aug 24, 2025

Choose a reason for hiding this comment

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

그렇네요!! 사실 for loop만 많이 쓰다가 될수록 higher-order function을 써보려고 하고 있는데 초보 티가 났네용ㅎㅎ 감사합니다. 다음주 풀리에 반영해서 올릴게요~!

var countsByAlphabet = Array(repeating: 0, count: 26)
for char in str.unicodeScalars {
countsByAlphabet[Int(char.value) - 97] += 1
}
stringsByCount[countsByAlphabet, default: []].append(str)
}

return Array(stringsByCount.values)
}
//시간 O(n*L) L=string들의 평균 길이
//공간 O(n*L)
}