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. applicationContext中普通数据源不用jndi数据源

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  2. SpringBoot---核心---基本配置

    1.[入口类和@SpringBootApplication注解] 2.[关闭特定的配置] 3.[定制Banner] 1.1 修改Banner 1.2 关闭Banner 4.SpringBoot配置文件

  3. log4j.properties错误及配置详解

    当在Eclipse上运行MapReduce程序遇到以上问题时,请检查项目中是否有log4j.properties配置文件,或者配置文件是否正确. 刚接触Hadoop的时候不太了解log4j.prope ...

  4. MoinMoin install in apache (win)

    一:下载环境 xampp:http://sourceforge.net/projects/xampp/files/XAMPP%20Windows/1.8.1/xampp-win32-1.8.1-VC9 ...

  5. mysql 中unsigned

    整型的每一种都分有无符号(unsigned)和有符号(signed)两种类型(float和double总是带符号的),在默认情况下声明的整型变量都是有符号的类型(char有点特别),如果需声明无符号类 ...

  6. calibre电子书管理软件

    软件介绍: Calibre 是电子书管理软件,支持 Amazon.Apple.Bookeen.Ectaco.Endless Ideas.Google/HTC.Hanlin Song 设备及格式,功能十 ...

  7. Redis数据库1

    一.启动服务 #进入redis安装文件夹 cd /usr/local/redis/ #开启服务端(后端开启) ./bin/redis-server ./redis.conf #开启客户端 ./bin/ ...

  8. Game Engine Architecture

  9. groff - groff 文档排版系统前端

    总览 (SYNOPSIS) groff [ -abehilpstvzCENRSUVXZ ] [ -wname ] [ -Wname ] [ -mname ] [ -Fdir ] [ -Idir ] [ ...

  10. MFC:Unicode和多字节字符集下 CString和char的转化(MFC中)

    2011-05-16 00:10 1166人阅读 评论(0) 收藏 举报 mfcdelete Unicode下 CString转TCHAR TCHAR* szMsg = new TCHAR[wcsle ...