File tree Expand file tree Collapse file tree 3 files changed +53
-0
lines changed
best-time-to-buy-and-sell-stock Expand file tree Collapse file tree 3 files changed +53
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } prices
3+ * @return {number }
4+ */
5+ const maxProfit = ( prices ) => {
6+ let minPrice = prices [ 0 ] ;
7+ let maxProfit = 0 ;
8+
9+ for ( let i = 1 ; i < prices . length ; i ++ ) {
10+ minPrice = Math . min ( minPrice , prices [ i ] ) ;
11+ maxProfit = Math . max ( maxProfit , prices [ i ] - minPrice ) ;
12+ }
13+
14+ return maxProfit ;
15+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string[] } strs
3+ * @return {string[][] }
4+ */
5+ const groupAnagrams = ( strs ) => {
6+ const map = new Map ( ) ;
7+
8+ for ( const str of strs ) {
9+ const sorted = str . split ( "" ) . sort ( ) . join ( "" ) ;
10+
11+ if ( ! map . has ( sorted ) ) {
12+ map . set ( sorted , [ ] ) ;
13+ }
14+ map . get ( sorted ) . push ( str ) ;
15+ }
16+
17+ return [ ...map . values ( ) ] ;
18+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s
3+ * @param {string[] } wordDict
4+ * @return {boolean }
5+ */
6+ const wordBreak = ( s , wordDict ) => {
7+ const dp = new Array ( s . length + 1 ) . fill ( false ) ;
8+ dp [ 0 ] = true ;
9+
10+ for ( let i = 1 ; i <= s . length ; i ++ ) {
11+ for ( const word of wordDict ) {
12+ const start = i - word . length ;
13+ if ( start >= 0 && dp [ start ] && s . slice ( start , i ) === word ) {
14+ dp [ i ] = true ;
15+ }
16+ }
17+ }
18+
19+ return dp [ s . length ] ;
20+ } ;
You can’t perform that action at this time.
0 commit comments