当前位置:网站首页>LeetCode 403. Frog crossing the river daily
LeetCode 403. Frog crossing the river daily
2022-07-07 16:59:00 【@Little safflower】
Problem description
A frog wants to cross the river . Suppose the river is divided equally into cells , And there may be a stone in each cell ( Maybe not ). Frogs can jump on stones , But don't jump into the water .
Here's a list of the locations of the stones stones( Use cell number Ascending Express ), Please judge whether the frog can cross the river successfully ( That is, whether we can jump to the last stone at the last step ). At the beginning of the , The frog stands on the first stone by default , And it can be assumed that its first step can only jump 1 A unit of ( That is, only from cells 1 Jump to cell 2 ).
If the frog jumps last k A unit of , Then its next jump distance can only be chosen as k - 1、k or k + 1 A unit of . Attention, please. , Frogs can only move forward ( The direction of the destination ) jumping .
Example 1:
Input :stones = [0,1,3,5,6,8,12,17]
Output :true
explain : Frogs can cross the river successfully , Jump as follows : jump 1 Units to 2 Block stone , Then jump 2 Units to 3 Block stone , next jump 2 Units to 4 Block stone , Then jump 3 Units to 6 Block stone , jump 4 Units to 7 Block stone , Last , jump 5 Units to 8 A stone ( The last stone ).
Example 2:Input :stones = [0,1,2,3,4,8,9,11]
Output :false
explain : This is because of the 5 And the 6 The space between the stones is too big , There is no alternative for frogs to jump over .
Tips :
2 <= stones.length <= 2000
0 <= stones[i] <= 231 - 1
stones[0] == 0
stones In strict ascending ordersource : Power button (LeetCode)
link :https://leetcode.cn/problems/frog-jump
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Java
class Solution {
public boolean canCross(int[] stones) {
int n = stones.length;
boolean[][] dp = new boolean[n + 1][n + 1];
dp[0][0] = true;
for(int i = 1;i < n;i++){
for(int j = 0;j < i;j++){
// From j Stone to i The minimum step size of a stone is k
int k = stones[i] - stones[j];
// The first j The maximum step length of a stone is j + 1
if(k <= j + 1){
// Can it be in steps k The speed reaches the i A stone
dp[i][k] = dp[j][k - 1] || dp[j][k] || dp[j][k + 1];
}
}
}
for(int i = 1;i <= n;i++){
if(dp[n - 1][i]) return true;
}
return false;
}
}
边栏推荐
- 01tire+ chain forward star +dfs+ greedy exercise one
- 【DesignMode】模板方法模式(Template method pattern)
- LeetCode 403. 青蛙过河 每日一题
- 【Seaborn】组合图表:PairPlot和JointPlot
- 最新阿里P7技术体系,妈妈再也不用担心我找工作了
- 深度监听 数组深度监听 watch
- Personal notes of graphics (4)
- URL和URI的关系
- QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
- logback. XML configure logs of different levels and set color output
猜你喜欢
随机推荐
QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
The latest interview experience of Android manufacturers in 2022, Android view+handler+binder
Module VI
Temperature sensor chip used in temperature detector
typescript ts 基础知识之类型声明
AutoLISP series (3): function function 3
字节跳动Android面试,知识点总结+面试题解析
LeetCode 120. 三角形最小路径和 每日一题
LeetCode 1986. 完成任务的最少工作时间段 每日一题
DAPP defi NFT LP single and dual currency liquidity mining system development details and source code
Opportunity interview experience summary
LeetCode 1696. 跳跃游戏 VI 每日一题
Personal notes of graphics (1)
OpenGL personal notes
谈谈 SAP 系统的权限管控和事务记录功能的实现
《产品经理必读:五种经典的创新思维模型》的读后感
Pycharm terminal enables virtual environment
null == undefined
Geoserver2.18 series (5): connect to SQLSERVER database
Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions