From 14eecaffba53e9b12748fbc0f8f7a281863d95b5 Mon Sep 17 00:00:00 2001 From: Jihyun Son Date: Mon, 18 Aug 2025 16:15:24 +0900 Subject: [PATCH 1/2] best time to buy and sell stock --- .../sonjh1217.swift | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/sonjh1217.swift diff --git a/best-time-to-buy-and-sell-stock/sonjh1217.swift b/best-time-to-buy-and-sell-stock/sonjh1217.swift new file mode 100644 index 000000000..f03750ff8 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/sonjh1217.swift @@ -0,0 +1,18 @@ +class Solution { + func maxProfit(_ prices: [Int]) -> Int { + var maxProfit = 0 + var minPrice = prices[0] + + for i in (1.. Date: Fri, 22 Aug 2025 23:49:44 +0900 Subject: [PATCH 2/2] group anagrams --- group-anagrams/sonjh1217.swift | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 group-anagrams/sonjh1217.swift diff --git a/group-anagrams/sonjh1217.swift b/group-anagrams/sonjh1217.swift new file mode 100644 index 000000000..7682acb6e --- /dev/null +++ b/group-anagrams/sonjh1217.swift @@ -0,0 +1,18 @@ +class Solution { + func groupAnagrams(_ strs: [String]) -> [[String]] { + var stringsByCount = [[Int]: [String]]() + + strs.map { str in + 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) +} +