Question:
跳河问题。给一个0/1数组R代表一条河,0代表水,1代表石头。起始位置R[0]等于1,
初速度为1. 每一步可以选择以当前速度移动,或者当前速度加1再移动。只能停留在石
头上。问最少几步可以跳完整条河流。
给定数组为R=[1,1,1,0,1,1,0,0],最少3步能过河:
第一步先提速到2,再跳到R[2];
第二步先提速到3,再跳到R[5];
第三步保持速度3,跳出数组范围,成功过河。
题目来自: http://www.mitbbs.com/article_t1/JobHunting/32617501_0_1.html
Solution 1:
int FlogCrossRiver(string river) { if (river.empty()) { return 0; } vector<vector<pair<size_t, int>>> vp(river.size()); vp[0].emplace_back(1, 1); int res = INT_MAX; for (size_t i = 0; i < vp.size(); ++i) { if (river[i] == '0') { continue; } for (auto pr : vp[i]) { if (i + pr.first >= vp.size()) { res = min(pr.second, res); } else if (river[i + pr.first] == '1') { vp[i + pr.first].emplace_back(pr.first, pr.second + 1); } if (i + pr.first + 1 >= vp.size()) { res = min(pr.second, res); } else if (river[i + pr.first + 1] == '1') { vp[i + pr.first + 1].emplace_back(pr.first + 1, pr.second + 1); } } } return res; } void FlogCrossRiverTest() { cout << "11101100t" << FlogCrossRiver("11101100") << endl; cout << "11111111t" << FlogCrossRiver("11111111") << endl; cout << "11101000t" << FlogCrossRiver("11101000") << endl; cout << "11010101t" << FlogCrossRiver("11010101") << endl; }
Solution2:
DP状态方程:f(i,j) = min( f(i-j, j), f(i-j, j-1) ) + 1
i: 当前石头在数组中所处的index (0 based)
j: 到达i时的可能最大步数, <= sqrt(i*2) 假设处处有石子,那么青蛙可以随意跳跃,每次跳跃步伐比前一次跳跃大1,就有1+2+..+j = j(j+1)/2 = i, j^2+j = i*2, 所以 j <= sqrt(i*2)
f(i, j): 以步数j到达i时所需的最少跳跃次数,其中f(0,1)=0
时间和空间复杂度均为O(n^(3/2))
public int minFrogJumps(int[] a) { int len = a.length; int[][] f = new int[len][(int)Math.sqrt(len * 2) + 1]; int ans = Integer.MAX_VALUE; for (int i = 1; i < len; i++) { Arrays.fill(f[i], -1); if (a[i] == 0) { continue; } for (int j = 1; j <= (int)Math.sqrt(i * 2); j++) { if (a[i - j] == 0) { f[i][j] = -1; continue; } if (f[i - j][j] == -1 && f[i - j][j - 1] == -1) { continue; } if (f[i - j][j] != -1) f[i][j] = f[i - j][j] + 1; if (j > 1 && f[i - j][j - 1] != -1) { if (f[i][j] == -1) f[i][j] = f[i - j][j - 1] + 1; else f[i][j] = Math.min(f[i - j][j - 1] + 1, f[i][j]); } if (i + j + 1 >= len) { ans = Math.min(f[i][j] + 1, ans); } } } return ans; }
相关推荐
带跳多值随机微分方程不变测度的存在性、唯一性,关岳,巫静,本文主要研究L'evy驱动的多值随机微分方程解过程的指数遍历性问题。通过$L^2$-收敛结果,结合Girsanov变换、耦合方法和停止理论,对方程的
matlab频谱分析代码用于变更检测和时间序列分析的软件包 ...Jumps Upon Spectrum and Trend (JUST) JUSTdecompose - JUST Decomposition into Trend, Seasonal, and Remainder Components JUSTmonito
Special attention is paid to Poisson random measures and their roles in regulating the excursions of Brownian motion and the jumps of Levy and Markov processes. Each chapter has a large number of ...
传送门 题意: 开始位置在0,问能否跳到n+1位置 每步只能跳d 在1——n每个位置有方向,L,R,求d的最小值 思路: 只用找相邻两个R之间的最大值即可 代码: #include #include ...typedef long long l
Forward-backward doubly stochastic differential equations with random jumps and stochastic partial differential-integral equations,朱庆峰,石玉峰,A type of forward-backward doubly stochastic ...
根据提供的文件信息,我们可以从中提炼出以下知识点: 1. 带跳随机泛函Kolmogorov模型:文中介绍了一种特定的随机微分方程模型,即带跳的随机泛函Kolmogorov模型,用以模拟和分析人口数量的动态变化。...
- Ported most of Qemu's 'virtual VFAT' block driver (except runtime write support, but plus FAT32 suppport) - Added write protect option for floppy drives. - Bugfixes / improved internal debugger + ...
《K-SVD算法在图像分类中的应用及其源码解析》 K-SVD(K-Singular Value Decomposition,K-奇异值分解)算法是一种基于稀疏表示的非负矩阵分解方法,由Aharon等人于2006年提出。它不仅在图像处理领域,而且在信号...
Instead of starting at "Hello World," Wicked Cool PHP assumes that you're familiar with the language and jumps right into the good stuff. After you learn the FAQs of life-the most commonly wished for ...
在本文中,我们将探讨一类带有时滞的非线性中立型微分方程在Euler形式下,具有常数冲击跳跃和无界延迟的解的渐近行为。作者方芳江和孙继涛来自同济大学数学系,上海200092。关键词包括渐近行为、非线性中立型微分...
You are visitor as of October 17, 1996. The Art of Assembly Language Programming <br>Forward Why Would Anyone Learn This Stuff? 1 What's Wrong With Assembly Language 2 What's Right With ...
本文讨论了带跳扩散过程的随机序问题,阐述了这些过程之间的一个比较定理,并将此定理应用于研究带跳扩散过程的不变概率测度的存在性和唯一性。 首先,我们考虑的是定义在R^d中的扩散过程X(t),它是由随机微分方程...
Instead of starting at “Hello World,” Wicked Cool PHP assumes that you’re familiar with the language and jumps right into the good stuff. After you learn the FAQs of life – the most commonly ...
构造FILE结构时,关键步骤是将vtable从_IO_file_jumps改为_IO_str_jumps。这样,原本调用的IO_file_overflow会转而调用IO_str_overflow。IO_str_overflow函数接收FILE结构的地址作为参数,并连续调用malloc、memcpy...
This book skips the beginning level material and jumps right in to Visual C++. By the end of the book, you will be able to master the 32-bit power of Windows using Visual C++ as your programming ...
This book jumps into the world of Hadoop ecosystem components and its tools in a simplified manner, and provides you with the skills to utilize them effectively for faster and effective development of...
- `Walk past the house, turn left, and then walk along the path next to the river`: 走过房子,向左转,然后沿着河边的小路走。 - `Which way should we go at the traffic lights?`: 在交通灯处我们应该走哪...