【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 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).
解题:简单贪心,如果今天买,明天涨就不卖,如果明天跌就卖掉,如果后天涨明天就又买进来。
代码:
class Solution {
public:
int maxProfit(vector<int> &prices) {
int answer = ;
if(prices.size() == )
return ;
for(int i = ;i < prices.size()-;i++)
{
if(prices[i+]>prices[i])
answer += prices[i+]-prices[i];
}
return answer;
}
};
我还问了这个问题:http://oj.leetcode.com/discuss/4082/why-do-i-have-to-add-if-prices-size-0-return-0
Java版本:
public class Solution {
public int maxProfit(int[] prices) {
int sum = 0;
for(int i = 0;i < prices.length-1;i++){
if(prices[i+1] > prices[i])
sum += prices[i+1]-prices[i];
}
return sum;
}
}
【leetcode刷题笔记】Best Time to Buy and Sell Stock II的更多相关文章
- 【LEETCODE】37、122题,Best Time to Buy and Sell Stock II
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 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 an ...
- LeetCode Array Easy 122. 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&Python] Problem 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 al ...
- 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 ...
- 122. Best Time to Buy and Sell Stock II@python
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- leetcode刷题笔记
(1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...
- 【刷题-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之“动态规划”:Best Time to Buy and Sell Stock I && II && III && IV
Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the ...
随机推荐
- zxing学习笔记 android入门
对于刚开始学习android开发的童鞋们来说,若有一个简单而又全面的android工程能来剖析,那就是再好不过了,zxing就是不错得例子. zxing的源码可以到google code上下载, ...
- PHP面试题及答案解析(2)—PHP面向对象
1. 写出 php 的 public.protected.private 三种访问控制模式的区别. public:公有,任何地方都可以访问protected:继承,只能在本类或子类中访问,在其它地方不 ...
- Mongodb基本操作入门,增删改查和索引
主要进程 mongod.exe为启动数据库实例的进程. mongo是一个与mongod进程进行交互的JavaScript shell进程,它提供了一些交互的接口函数用户对数据库的管理. 基本命令 sh ...
- python常见面试题(二)
1. 到底什么是Python?你可以在回答中与其他技术进行对比(也鼓励这样做). 下面是一些关键点: Python是一种解释型语言.这就是说,与C语言和C的衍生语言不同,Python代码在运行之前不需 ...
- Hibernate二次学习一----------Hibernate简单搭建
因为博客园自带的markdown不太好用,因此所有markdown笔记都使用cmd_markdown发布 Hibernate二次学习一----------Hibernate简单搭建: https:// ...
- IPython introduction
转载:http://blog.csdn.net/gavin_john/article/details/53086766 1. IPython介绍 ipython是一个python的交互式shell,比 ...
- OpenGL/GLSL数据传递小记(3.x)(转)
OpenGL/GLSL规范在不断演进着,我们渐渐走进可编程管道的时代的同时,崭新的功能接口也让我们有点缭乱的感觉.本文再次从OpenGL和GLSL之间数据的传递这一点,记录和介绍基于OpenGL3.x ...
- 系统服务-----NotificationManager
熟悉api事例笔记: package com.test; import com.example.test.R; import android.app.Activity; import android. ...
- Swing实现系统托盘
/* 实现系统托盘化 */ protected void setToolSystemTray() { // 系统是否支持系统托盘 if (SystemTray.isSupported()) { // ...
- Spring在注入bean异常时的替换
情形:需要把数据库1的连接池A注入到DAO中,但是如果数据库A的连接池实例化失败了整个项目也启动不了,这个时候希望用数据库2的连接池来替换. 这里没有想到什么好的解决方法,只是想到了工厂方法提供Bea ...