File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ var maxProfit = function ( prices ) {
2
+ let minPrice = prices [ 0 ] ;
3
+ let maxProfit = 0 ;
4
+
5
+ for ( let i = 1 ; i < prices . length ; i ++ ) {
6
+ minPrice = Math . min ( minPrice , prices [ i ] ) ;
7
+ maxProfit = Math . max ( maxProfit , prices [ i ] - minPrice ) ;
8
+ }
9
+
10
+ return maxProfit ;
11
+ } ;
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
+ const hashMap = new Map ( ) ;
7
+ const res = [ ] ;
8
+ strs . forEach ( ( str , index ) => {
9
+ const sortedStr = [ ...str ] . sort ( ) . join ( '' ) ;
10
+ if ( hashMap . has ( sortedStr ) ) {
11
+ hashMap . set ( sortedStr , [ ...hashMap . get ( sortedStr ) , index ] ) ;
12
+ } else {
13
+ hashMap . set ( sortedStr , [ index ] ) ;
14
+ }
15
+ } ) ;
16
+ for ( const [ key , values ] of hashMap ) {
17
+ const anagrams = values . map ( ( v ) => strs [ v ] ) ;
18
+ res . push ( anagrams ) ;
19
+ }
20
+ return res ;
21
+ } ;
You can’t perform that action at this time.
0 commit comments