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 ,这是能让孩子们满足胃口的饼干的最小尺寸:并且每块饼 ...
随机推荐
- POJ:2976 Dropping tests(二分+最大化平均值)
Description In a certain course, you take n tests. If you get ai out of bi questions correct on test ...
- Linux学习grep,sed,awk工具的使用
1.grep:根据模式搜索文本并将符合模式的文本显示出来(pattern模式:由文本字符和正则表达式元字符所匹配出来的条件) 注:alias grep='grep --color' -c:打印符合要求 ...
- 关于Yii2中的MVC中的视图总结(持续更新中)
一.首先在控制器中,将处理好的数据发送给前台: $this->layout = 'base'; 这里填写视图的模板文件(可以不写这行代码,如果不写,默认为views/layouts/main.p ...
- 信息领域热词分析系统--python统计
统计词语出现的频率,并且按从高到低的顺序报错在文件中 def main(): file=open("F:\大数据\大作业\分词后的文件\data4_xinxi.txt",'r') ...
- my.资质
换算到每1点魔力初始,1点魔力等同于22点法资,等于0.00235点成长. 气血=等级*体力资质*0.002895+体力属性点*成长*7 魔法=等级*法力资质*0.002085+法力属性点*成长*5 ...
- 在Mac上配置iTerm2+Oh-My-Zsh&配置主题
本教程基本完全按照iTerm2 + Oh My Zsh 打造舒适终端体验配置 但是个人感觉博主的颜色搭配不合理,体现在补全命令的字体不清晰,提示命令与背景颜色太过相近 所以,再此之后使用了Bullet ...
- suau 公约公倍
8619 公约公倍 时间限制:500MS 内存限制:1000K 提交次数:475 通过次数:108 题型: 编程题 语言: G++;GCC Description 给定六个正整数a,b,c,d ...
- Java基础04-数据的输入
1.为什么要有数据的输入? 实现人机进行交互 2.什么是数据的输入? 利用扫描仪Scanner进行数据输入 3.怎么使用扫描仪Scanner? (1)放在类声明之前,引入扫描仪 import java ...
- maven 如何将自己的jar包添加到本地仓库
1 准备一个需要添加到本地仓库的jar包 我这里准备了一个名为mail.jar 的jar包,放到E:\Install Files目录下面 2 下面演示如何将准备的jar包添加到本地仓库 1 语法 mv ...
- WEBAPI测试
测试 #region 测试 [HttpPost] public HttpResponseMessage UserData(int userId, string userName) { var user ...