File tree Expand file tree Collapse file tree 3 files changed +60
-0
lines changed
Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Original file line number Diff line number Diff line change 1+ function solution ( numbers ) {
2+ const answer = Array . from ( { length : numbers . length } , ( ) => - 1 ) ;
3+ const stack = [ ] ;
4+
5+ for ( let i = 0 ; i < numbers . length ; i ++ ) {
6+ while ( stack . length > 0 && numbers [ stack . at ( - 1 ) ] < numbers [ i ] ) {
7+ answer [ stack . pop ( ) ] = numbers [ i ] ;
8+ }
9+ stack . push ( i ) ;
10+ }
11+ return answer ;
12+ }
Original file line number Diff line number Diff line change 1+ function solution ( people , limit ) {
2+ let answer = 0 ;
3+ let left = 0 ;
4+ let right = people . length - 1 ;
5+ people . sort ( ( a , b ) => a - b ) ;
6+
7+ while ( left <= right ) {
8+ if ( people [ left ] + people [ right ] <= limit ) {
9+ left ++ ;
10+ right -- ;
11+ } else {
12+ right -- ;
13+ }
14+ answer ++ ;
15+ }
16+ return answer ;
17+ }
Original file line number Diff line number Diff line change 1+ function validateDir ( x , y , dir ) {
2+ switch ( dir ) {
3+ case "U" :
4+ return [ x , y + 1 ] ;
5+ case "D" :
6+ return [ x , y - 1 ] ;
7+ case "R" :
8+ return [ x + 1 , y ] ;
9+ case "L" :
10+ return [ x - 1 , y ] ;
11+ }
12+ }
13+
14+ function solution ( dirs ) {
15+ const visited = new Set ( ) ;
16+ let x = 0 ;
17+ let y = 0 ;
18+
19+ for ( dir of dirs ) {
20+ const [ nx , ny ] = validateDir ( x , y , dir ) ;
21+
22+ if ( nx >= - 5 && nx <= 5 && ny >= - 5 && ny <= 5 ) {
23+ visited . add ( `${ x } ${ y } ${ nx } ${ ny } ` ) ;
24+ visited . add ( `${ nx } ${ ny } ${ x } ${ y } ` ) ;
25+ x = nx ;
26+ y = ny ;
27+ }
28+ }
29+
30+ return visited . size / 2 ;
31+ }
You can’t perform that action at this time.
0 commit comments