LeetCode: Best Time to Buy and Sell Stock II 解题报告
Best Time to Buy and Sell Stock II
Question Solution
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) {
if (prices == null) {
return 0;
}
int maxProfit = 0;
int len = prices.length;
for (int i = 1; i < len; i++) {
int dif = prices[i] - prices[i - 1];
if (dif > 0) {
maxProfit += dif;
}
}
return maxProfit;
}
}
LeetCode: Best Time to Buy and Sell Stock II 解题报告的更多相关文章
- LeetCode: Best Time to Buy and Sell Stock III 解题报告
Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...
- 【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 ...
- 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 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【LeetCode】188. Best Time to Buy and Sell Stock IV 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
随机推荐
- google开发新人入职100天,聊聊自己的经验&教训 个人对编程和开发的理解 技术发展路线
新人入职100天,聊聊自己的经验&教训 这篇文章讲了什么? 如题,本屌入职100天之后的经验和教训,具体包含: 对开发的一点感悟. 对如何提问的一点见解. 对Google开发流程的吐槽. 如果 ...
- WebDriver基本操作入门及UI自动化练手页面
在这里集中了我们在做UI自动化时常见的一些控件操作.希望能对新手有帮助. 下载地址:http://files.cnblogs.com/zhangfei/demo.rar package com.tes ...
- STC单片机串口输出ADXL335角度值
STC单片机串口输出ADXL335角度值: //***************************************************** //名称:单片机串口输出ADXL335角度值 ...
- 树莓派进阶之路 (003) - Raspberry Pi(树莓派)国内软件源
树莓派自带的软件源是 deb http://mirrordirector.raspbian.org/raspbian/ wheezy main contrib non-free rpi 由于网站在国外 ...
- Python学习笔记(四)——编码和字符串
一.编码 1.编码类别: (1)ASCII码:127个字母被编码到计算机里,也就是大小写英文字母.数字和一些符号 (2)GB2312码:中国制定的用于加入中文汉字的编码 (3)Unicode:防止由于 ...
- ML之回归
一.概述 一元形式: 多元形式: 最小二乘的目标函数
- sql中union 和 union all的区别
最近发现一个视图出奇的慢,在生产环境还好,由于服务器配置较高,没有察觉出来.但是做了一次修改后在开发版 和测试版就直接查询不出结果了.就连select count(1) from 都运行2个小时没有结 ...
- easyui-linkbutton 设置和获取text文本
<a id="butTransagt" href="#" class="easyui-linkbutton" icon=" ...
- SharePoint 2013怎样创建Wiki库
们使用Wiki页面来分享知识,增进团队合作.下面我将向大家展示SharePoint 2013 Wiki的使用方法.教程我都将以这张Wiki页面(即当前页)为示例. 编辑页面 如要编辑页面,单击顶部Ed ...
- ICDAR2015 数据处理及训练
训练数据处理: 天池ICPR2018和MSRA_TD500两个数据集: 1)天池ICPR的数据集为网络图像,都是一些淘宝商家上传到淘宝的一些商品介绍图像,其标签方式参考了ICDAR2015的数据标签格 ...