Skip to content

Commit 2c456df

Browse files
authored
Create index.ts
1 parent b13f491 commit 2c456df

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

minimum-sideway-jumps/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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
10+
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++
22+
}
23+
return ans
24+
}
25+
export default minSideJumps

0 commit comments

Comments
 (0)