您的位置:首页 > 其它

Leetcode-403.Frog Jump(青蛙跳石头)

2016-11-15 20:05 706 查看
今天的题目是:Leetcode 403-青蛙跳

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 order, determine if the frog is able to cross the river by landing on the last stone. Initially, the frog is on the first stone and assume the first jump must be 1 unit.

If the frog's last jump was k units, then its next jump must be either k - 1, k, or k + 1 units. Note that the frog can only jump in the forward direction.

Note:
The number of stones is ≥ 2 and is < 1,100.
Each stone's position will be a non-negative integer < 231.
The first stone's position is always 0.

Example 1:
[0,1,3,5,6,8,12,17]

There are a total of 8 stones.
The first stone at the 0th unit, second stone at the 1st unit,
third stone at the 3rd unit, and so on...
The last stone at the 17th unit.

Return true. 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:
[0,1,2,3,4,8,9,11]

Return false. There is no way to jump to the last stone as
the gap between the 5th and 6th stone is too large.


Subscribe to see which companies asked this question

这道题和传统的跳台阶有所区别,传统的跳台阶问题可以分为两大类

1、每次可以调的台阶数小于或等于n,这种情况一般采用递归调用

2、每次跳的台阶数限定在某个数组num里面,比如说num={2,3 5},这种一般采用队列的思想和动态规划思想

这道题和第2种情况很相似,但是有一点不同的是,在跳台阶问题中,比如从1号台阶到100阶台阶,这其中的任何一个台阶都是可达的,但是本题的青蛙跳石头问题中,制定了

青蛙可以到达的Unit集合,比如{在序列num=【0, 1, 3, 6, 10, 13, 15, 18】,青蛙可以到达的unit编号依次只能是0,1,3......18,因为其它位置都是水,没有石头,所以不可达,最

终导致我们在求出一个青蛙可以跳到的新位置时,一定要验证这个位置上有没有石头,换句话说就是这个位置对应的unit在不在数组Num中。

本题一共采用两种数据结构来实现算法:

第一种:类型于队列的形式,队列采用map数据结构,代码如下

class Solution1 {
public:
bool is_in(multimap<int,int>&has,int value,int step){
multimap<int, int>::iterator iter = has.find(value);
while (iter != has.end()&&(*iter).first == value){
if ((*iter).second == step)return false;//找到相同的
iter++;
}
return true;//没找到相同的
}

bool canCross(vector<int>& stones) {
int size = stones.size(), pre = 0, now;
int end = stones[size - 1];
multimap<int, int>has;
has.insert(make_pair(0, 0));
multimap<int, int>::iterator iter = has.begin();
set<int>sto;
for (int i = 0; i < stones.size(); i++)sto.insert(stones[i]);
while (iter != has.end()){
pre = (*iter).second;//上一次走的步数
now = (*iter).first;//上一次landing的石头
if ((pre >= 1 && now + pre - 1 == end) || now + pre == end || now + pre + 1 == end)return true;
else {
if (pre>1 && find(sto.begin(), sto.end(), now + pre - 1) != sto.end()){
if(is_in(has,now+pre-1,pre-1))has.insert(make_pair(now + pre - 1, pre - 1));
}
if (pre > 0 && find(sto.begin(), sto.end(), now + pre) != sto.end()){
if (is_in(has, now + pre , pre ))has.insert(make_pair(now + pre, pre));
}
if (find(sto.begin(), sto.end(), now + pre + 1) != sto.end()){
if (is_in(has, now + pre + 1, pre + 1))has.insert(make_pair(now + pre + 1, pre + 1));
}
}
iter++;
}
return false;
}
};


但是出现运行超时!!!主要原因是队列太长,如果依次遍历处理时间会很长!!!!

第二种数据结构,采用map<int, set<int>> dp结构,代码如下

class Solution {
public:
bool canCross(vector<int>& stones) {
map<int, set<int>> dp;
dp.clear();
int u = 0, v = 0, w = 0;
set<int> st;
set<int>::iterator it;
dp[0].insert(1);//从0号开始可以向前跳的步数
for (int i = 0; i < stones.size(); ++i)st.insert(stones[i]);
for (int i = 0; i < stones.size(); ++i){
u = stones[i];
if (dp.find(u) == dp.end())continue;
if (u == stones[stones.size() - 1])break;
for (it = dp[u].begin(); it != dp[u].end(); ++it){
w = *it;//当前step
v = u + w;//当前到达的石头的Unit
if (st.find(v) != st.end() && v > u){
dp[v].insert(w - 1);
dp[v].insert(w);
dp[v].insert(w + 1);
}
}
}
return dp.find(u) != dp.end() && dp[u].size() > 0;
}
};


Submission Result: Accepted  More
Details 

Next challenges: (H) Edit Distance (M)
Unique Binary Search Trees II (M) Maximum Product Subarray


Share your acceptance!

在这种情况下,每个石头stones[i]的可达情况分开独立,map中的每个元素的first代表到达石头的编号,second表示的是从该含有石头的Unit开始跳可以跳的步数!!!!同时second采用set可以去重,这样大大缩短了时间!!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息