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. Web自动化测试—PO设计模式(三)

    test_case目录下面放你要执行的用例 目录结构 ui_auto_test --src --test_case --__init.py --test_login_case --pages --__ ...

  2. java中实现定时任务 task 或quartz

    转载大神的 https://www.cnblogs.com/hafiz/p/6159106.html https://www.cnblogs.com/luchangyou/p/6856725.html ...

  3. NET Core源代码通过Autofac实现依赖注入

    查看.NET Core源代码通过Autofac实现依赖注入到Controller属性   阅读目录 一.前言 二.使用Autofac 三.最后 回到目录 一.前言 在之前的文章[ASP.NET Cor ...

  4. linux批量替换指定文件夹中所有文件的指定内容

    命令:sed -i "s#https#http#g" `grep http -rl VEROMODA` 功能:用来替换当前目录VEROMODA文件夹及子文件夹中所有文件中的http ...

  5. 网络编程之异步IO,rabbitMQ笔记

    对于网络并发编程而言,多线程与多进程算是最常见的需求场景了.毕竟网站开放就是想要更多的流量访问的. 回顾 回顾下之前学过的关于线程,进程和协程的知识点 IO密集型任务--用多线程更好计算密集型任务-- ...

  6. pixhawk 固件Firmware内执行make px4fmu-v2_default 编译报错解决办法

    执行下列指令报错 make px4fmu-v2_default /bin/sh: 1: Tools/check_cmake.sh: Permission denied Makefile:44: Not ...

  7. sublime text 快捷键新建.vue

    第一步:添加模板: 模板写法如下: <template> </template> <script type="ecmascript-6"> &l ...

  8. <Android 应用 之路> 天气预报(三)

    昨天介绍了基本的载入界面,今天介绍下天气信息显示界面的代码 基本ListView显示 搜索框,查询城市 上一篇文章中,载入界面通过showWeatherInfo()方法跳转到天气信息显示界面 priv ...

  9. informix服务端卸载后重新安装不成功

    可能原因: 1.实例未删除 2.配置文件未删除 安装成功后远程客户端连接不上问题: 1..如果自己设置的数据库实例报错,换一个数据库实例(database)试试,例如sysadmin

  10. javaSe-反射3

    package com.java.chap07.sec04; public class Student { private String name; private Integer age; publ ...