455. Assign Cookies

解题思路:

先将两个数组按升序排序,然后从后往前遍历,当s[j] >= g[i]的时候,就把s[j]分给g[i],i,j都向前移动,count+1;否则向前移动i,直到可以找到这样的i。

还是很典型的贪心算法啊。

int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int i = g.size() - 1;
int j = s.size() - 1;
int count = 0;
while (i >= 0 && j >= 0) {
if (g[i] > s[j])
i --;
else {
i --;
j --;
count ++;
}
}
return count;
}

122. Best Time to Buy and Sell Stock II

解题思路:

这道题的话,只要考虑临近两天的情况就好了。如果第二天卖的价格高于第一天买入的价格,收益为正,就可以进行。另外,当prices为空或者只有一个

元素时,显然不能买入,收益应该保持0。

int maxProfit(vector<int>& prices) {
if (prices.size() <= 1)
return 0;
int i;
int sum = 0;
for (i = 0; i < prices.size() - 1; i++ ) {
sum += max((prices[i+1] - prices[i]), 0);
}
return sum;
}

与上面相关的是这道题:

121. Best Time to Buy and Sell Stock

解题思路:

遍历一遍prices,用Min表示当前找到的最小值,用Max记录最大收益。找Max时,如果当前prices[i]-Min < Max,那么Max就不变。

int maxProfit(vector<int>& prices) {
if (prices.size() <= 1)
return 0;
int Min = prices[0];
int Max = 0;
for (int i = 0; i < prices.size(); i++) {
Min = min(Min, prices[i]);
Max = max(Max, prices[i] - Min);
}
return Max;
}

  

  

leetcode-16-greedyAlgorithm的更多相关文章

  1. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  2. [LeetCode] 16. 3Sum Closest 最近三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  3. Java实现 LeetCode 16 最接近的三数之和

    16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...

  4. LeetCode 16. 3Sum Closest. (最接近的三数之和)

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  5. LeetCode——16. 3Sum Closest

    一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...

  6. LeetCode 16 3Sum Closest (最接近target的3个数之和)

    题目链接 https://leetcode.com/problems/3sum-closest/?tab=Description     Problem : 找到给定数组中a+b+c 最接近targe ...

  7. LeetCode(16)题解--3Sum Closest

    https://leetcode.com/problems/3sum-closest/ 题目: Given an array S of n integers, find three integers ...

  8. Leetcode 16. 3Sum Closest(指针搜索)

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

  9. Leetcode 16. 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  10. Java [leetcode 16] 3Sum Closest

    题目描述: Given an array S of n integers, find three integers in S such that the sum is closest to a giv ...

随机推荐

  1. They say Rome wasn't built in a day, and yet what a difference a day makes.

    They say Rome wasn't built in a day, and yet what a difference a day makes.有人说罗马不是一天建成的,但一天却能改变很多事.

  2. base-command

    命令分类 自带命令 工具型 文件系统 第三方命令 CLI GUI 操作文件系统 pwd(print working directory) ls(list files) ls 列出当前目录文件 ls 目 ...

  3. Chrome Java插件过期

    企业应用软件中,基本都是基于某个版本的JDK进行开发的,更新跟不上Oracle更新的步伐,Chrome浏览器自动默认关闭了过期插件导致用Chrome无法打开应用软件. 解决办法如下:

  4. Ubuntu获取root 权限,开机自动登入root

    新机器获取root权限,只需要给root 增加密码: sudo passwd root 修改开机自动登入: #sudo gedit /etc/lightdm/lightdm.conf 修改参数: au ...

  5. 常用浏览器User-Agent大全

    =======================PC浏览器======================== OperaMozilla/5.0 (Windows NT 6.1; WOW64) AppleW ...

  6. 数据库迁移后报错提示MySQL Error:Can''t find file errno: 13 - Permission denied的解决方法

    用户MYSQL数据库迁移后,遇到报错MySQL Error:Can't find file (errno: 13 - Permission denied)使用以下指令重新设置所有者和权限,依然不能解决 ...

  7. SpringBoot服务监控

    SpringBoot服务监控分为客户端和服务端,即服务端是监控方,客户端为被监控方. 例如需要对线上的SpringBoot服务project-A进行监控,则project-A 为客户端.而监控的服务p ...

  8. 一款新型的EASY饼图数据统计Jquery插件

    http://www.oschina.net/code/snippet_197014_12865   http://www.cnblogs.com/ada-zheng/p/3760947.html - ...

  9. uva 10328 - Coin Toss 投硬币(dp递推,大数)

    题意:抛出n次硬币(有顺序),求至少k个以上的连续正面的情况的种数. 思路:转换成求抛n个硬币,至多k-1个连续的情况种数,用所有可能出现的情况种数减去至多k-1个的情况,就得到答案了.此题涉及大数加 ...

  10. UVA 674 Coin Change 硬币转换(完全背包,常规)

    题意:有5种硬币,个数无限的,组成n元的不同方案有多少种? 思路:常规完全背包.重点在dp[0]=1,dp[j]中记录的是组成 j 元的方案数.状态转移方程dp[j+coin[i]]+=dp[j]. ...