[leetcode] 403. Frog Jump】的更多相关文章

A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water. Given a list of stones' positions (in units) in sorted ascending ord…
https://leetcode.com/contest/5/problems/frog-jump/ 这个题目,还是有套路的,之前做过一道题,好像是贪心性质,就是每次可以跳多远,最后问能不能跳到最右边,一会找一下这道题(找到了,就是这个55. Jump Game).然后这道题,差不多是相同的意思,但是每次只能跳3个或者2个,跳了之后还要判断那个位置有没有石头,然后还要记录上一次跳的间隔,然后我就想到了要用map做一个位置到index的映射,主要用于o(1)的查找,可以用unordered_map…
A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water. Given a list of stones' positions (in units) in sorted ascending ord…
leetcode403 我们维护青蛙从某个石头上可以跳那些长度的距离即可 用平衡树维护. 总的复杂度O(n^2logn) class Solution { public: bool canCross(vector<int>& stones) { map<int,int>po; int n=stones.size(); map<int,int>dis[1500]; for(int i=0;i<stones.size();i++) po[stones[i]]=…
题目如下: 解题思路:我的做法是建立一个字典dic,key为stone,value是一个set,里面存的是从前面的所有stone跳跃到当前stone的unit集合.例如stones=[0,1,2,3].stone3可以从stone1跳跃两步得到或者从stone2跳跃1步得到,所有dic[3] = (2,1).那么从stone3可以跳的unit就是[1,2,3] (set中每个元素+1或者-1得到),通过stone3就能到达stone4,stone5,stone6.这样只要按顺序遍历stones,…
一只青蛙想要过河. 假定河流被等分为 x 个单元格,并且在每一个单元格内都有可能放有一石子(也有可能没有). 青蛙可以跳上石头,但是不可以跳入水中.给定石子的位置列表(用单元格序号升序表示), 请判定青蛙能否成功过河(即能否在最后一步跳至最后一个石子上). 开始时, 青蛙默认已站在第一个石子上,并可以假定它第一步只能跳跃一个单位(即只能从单元格1跳至单元格2).如果青蛙上一步跳跃了 k 个单位,那么它接下来的跳跃距离只能选择为 k - 1.k 或 k + 1个单位. 另请注意,青蛙只能向前方(终…
做完了终于可以吃饭了,万岁~ 假设从stone[i]无法跳到stone[i+1]: 可能是,他们之间的距离超过了stone[i]所能跳的最远距离,0 1 3 7, 从3怎么都调不到7: 也可能是,他们之间的距离小于stone[i]能跳的最近距离,0 1 3 6 10 11,从10无法挑到11: 那么有没有可能下一个石头到当前的距离,小于最大值,大于最小值,因为中间有空白让我正好跳不到?(突然发现自己变成青蛙了) 我没用严谨的数学公式证明,但是写了几个式子发现不存在这种情况,如果出现,那么在i之前…
一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last in…
A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water. Given a list of stones' positions (in units) in sorted ascending ord…
A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water. Given a list of stones' positions (in units) in sorted ascending ord…