LeetCode122: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).
解题思路:
这题与上一题Best Time to Buy and Sell Stock相比,其实还更简单,扫描一遍数组,当前前元素大于前一个元素,则将其差值累加到max中,最后求得的max即为最大利益。
代码:
- #include <iostream>
- #include <vector>
- using namespace std;
- /**
- 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) {
- if(prices.empty())
- return 0;
- int max = 0;
- for(int i = 1; i < prices.size(); i++)
- {
- if(prices[i] > prices[i-1])
- max += prices[i] - prices[i-1];
- }
- return max;
- }
- };
- int main(void)
- {
- int arr[] = {2,4,5,1,7,10};
- int n = sizeof(arr) / sizeof(arr[0]);
- vector<int> stock(arr, arr+n);
- Solution solution;
- int max = solution.maxProfit(stock);
- cout<<max<<endl;
- return 0;
- return 0;
- }
LeetCode122: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 Say you have an array for which the ith element is the pric ...
- LeetCode122——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 ...
- [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】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 ...
- LeetCode: Best Time to Buy and Sell Stock II 解题报告
Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...
- 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 ...
随机推荐
- 好脑袋不如烂笔头-Quartz使用总结
Quartz是Java平台的一个开源的作业调度框架.Quartz.net是从java版本移植到.net版本的..net项目使用Quartz来执行批处理等定时任务非常方便. (1)从nuget上可以安装 ...
- Oracle Redo 以及 Archived日志简述
Oracle通过Redo Archived实现数据的归档 什么是Redo日志 Redo日志记录了数据的变更,用于在数据库出现故障后,进行数据恢复. 功能主要由三个组件实现:Redo Log Buffe ...
- Atitit (Sketch Filter)素描滤镜的实现 图像处理 attilax总结
Atitit (Sketch Filter)素描滤镜的实现 图像处理 attilax总结 1.1. 素描滤镜的实现方法比较简单,这里我们直接写出算法过程如下:1 1.2. 颜色减淡COLOR_DO ...
- KnockoutJS 3.X API 第四章 表单绑定(12) selectedOptions、uniqueName绑定
selectedOptions绑定目的 selectedOptions绑定控制当前选择多选列表中的哪些元素. 这旨在与<select>元素和选项绑定结合使用. 当用户选择或取消选择多选列表 ...
- CSS 框模型( Box module )
框和布局 在 KB005: CSS 层叠 中已经介绍了 CSS 的重要之处.CSS 可以说是页面表现的基础, CSS 可以控制布局,控制元素的渲染. 布局是讲在电影画面构图中,对环境的布置.人物地位的 ...
- Sublime Text2 Jsformat自定义使用之代码折叠方式修改
将代码括号的折叠方式从 function abc(){ } 变成 function abc() { } 打开 Setting-user,把setting-default里的文本全部复制过来. 然后 将 ...
- OpenCascade Modeling Algorithms Fillets and Chamfers
Modeling Algorithms Fillets and Chamfers 造型算法——倒圆与倒角 eryar@163.com 一.倒圆Fillet Constructor 1. BRepFil ...
- 通过js获得选择文件的绝对路径
<form name="thisform" method="get" action="test.jsp" id="thisf ...
- poj 1950 Dessert(dfs枚举,模拟运算过程)
/* 这个代码运行的时间长主要是因为每次枚举之后都要重新计算一下和的值! 如果要快的话,应该在dfs,也就是枚举的过程中计算出前边的数值(这种方法见第二个代码),直到最后,这样不必每一次枚举都要从头再 ...
- poj 2492A Bug's Life(并查集)
/* 目大意:输入一个数t,表示测试组数.然后每组第一行两个数字n,m,n表示有n只昆虫,编号从1—n,m表示下面要输入m行交配情况,每行两个整数,表示这两个编号的昆虫为异性,要交配. 要求统计交配过 ...