File tree 2 files changed +34
-0
lines changed
best-time-to-buy-and-sell-stock
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * https://leetcode.com/problems/best-time-to-buy-and-sell-stock
3
+ * time complexity : O(n)
4
+ * space complexity : O(1)
5
+ */
6
+
7
+ function maxProfit ( prices : number [ ] ) : number {
8
+ let maxProfit = 0 ;
9
+ let [ minPrice ] = prices ;
10
+
11
+ for ( let price of prices ) {
12
+ maxProfit = Math . max ( maxProfit , price - minPrice ) ;
13
+ minPrice = Math . min ( minPrice , price ) ;
14
+ }
15
+
16
+ return maxProfit ;
17
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * https://leetcode.com/problems/group-anagrams
3
+ * time complexity : O(n * k log k)
4
+ * space complexity : O(n * k)
5
+ */
6
+ function groupAnagrams ( strs : string [ ] ) : string [ ] [ ] {
7
+ const map = new Map ( ) ;
8
+
9
+ for ( const str of strs ) {
10
+ const sortedStr = str . split ( "" ) . sort ( ) . join ( "" ) ;
11
+
12
+ if ( map . has ( sortedStr ) ) map . get ( sortedStr ) . push ( str ) ;
13
+ else map . set ( sortedStr , [ str ] ) ;
14
+ }
15
+
16
+ return Array . from ( map . values ( ) ) ;
17
+ } ;
You can’t perform that action at this time.
0 commit comments