LeetCode OJ 122. 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).
【思路】
相比较上一个题目,这个题目对交易放松了限制,可以做多笔交易,但是在进行下一笔交易之前必须先完成上一笔交易,而且相同交易只能做一次。我的思路是:如果价格在未来上涨,那么我就在当前买入。如果价格在未来下跌,我就在当前卖出。这样的结果就是把数组划分成了一段段的递增序列,在序列的最开始买入,最后卖出。举个例子:[2,1,25,4,5,6,7,2,4,5,1,5]。数组被划分成了5个递增序列,在第一个序列利润为0,第二个序列利润为24,第三个为3,第四个为3,第五个为4.总的利润是34。代码如下:
public class Solution {
public int maxProfit(int[] prices) {
int min = 0;
int sump = 0;
int mp = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] < prices[i-1]){
sump = sump + mp;
min = i;
mp = 0;
}
else
mp = prices[i] - prices[min];
}
return sump + mp;
}
}
其实更加简单直观的代码如下:
public class Solution {
public int maxProfit(int[] prices) {
int total = 0;
for(int i = 0; i < prices.length-1; i++){
if(prices[i+1] > prices[i]) total += prices[i+1] - prices[i];
}
return total;
}
}
最后要把所有递增序列中的最大值和最小值的差值加起来,其实这个过程和上述代码的描述是相同的。
LeetCode OJ 122. Best Time to Buy and Sell Stock II的更多相关文章
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- leetcode:122. Best Time to Buy and Sell Stock II(java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...
- 【刷题-LeetCode】122 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 ...
- 【LeetCode OJ】Best Time to Buy and Sell Stock II
Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ We solve this prob ...
- 【LeetCode】122.Best Time to Buy and Sell Stock II 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】122. 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 a ...
- LeetCode OJ:Best Time to Buy and Sell Stock II(股票买入卖出最佳实际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 No.122 Best Time to Buy and Sell Stock II Easy(c++实现)
1. 题目 1.1 英文题目 You are given an array prices where prices[i] is the price of a given stock on the it ...
- 【leetcode】122.Best Time to Buy and Sell Stock II(股票问题)
You are given an integer array prices where prices[i] is the price of a given stock on the ith day. ...
随机推荐
- [MFC美化] SkinMagic使用详解1- SkinMagic使用流程
[SkinMagic使用流程] 1.工程配置SkinMagic相关文件 2.初始化SkinMagic皮肤文件,窗体加载皮肤 3.释放皮肤资源 特别声明,SkinMagic要是破解版的,如果不是,可能需 ...
- Spring MVC中页面向后台传值的几种方式
在学习 Spring Mvc 过程中,有必要来先了解几个关键参数: @Controller: 在类上注解,则此类将编程一个控制器,在项目启动 Spring 将自动扫描此类,并进行对 ...
- Openjudge-NOI题库-数根
题目描述 Description 数根可以通过把一个数的各个位上的数字加起来得到.如果得到的数是一位数,那么这个数就是数根.如果结果是两位数或者包括更多位的数字,那么再把这些数字加起来.如此进行下去, ...
- centos7配置开启无线网卡,重启防火墙
centos7配置无线网卡: 在虚拟机为nat的网络连接下(就是默认的那个),centos7默认网卡未激活. 可以设置 文件 /etc/sysconfig/network-scripts/ifcfg- ...
- ntopng源码分析
参数初始化以及ntop主流程启动 #ifndef WIN32 ) && (argv[][] != '-')) rc = prefs->loadFromFile(argv[]); ...
- Redis 数据序列化方法 serialize, msgpack, json, hprose 比较
最近弄 Redis ,涉及数据序列化存储的问题,对比了:JSON, Serialize, Msgpack, Hprose 四种方式 1. 对序列化后的字符串长度对比: 测试代码: $arr = [0, ...
- CSS Hank兼容浏览器的
color:red; /* 所有浏览器都支持 */ color:red !important; /* 除IE6外 */ _color:red; /* IE6支持 */ *color:red; /* I ...
- JS 一些常用技巧
记录 JS 常用的技巧 1. 生成随机数 2. 解决浮点数问题 3. 无路可走时,看看是不是 事件 冒泡了...
- [Q]pdfFactory打印机纸张方向设置为横向
不推荐更改pdfFactory打印机默认纸张方向(默认为横向),更改后可能导致不必要的麻烦(pdfFactory要求所定义的纸张方向与实际的纸张方向需一致,因此若更改为横向,则纸张宽度的定义需大于纸张 ...
- 初始化git
git config --global user.name "Firstname Lastname" git config --global user.email "yo ...