leetcode-16-greedyAlgorithm
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的更多相关文章
- LeetCode 16. 3Sum Closest(最接近的三数之和)
LeetCode 16. 3Sum Closest(最接近的三数之和)
- [LeetCode] 16. 3Sum Closest 最近三数之和
Given an array nums of n integers and an integer target, find three integers in nums such that the s ...
- Java实现 LeetCode 16 最接近的三数之和
16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...
- 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 ...
- LeetCode——16. 3Sum Closest
一.题目链接:https://leetcode.com/problems/3sum-closest/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出3个数来,使得这三个数的 ...
- LeetCode 16 3Sum Closest (最接近target的3个数之和)
题目链接 https://leetcode.com/problems/3sum-closest/?tab=Description Problem : 找到给定数组中a+b+c 最接近targe ...
- LeetCode(16)题解--3Sum Closest
https://leetcode.com/problems/3sum-closest/ 题目: Given an array S of n integers, find three integers ...
- Leetcode 16. 3Sum Closest(指针搜索)
16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...
- 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 ...
- 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 ...
随机推荐
- jq:静态页面常会用到的jq操作(记录)
1 在php的mvc模式中通常需要做数据异步处理,其中也包含了对htm页面的操作,下面是近期长用到的jq操作,后续会继续补充 2 3 1:修改input框的value值 4 <input typ ...
- (转) Linux命令详解-date
Linux命令详解-date 原文:https://www.cnblogs.com/Dodge/p/4278292.html 在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到 ...
- Spark Mllib里如何将如温度、湿度和风速等数值特征字段用除以***进行标准化(图文详解)
不多说,直接上干货! 具体,见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的第18章 决策树回归分类Bike Sharing数据集
- @Primary 使用
造轮子的一个小小的发现 当一个接口被两个service实现时,controller调用接口实现功能,会报错,提示开发者指定service,官方是建议你使用@Qualifier来区分的,但是,总有另一种 ...
- SyntaxError: Use of const in strict mode.
具体报错console c:\Users\Administrator\WebstormProjects\blogtest\node_modules\connect-mongo\src\index.js ...
- 【Linux】Ubuntu配置zshell&oh-my-zsh
zshell:https://archive.codeplex.com/?p=shell oh-my-zsh: https://github.com/robbyrussell/oh-my-zsh 终极 ...
- JVM类加载之创建对象的过程
JVM对象的创建 new一个对象到底发生了什么? 1.VM遇到new关键字:首先检查是否能在常量池中定位到一个类的符号引用,并检查这个符号引用代表的类是否被加载,验证,准备,解析,初始化过,如果没有, ...
- IDEA的第一个JavaEE项目&集成Tomcat、Jrebel、Git插件
创建第一个JavaEE项目 Next之后选择项目存储位置之后点击Finish即可. 新建的项目需要新建两个文件夹classes和lib 集成Tomcat Tomcat自行到官网下载 JRebel集成 ...
- Vuforia切换回识别场景后黑屏解决
使用Vuforia SDK开发时,如果从其他非识别场景切换回识别场景,可能会出现黑屏问题. 解决方法是在切换到其他场景时,先将当前场景的Tracker信息全部Stop.代码如下: IEnumerato ...
- UNITY_MATRIX_MVP和UnityObjectToClipPos
在unity5.6以上版本中,shader中的UNITY_MATRIX_MVP将会被UnityObjectToClipPos替代,以后我们在写顶点函数时就是这样的 v2f vert(appdata v ...