leetcode — best-time-to-buy-and-sell-stock-ii
/**
* Source : https://oj.leetcode.com/problems/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 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 BestTimeToBuyAndSellStock2 {
/**
* 找出最大可能的收益
* 可以交易多次,但是同一时间手中只能有一只股票
* 只要第二天的价格高于第一天,那么就可以在第一天买入,第二天卖出,这样就能保证每次赚钱,而且能赚到整个时间段内所有可能的利润
*
* @param prices
* @return
*/
public int maxProfit (int[] prices) {
int result = 0;
for (int i = 1; i < prices.length; i++) {
result += prices[i] > prices[i-1] ? prices[i] - prices[i-1] : 0;
}
return result;
}
public static void main(String[] args) {
BestTimeToBuyAndSellStock2 bestTimeToBuyAndSellStock2 = new BestTimeToBuyAndSellStock2();
System.out.println(bestTimeToBuyAndSellStock2.maxProfit(new int[]{1,2,3,4,5}));
System.out.println(bestTimeToBuyAndSellStock2.maxProfit(new int[]{-2,1,3,5,-9,1,2}));
}
}
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
Description: Say you have an array for which the ith element is the price of a given stock on day i. ...
- [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 ...
随机推荐
- 一个陌生女人的来信 Brief einer Unbekannten
[Österreich] Stefan Zweig Mein Kind ist gestorben, unser Kind - jetzt habe ich niemanden mehr in der ...
- Django——小结
课程介绍 MVC MVC框架的核心思想是:解耦,让不同的代码块之间降低耦合,增强代码的可扩展性和可移植性,实现向后兼容 M:Model,主要封装对数据库层的访问,对数据库中的数据进行增.删.改.查操作 ...
- Hive参数的临时设置和永久性设置
Hive中有一些参数是系统给提供给用户的,我们可以通过这些参数的设置可以让Hive在不同的模式下工作,或者改变显示的效果. 1.通过set对参数值进行设定,这种设置只能是在本次会话有效,退出Hive就 ...
- C# 计算地图上某个坐标点的到多边形各边的距离
在判断了某个坐标点是否在多边形内后,还有另一个需求就是当我这个坐标点在多边形外部时,我需要计算这个坐标点到多边形的距离是否在一个允许的误差范围内 通过两个位置的经纬度坐标计算距离(C#版本) 转自:h ...
- Win 10 Revit 2019 安装过程,亲自踩的一遍坑,有你想要的细节
首先就是安装吖,不管是管理员权限还是普通权限,都是以下这个问题,跟权限没关系 failed to load .....revitcontentpackui.dll (126) 尝试了网上能查到的各种方 ...
- 180815 Python自学成才001
1.为什么学习Python? Python:脚本语言,易入门,可移植. Python适用范围:web开发.自动化测试工具编写. 适用岗位:运维开发(运维).自动化测试(软件测试).Python开发(软 ...
- SpringBoot报错:nested exception is org.apache.ibatis.executor.ExecutorException: No constructor found in com.tuyrk.test.User matching [java.lang.Long, java.lang.String, java.lang.String]
错误提示: Caused by: org.apache.ibatis.executor.ExecutorException: No constructor found in com.tuyrk._16 ...
- ActiveMQ队列、主题模式区别
1.ActiveMQ队列模式如下图,生产者创建消息到消息中间件,再“均分给消费者”. 2.ActiveMQ主题模式如下图,生产者创建消息到消息中间件,消费者会接受到订阅的主题中所有的消息.在主题模式下 ...
- pygame学习
http://eyehere.net/2011/python-pygame-novice-professional-3/ http://www.pygame.org/docs/ref/event.ht ...
- vue中计算属性computed方法内传参
vue中computed计算属性无法直接进行传参 如果有传参数的需求比如说做数据筛选功能可以使用闭包函数(也叫匿名函数)实现 例如: 在上篇博客vue安装使用最后的成绩表练习中的过滤功能的实现: &l ...