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 ...
随机推荐
- sublime下Docblocker插件自定义配置
1.下载安装Docblocker插件 栏目preferences-->Package Control 输入 Docblocker 搜索插件,单击安装 简单使用: 在函数头部输入: /** 按 ...
- 详解window.history
http://blog.csdn.net/woxueliuyun/article/details/51075272
- net core mvc剖析:启动流程
net core mvc剖析:启动流程 asp.net core mvc是微软开源的跨平台的mvc框架,首先它跟原有的MVC相比,最大的不同就是跨平台,然后又增加了一些非常实用的新功能,比如taghe ...
- 转 dos 下的 find 和 重定向 and 删除
1.find /i "ora-" *.* > check.log del /Q .\log\*.* 附录: 我对findstr是如此的依赖,以至于当我向各位讲解find命令的 ...
- Main函数中的参数argc,argv的使用简单解析
本篇文章是对Main函数中的参数argc,argv的使用进行了简单的分析介绍,需要的朋友参考下: C/C++语言中的main函数,经常带有参数argc,argv,如下: int main(int a ...
- kindeditor编辑器和图片上传独立分开的配置细节
关于kindeditor编辑器上传按钮的异步加载最关键的部署问题,它的上传图片的组件都已经封装得很好了的,只需要监听到页面按钮的点击事件给编辑器对象传递一些对应的初始化参数即可显示图片上传的弹窗实现异 ...
- c#进行MD5加密方式和解密算法
--------------- 因为加密个解密都需要用到key所有在加密的后需要把key和加密码都存到数据库中 /// <summary> /// 唯一加密方式 /// </summ ...
- text-transform 字母的大小写
text-transform: none 默认 capitalize 每个单词以大写字母开头 uppercase 仅有大写字母 lowercase 无大写字母,仅有小写字母 i ...
- 适配器模式和php实现
1. 概述 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 2. 解决的问题 即Adapter模式使得原本由于接口不兼容而不 ...
- AtCoderBeginnerContest109题解
第一次AK,真爽qwq A 很zz啊,,直接判断三种情况就行 /* */ #include<iostream> #include<cstdio> #include<cst ...