We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b13f491 commit 2c456dfCopy full SHA for 2c456df
minimum-sideway-jumps/index.ts
@@ -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