File tree 2 files changed +45
-0
lines changed
best-time-to-buy-and-sell-stock
2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } prices
3
+ * @return {number }
4
+ */
5
+ var maxProfit = function ( prices ) {
6
+
7
+ // 최대 수익
8
+ let maxProfit = 0 ;
9
+ // prices 배열의 0번째를 최소 가격으로 시작
10
+ let minPrice = prices [ 0 ] ;
11
+
12
+ // prices 배열 for문 돌려서 최대 수익 찾기
13
+ for ( let i = 0 ; i < prices . length ; i ++ ) {
14
+ minPrice = Math . min ( minPrice , prices [ i ] ) ;
15
+ let current = prices [ i ] - minPrice ;
16
+ maxProfit = Math . max ( current , maxProfit ) ;
17
+ }
18
+ // 최대 수익 반환
19
+ return maxProfit ;
20
+
21
+ } ;
22
+
23
+ // 시간 복잡도: O(n)
24
+ // 공간 복잡도: O(1)
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string[] } strs
3
+ * @return {string[][] }
4
+ */
5
+ var groupAnagrams = function ( strs ) {
6
+ // map에 담기
7
+ const map = new Map ( ) ;
8
+
9
+ // for문 돌기
10
+ for ( const str of strs ) {
11
+ // 정렬된 단어
12
+ const sortWord = str . split ( "" ) . sort ( ) . join ( "" ) ;
13
+ // map에 가지고 있지 않을 때
14
+ if ( ! map . has ( sortWord ) ) {
15
+ map . set ( sortWord , [ ] ) ;
16
+ }
17
+ map . get ( sortWord ) . push ( str ) ;
18
+ }
19
+
20
+ return Array . from ( map . values ( ) ) ;
21
+ } ;
You can’t perform that action at this time.
0 commit comments