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 ,这是能让孩子们满足胃口的饼干的最小尺寸:并且每块饼 ...
随机推荐
- SQL Server 数据导入与导出
1. BCP 命令 用法: bcp {dbtable | query} {in | out | queryout | format} 数据文件 [-m 最大错误数] [-f 格式化文件] [-e 错误 ...
- MVC中Cookies的简单读写操作
写入 public static void WriteCookie(string cn, string cv, DateTime Time) { HttpCookie cookie = new Htt ...
- hive支持事务及单行操作 update delete
测试环境 Hive 1.2.1000.2.6.0.3-8 set hive.support.concurrency=true; set hive.exec.dynamic.partition.mod ...
- CSS background 属性全家桶
介绍我们都知道css的background属性是一个复合属性,可以简写成一行代码,也可以将每个属性分开来写. background 简写属性在一个声明中设置所有的背景属性.如:body{ backgr ...
- my07_lock-tables与single-transaction的区别
概念描述 ************************************************************ mysqldump进行逻辑备份时(innodb),为保证事务的一致性 ...
- vue-cli中webpack配置解析
版本号 vue-cli 2.8.1 (终端通过vue -V 可查看) vue 2.2.2 webpack 2.2.1 目录结构 ├── README.md ├── build │ ├── build. ...
- Android文件/文件夹选择器(支持多选操作),已封装为lib库,直接添加依赖即可。
话不多少,先上图一览: 接下来我们开始写个app测试: 1.新建Android工程:FileSelectorTest 2.更改MainActivity: 在里面写四个textview模拟button, ...
- appium手机自动化环境搭建
在robotframework环境安装完成的基础上进行如下安装,如果没有安装rfs环境,请先参考robotframework安装文章:Robot Framework的环境搭建 文件下载地址:链接:ht ...
- Checkstyle的配置详解
Checkstyle是一款检查java程序代码样式的工具,可以有效的帮助我们检视代码以便更好的遵循代码编写标准,特别适用于小组开发时彼此间的样式规范和统一.Checkstyle提供了高可配置性,以便适 ...
- restful风格下的ajax跨域问题的解决
Ajax跨域请求时,如果设置Header的ContentType为application/json,会分两次发送请求一次先发送Method为OPTIONS的请求到服务器,这个请求会询问服务器支持哪些请 ...