Skip to content

Update dependencies #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions minimum-sideway-jumps/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
function minSideJumps(obstacles: number[]): number {
const n = obstacles.length
let ans = 0
let cur = 2 // 记录青蛙当前所在的跑道
for (let i = 0; i < n; i++) {
if (obstacles[i + 1] !== cur) continue
// 记录另外两条跑道
const other = (cur + 1) % 3 || 3
const another = (cur + 2) % 3 || 3
const n = obstacles.length;
let ans = 0;
let cur = 2; // 记录青蛙当前所在的跑道
for (let i = 0; i < n; i++) {
if (obstacles[i + 1] !== cur) continue;
// 记录另外两条跑道
const other = (cur + 1) % 3 || 3;
const another = (cur + 2) % 3 || 3;

// 求另两个跑道,下一个障碍物的位置
let next = i
while (next < n && obstacles[next] !== other) {
next++
// 求另两个跑道,下一个障碍物的位置
let next = i;
while (next < n && obstacles[next] !== other) {
next++;
}
while (i < n && obstacles[i] !== another) {
i++;
}
cur = next > i ? other : another;
i = Math.max(next, i) - 2;
ans++;
}
while (i < n && obstacles[i] !== another) {
i++
}
cur = next > i ? other : another
i = Math.max(next, i) - 2
ans++
}
return ans
return ans;
}
export default minSideJumps
export default minSideJumps;