File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ func maxProfit( _ prices: [ Int ] ) -> Int {
3+ var maxProfit = 0
4+ var minPrice = prices [ 0 ]
5+
6+ for i in ( 1 ..< prices. count) {
7+ let profit = prices [ i] - minPrice
8+ maxProfit = max ( profit, maxProfit)
9+ minPrice = min ( prices [ i] , minPrice)
10+ }
11+
12+ return maxProfit
13+
14+ //시간복잡도 O(n)
15+ //공간복잡도 O(1)
16+ }
17+ }
18+
Original file line number Diff line number Diff line change 1+ class Solution {
2+ func groupAnagrams( _ strs: [ String ] ) -> [ [ String ] ] {
3+ var stringsByCount = [ [ Int] : [ String] ] ( )
4+
5+ strs. map { str in
6+ var countsByAlphabet = Array ( repeating: 0 , count: 26 )
7+ for char in str. unicodeScalars {
8+ countsByAlphabet [ Int ( char. value) - 97 ] += 1
9+ }
10+ stringsByCount [ countsByAlphabet, default: [ ] ] . append ( str)
11+ }
12+
13+ return Array ( stringsByCount. values)
14+ }
15+ //시간 O(n*L) L=string들의 평균 길이
16+ //공간 O(n*L)
17+ }
18+
You can’t perform that action at this time.
0 commit comments