当前位置:网站首页>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;
}
}边栏推荐
- [PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
- As an Android Developer programmer, Android advanced interview
- QT video transmission
- LeetCode 1626. The best team without contradiction
- 正在准备面试,分享面经
- The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
- 掌握这套精编Android高级面试题解析,oppoAndroid面试题
- 两类更新丢失及解决办法
- [designmode] flyweight pattern
- Temperature sensor chip used in temperature detector
猜你喜欢

QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏

【医学分割】attention-unet

DNS 系列(一):为什么更新了 DNS 记录不生效?

最新2022年Android大厂面试经验,安卓View+Handler+Binder

Sort out several important Android knowledge and advanced Android development interview questions

Personal notes of graphics (2)

【MySql进阶】索引详解(一):索引数据页结构

爬虫(17) - 面试(2) | 爬虫面试题库

AutoLISP series (1): function function 1
3000 words speak through HTTP cache
随机推荐
网关Gateway的介绍与使用
[designmode] template method pattern
谎牛计数(春季每日一题 53)
应用在温度检测仪中的温度传感芯片
LeetCode-SQL第一天
二叉搜索树(特性篇)
QML初学
Lowcode: four ways to help transportation companies enhance supply chain management
LeetCode 1043. Separate the array to get the maximum and daily questions
Interface oriented programming
Geoserver2.18 series (5): connect to SQLSERVER database
node:504报错
LeetCode 1774. 最接近目标价格的甜点成本 每日一题
LeetCode 403. 青蛙过河 每日一题
【Seaborn】组合图表:FacetGrid、JointGrid、PairGrid
Tidb cannot start after modifying the configuration file
LeetCode 152. 乘积最大子数组 每日一题
LeetCode 1186. Delete once to get the sub array maximum and daily question
Vs2019 configuration matrix library eigen
【DesignMode】模板方法模式(Template method pattern)