122. Best Time to Buy and Sell Stock II
来源:互联网 发布:linux下oracle自启动 编辑:程序博客网 时间:2024/05/15 11:40
题意
给你一个数组,数组的每一个元素是当天股价,计算最大收益 find the maximum profit,设计一个算法来找到最大的利润。 您可以根据需要完成尽可能多的交易(即,购买一次并多次出售股票)。 但是,您可能不会同时从事多个交易(即,您必须在再次购买之前先出售该股票)。
题解
贪心法则, 只要i+1天比i天股票涨了,那就进行一笔i天买入,i+1天卖出的交易,哪怕i+2天跌了,也无关最大收益的事,此题有五道题相关,后面会有DP问题,但这题是简答的贪心问题,因为可以多次交易,只要会涨,就抓住每一次获取收益的机会,就算得到maximum profit
// Javapublic class Solution { public int maxProfit(int[] prices) { int res = 0; for(int i = 0;i < prices.length -1;i ++) res += Math.max((prices[i+1] - prices[i]),0); return res; }}
0 0
- 122. Best Time to Buy and Sell Stock II
- LeetCode 122. Best Time to Buy and Sell Stock II
- 122.Best Time to Buy and Sell Stock II
- [Leetcode] 122. Best Time to Buy and Sell Stock II
- [leetcode] 122.Best Time to Buy and Sell Stock II
- 【leetcode】122.Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II
- 【LeetCode】122.Best Time to Buy and Sell Stock II
- [leetcode] 122. Best Time to Buy and Sell Stock II
- LeetCode #122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II LeetCode
- 122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II
- [LeetCode]122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II
- Leetcode 122. Best Time to Buy and Sell Stock II
- leetcode 122. Best Time to Buy and Sell Stock II
- C++项目和解决方案的区别
- JAVA FinalReference
- 蓝桥杯-2的次幂表示(java)
- centos中安装nginx
- 蓝桥杯 基础练习 回文数
- 122. Best Time to Buy and Sell Stock II
- 堆栈
- 原理调查之jdk配置原理
- 谁是凶手:
- 烟雨徽州行
- 如何用JQuery向select标签中添加option
- 采购单(京东2017秋招真题)
- 广告精准推送项目介绍
- 记录一下微信支付h5支付fail_no permission to execute