Best Time to Buy and Sell Stock II [LeetCode]
Problem Description: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
Basic idea: from code below, it seems super easy. But intuitively, we may use one more variable "tmp_max_profit" to record every times we get the max profit.
class Solution {
public:
int maxProfit(vector<int> &prices) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int max_profit = ;
for(int i = ; i < prices.size(); i ++) {
if(i + >= prices.size())
break;
if(prices[i + ] > prices[i])
max_profit += prices[i + ] - prices[i];
}
return max_profit;
}
};
Best Time to Buy and Sell Stock II [LeetCode]的更多相关文章
- Best Time to Buy and Sell Stock II ——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Best Time to Buy and Sell Stock II leetcode java
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 122. Best Time to Buy and Sell Stock II ——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LintCode] 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 ...
- 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...
- Leetcode-122 Best Time to Buy and Sell Stock II
#122 Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the pric ...
- 【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 ...
- 31. leetcode 122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
随机推荐
- mongodb .net driver
1.介绍 The official MongoDB .NET Driver provides asynchronous interaction with MongoDB. Powering the d ...
- How To PLAY_SOUND in Oracle Forms
Play_sound is used to play audio files in Oracle Forms, Play_Sound plays the sound object in the spe ...
- wooyunAPI
经常要爬去乌云的信息,但是每次都是硬爬,写完了发现乌云有提供API的,整理给大家: 1. WooYun Api是什么 通过WooYun开放的Api接口,其它网站或应用可以根据自己获取的权限调用WooY ...
- hdu 2196 Computer 树的直径
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- data-*属性——使用自定义属性的方式存储数据
HTML5提供了data-*属性能存储页面或应用程序的私有自定义数据.只需在属性前加上data-前缀即可,值可以是任意字符串. 存储的(自定义)数据能够被页面的 JavaScript 中利用,以创建更 ...
- Eclipse使用Jetty(转)
eclipse 与 jetty 结合的最佳实践 http://www.cnblogs.com/mignet/archive/2011/12/04/eclipse_jetty_perfect_integ ...
- iOS - OC NSRect 位置和尺寸
前言 结构体 这个结构体用来表示事物的坐标点和宽高度. typedef CGRect NSRect; struct CGRect { CGPoint origin; CGSize size; }; t ...
- poj3667【线段树】/【类似权值线段树写法】
题意:n个空房间.两种操作:1.选择最小的连续D个房间入住,并输出这连续D个房间的最小标号.2.将某个区间内的房间全部退房. #include <cstdio> #include < ...
- (转)Web自动化测试中的接口测试
1.背景 1.1 Web程序中的接口 1.1.1 典型的Web设计架构 web是实现了基于网络通信的浏览器客户端与远程服务器进行交互的应用,通常包括两部分:web服务器和web客户端.web客户端的应 ...
- linux清空文件等有用的指令
1). > filename 2). :> filename 3). echo "" > filename (文件大小被截为1字节) 4). ...