File tree Expand file tree Collapse file tree 1 file changed +21
-21
lines changed Expand file tree Collapse file tree 1 file changed +21
-21
lines changed Original file line number Diff line number Diff line change 1
1
function minSideJumps ( obstacles : number [ ] ) : number {
2
- const n = obstacles . length
3
- let ans = 0
4
- let cur = 2 // 记录青蛙当前所在的跑道
5
- for ( let i = 0 ; i < n ; i ++ ) {
6
- if ( obstacles [ i + 1 ] !== cur ) continue
7
- // 记录另外两条跑道
8
- const other = ( cur + 1 ) % 3 || 3
9
- const another = ( cur + 2 ) % 3 || 3
2
+ const n = obstacles . length ;
3
+ let ans = 0 ;
4
+ let cur = 2 ; // 记录青蛙当前所在的跑道
5
+ for ( let i = 0 ; i < n ; i ++ ) {
6
+ if ( obstacles [ i + 1 ] !== cur ) continue ;
7
+ // 记录另外两条跑道
8
+ const other = ( cur + 1 ) % 3 || 3 ;
9
+ const another = ( cur + 2 ) % 3 || 3 ;
10
10
11
- // 求另两个跑道,下一个障碍物的位置
12
- let next = i
13
- while ( next < n && obstacles [ next ] !== other ) {
14
- next ++
11
+ // 求另两个跑道,下一个障碍物的位置
12
+ let next = i ;
13
+ while ( next < n && obstacles [ next ] !== other ) {
14
+ next ++ ;
15
+ }
16
+ while ( i < n && obstacles [ i ] !== another ) {
17
+ i ++ ;
18
+ }
19
+ cur = next > i ? other : another ;
20
+ i = Math . max ( next , i ) - 2 ;
21
+ ans ++ ;
15
22
}
16
- while ( i < n && obstacles [ i ] !== another ) {
17
- i ++
18
- }
19
- cur = next > i ? other : another
20
- i = Math . max ( next , i ) - 2
21
- ans ++
22
- }
23
- return ans
23
+ return ans ;
24
24
}
25
- export default minSideJumps
25
+ export default minSideJumps ;
You can’t perform that action at this time.
0 commit comments