LeetCode——Best Time to Buy and Sell Stock II
Description:
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
知道股价,可以有多次买卖,必须先买后卖,求最大利润。
贪心,只要股价比买价高就卖出。
public class Solution {
public int maxProfit(int[] prices) {
int res = 0;
for(int i=0; i<prices.length-1; i++) {
if(prices[i] < prices[i+1]) {
res += prices[i+1] - prices[i];
}
} return res; }
}
LeetCode——Best Time to Buy and Sell Stock II的更多相关文章
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)
先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...
- LeetCode: Best Time to Buy and Sell Stock II 解题报告
Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...
- [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode——Best Time to Buy and Sell Stock II (股票买卖时机问题2)
问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- [leetcode]Best Time to Buy and Sell Stock II @ Python
原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题意: Say you have an array ...
- [LeetCode] Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode: Best Time to Buy and Sell Stock II [122]
[题目] Say you have an array for which the ith element is the price of a given stock on day i. Design ...
- [Leetcode] Best time to buy and sell stock ii 买卖股票的最佳时机
Say you have an array for which the i th element is the price of a given stock on day i. Design an a ...
随机推荐
- dpkg制作deb包
deb包的文件结构: deb包里面的结构:DEBIAN目录 和 软件具体安装目录(模拟安装目录)(如etc, usr, opt, tmp等). 在DEBIAN目录中至少有control文件,还可能有p ...
- Hibernate使用createSqlQuery进行模糊查询时找不到数据
1. 首先明确一点,使用createSqlQuery如下两种方式的占位符都可以使用,这个在官方的文档可以查到. 注意使用模糊查询时,赋值两边不可以添加单引号. Query query = sess.c ...
- import_tasks: tasks/sometasks.yml
- import_tasks: tasks/sometasks.yml when: "'reticulating splines' in output" unarchive模块用 ...
- Ajax-jQuery_Ajax_实例 ($.ajax、$.post、$.get)
Jquery在异步提交方面封装的很好,直接用AJAX非常麻烦,Jquery大大简化了我们的操作,不用考虑浏览器的诧异了. 推荐一篇不错的jQuery Ajax 实例文章,忘记了可以去看看, 地址为:h ...
- Windoows窗口程序五
程序执行机制 过程驱动-程序的执行过程是按照预订好的顺序执行. 事件驱动-程序的执行是无序,用户可以根据需要随机触发相应的事件. Win32窗口程序就是采用事件驱动方式执行,也就是消息机制. 当系统通 ...
- 解决只有单引号的Json格式转换成bean问题
objectMapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);单引号类似Json格式:{id:124463277,code:null ...
- JavaStuNote 5
接口 (interface) 一个抽象类,全部的方法都是抽象的,全部方法的public, 我们把这种类叫做极度抽象类,是最干瘪的类. public abstract class A { public ...
- LINUX 环境变量总结
1.概述 Linux是一个多用户的操作系统.多用户意味着每个用户登录系统后,都有自己专用的运行环境.而这个环境是由一组变量所定义,这组变量被称为环境变量.用户可以对自己的环境变量进行修改以达到对环境的 ...
- OpenCV学习:改变图像的对比度和亮度
本实例演示简单地改变图像的对比度和亮度,使用了如下线性变换来实现像素值的遍历操作: The parameters α > 0 and β often called the gain and bi ...
- 查看当前mysql数据库实例中,支持的字符集有哪些,或者是否支持某个特定字符集
需求描述: 查看当前mysql实例中支持哪些字符集,过滤特定的字符集 操作过程: 1.通过show character set来进行查看 mysql> show character set; + ...