Re: [闲聊] 每日LeetCode

楼主: Rushia (みけねこ的鼻屎)   2023-08-27 17:40:27
https://leetcode.com/problems/frog-jump/description/
403. Frog Jump
给你一个阵列 stones[],stones[i] 表示石头的座标,青蛙会从 stones[0] 开始跳,
第一次跳 1 步,后面可以跳 k, k + 1, k - 1 步(k 为上一次跳的步数),青蛙只能
走在石头上,求出青蛙是否可以跳到最后一个石头的位置。
Example 1:
Input: stones = [0,1,3,5,6,8,12,17]
Output: true
Explanation: The frog can jump to the last stone by jumping 1 unit to the 2nd
stone, then 2 units to the 3rd stone, then 2 units to the 4th stone, then 3
units to the 6th stone, 4 units to the 7th stone, and 5 units to the 8th
stone.
Example 2:
Input: stones = [0,1,2,3,4,8,9,11]
Output: false
Explanation: There is no way to jump to the last stone as the gap between the
5th and 6th stone is too large.
思路:
1.从第一个石头往后跳 1 步并标记目的地石头为可到达,之后遍历所有的石头,如果这
个石头是可到达的就往后跳 k, k+1, k-1 步,如果目的地是石头也标记为可到达。
2.最后检查最后一颗石头是不是可到达即可。
Java Code:
作者: UsadaBeguora (兔田背锅拉)   2022-08-27 17:40:00
露酱最强
作者: sustainer123 (caster)   2023-08-27 18:00:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com