leetcode: 贪心
1. jump game
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 index.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
当前的A[i]值代表,当前能跳的最大值,并不是一定要跳这么远的,比如A[0]=3,那么我们可以跳0到3步。判定这里的判断结束条件应该是:
记录lastJump的位置,循环i到lastJump,所有位置,找出其中的最大值maxJump =max{ i+A[i]};如果这个区间内的最大值maxJump <= lastJump,那么就返回假,因为没有任何一个位置可以跳出这个位置。
如果没有这样的位置,一直可以跳到最后,那么就返回为真。
public boolean canJump(int[] A) {
if(A.length <= 1)
return true;
int max = A[0]; //max stands for the largest index that can be reached.
for(int i=0; i<A.length; i++){
//if not enough to go to next
if(max <= i && A[i] == 0)
return false;
//update max
if(i + A[i] > max){
max = i + A[i];
}
//max is enough to reach the end
if(max >= A.length-1)
return true;
}
return false;
}
. jump-game-ii
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.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A =[2,3,1,1,4]
The minimum number of jumps to reach the last index is2. (Jump1step from index 0 to 1, then3steps to the last index.)
给定一个非负整数数组,给定的初始化位置在数组的起始位置。数组中的每个元素代表着你能都在此位置跳跃的最大的距离。你的目标是用最少的跳跃数达到数组的末尾。
比如:给定A = [2,3,1,1,4]达到数组尾部的最小的跳跃步数为2。(用1步从索引 0 到 1, 接着用3步到达结尾索引。)
public int jump(int[] A) {
if(A==null||A.length==0)
return 0;
int maxcover = 0;
int step = 0;
int lastcover = 0;
for(int i = 0; i<=maxcover&&i<A.length;i++){
if(i>lastcover){
step++;
lastcover = maxcover;
}
if(A[i]+i>maxcover)
maxcover = A[i]+i;
}
if(maxcover<A.length-1)
return 0;
return step;
}
3. maximum subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array[−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray[4,−1,2,1]has the largest sum =6.
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
这道题要求 求连续的数组值,加和最大。
试想一下,如果我们从头遍历这个数组。对于数组中的其中一个元素,它只有两个选择:
1. 要么加入之前的数组加和之中(跟别人一组)
2. 要么自己单立一个数组(自己单开一组)
所以对于这个元素应该如何选择,就看他能对哪个组的贡献大。如果跟别人一组,能让总加和变大,还是跟别人一组好了;如果自己起个头一组,自己的值比之前加和的值还要大,那么还是自己单开一组好了。
所以利用一个sum数组,记录每一轮sum的最大值,sum[i]表示当前这个元素是跟之前数组加和一组还是自己单立一组好,然后维护一个全局最大值即位答案。
public int maxSubArray(int[] A) {
int[] sum = new int[A.length];
int max = A[0];
sum[0] = A[0];
for (int i = 1; i < A.length; i++) {
sum[i] = Math.max(A[i], sum[i - 1] + A[i]);
max = Math.max(max, sum[i]);
}
return max;
}
leetcode: 贪心的更多相关文章
- Gas Station|leetcode 贪心
贪心:尽量将gas[i]-cost[i]>0的放在前面,gas[i]-cost[i]<0的放在后面.(路程的前面有汽油剩下,耗汽油的放在路程的后面). 能否全程通过的 条件 是:sum(g ...
- Leetcode 贪心 Best Time to Buy and Sell Stock
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie Best Time to Buy and Sell Stock Total Accepted ...
- leetcode 贪心算法
贪心算法中,是以自顶向下的方式使用最优子结构,贪心算法会先做选择,在当时看起来是最优的选择,然后再求解一个结果的子问题. 贪心算法是使所做的选择看起来都是当前最佳的,期望通过所做的局部最优选择来产生一 ...
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)
先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- [LeetCode] Jump Game II 贪心
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 【LeetCode】盛最多水的容器【双指针+贪心 寻找最大面积】
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...
- leetcode刷题-- 4. 贪心
贪心 455分发饼干 假设你是一位很棒的家长,想要给你的孩子们一些小饼干.但是,每个孩子最多只能给一块饼干.对每个孩子 i ,都有一个胃口值 gi ,这是能让孩子们满足胃口的饼干的最小尺寸:并且每块饼 ...
随机推荐
- SpringMVC中RequetContextListener
来自:https://www.cnblogs.com/softidea/p/7068196.html 零.引言 RequetContextListener从名字结尾Listener来看就知道属于监听器 ...
- SQL Server Reporting Service(SSRS) 第六篇 SSRS 部署总结
前段时间完成了第一批次SSRS报表的开发,本来以为大功已经告成,结果没有想到在整个发布与部署过程中还是遇到了很多的问题,现将这些问题一一列举出来,希望对以后能够有所启发! 1. 关于数据源与数据集的发 ...
- Head First 设计模式笔记(适配器)
1.定义: 将一个类的接口转换成客户期望的另外一个接口.适配器让原来不兼容的类可以合作无间. 例子:插座转接头. 2.类图: 3.说明: 埋坑 4.例子 埋坑
- python 缺失值的向前填充
method='bfill'可实现按下方值填充
- HTML_DOM学习
HTML DOM 树 通过ID/类名/标号可以定位HTML元素,然后可用JS改变这些元素的样式内容,并对DOM事件作出反应 对HTML事件的响应: onmousedown 和onmouseup/onc ...
- jquery mobile - select and input - horizontal - in same line
控件组合的水平布局 select + input 在同一行 注意jquery mobile 的js 和css 的版本, 一些低版本 估计不支持 <!DOCTYPE html> <ht ...
- (转)Shell脚本编程--Uniq命令
uniq 原文:http://blog.csdn.net/xifeijian/article/details/9209627 uniq命令可以去除排序过的文件中的重复行,因此uniq经常和sort合用 ...
- Beam的抽象模型
不多说,直接上干货! Apache Beam抽象模型 计算机最简单的抽象模型是输入+计算+输出.对于数据处理类的应用来说,将计算的部分展开,变成了 数据输入 + 数据集 + 数据处理 + ...
- Kudu-Master的设计
不多说,直接上干货! http://blog.csdn.net/lookqlp/article/details/70858466
- Kudu的概念术语
不多说,直接上干货! Columnar Data Store(列式数据存储) Kudu 是一个 columnar data store(列式数据存储).列式数据存储在强类型列中.由于几个原因,通过适当 ...