当前位置:网站首页>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;
}
}边栏推荐
- LeetCode 120. 三角形最小路径和 每日一题
- Direct dry goods, 100% praise
- SqlServer2014+: 创建表的同时创建索引
- Process from creation to encapsulation of custom controls in QT to toolbar (I): creation of custom controls
- dapp丨defi丨nft丨lp单双币流动性挖矿系统开发详细说明及源码
- DNS 系列(一):为什么更新了 DNS 记录不生效?
- Interface oriented programming
- Opencv configuration 2019vs
- skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配
- Lowcode: four ways to help transportation companies enhance supply chain management
猜你喜欢

Skimage learning (3) -- gamma and log contrast adjustment, histogram equalization, coloring gray images

全网“追杀”钟薛高
3000 words speak through HTTP cache

Personal notes of graphics (4)

整理几个重要的Android知识,高级Android开发面试题

skimage学习(1)

QML beginner

Personal notes of graphics (1)

掌握这套精编Android高级面试题解析,oppoAndroid面试题
字节跳动Android金三银四解析,android面试题app
随机推荐
Record the migration process of a project
DNS 系列(一):为什么更新了 DNS 记录不生效?
SqlServer2014+: 创建表的同时创建索引
Binary search tree (basic operation)
最新高频Android面试题目分享,带你一起探究Android事件分发机制
Sqlserver2014+: create indexes while creating tables
LeetCode 1186. Delete once to get the sub array maximum and daily question
面向接口编程
Opportunity interview experience summary
低代码(lowcode)帮助运输公司增强供应链管理的4种方式
Three. JS series (2): API structure diagram-2
01tire+ chain forward star +dfs+ greedy exercise one
【MySql进阶】索引详解(一):索引数据页结构
谈谈 SAP 系统的权限管控和事务记录功能的实现
Personal notes of graphics (2)
掌握这套精编Android高级面试题解析,oppoAndroid面试题
LeetCode 1043. Separate the array to get the maximum and daily questions
Pisa-Proxy SQL 解析之 Lex & Yacc
skimage学习(3)——Gamma 和 log对比度调整、直方图均衡、为灰度图像着色
null == undefined