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).

Hide Tags

Array Greedy

 

#include <iostream>
#include <vector>
using namespace std; class Solution {
public:
int maxProfit(vector<int> &prices) {
int n=prices.size();
if(n<) return ;
int sum=;
for(int i =;i<n;i++){
if(prices[i]>prices[i-]){
sum += prices[i] - prices[i-];
}
}
return sum;
}
}; int main()
{
vector<int > prices{,,,,};
Solution sol;
cout<<sol.maxProfit(prices)<<endl;
return ;
}

[LeetCode] Best Time to Buy and Sell Stock II 贪心算法的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. [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 ...

  5. 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 ...

  6. [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 ...

  7. [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 ...

  8. LeetCode: Best Time to Buy and Sell Stock II [122]

    [题目] Say you have an array for which the ith element is the price of a given stock on day i. Design ...

  9. LeetCode——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. ...

随机推荐

  1. Laravel — homestead 配置多站点

    一.homestead.yaml 配置 homestead.yaml 文件配置sites,如下 sites: - map: homestead.test to: /home/vagrant/Code/ ...

  2. Python3爬取起猫眼电影实时票房信息,解决文字反爬~~~附源代码

    上文解决了起点中文网部分数字反爬的信息,详细链接https://www.cnblogs.com/aby321/p/10214123.html 本文研究另一种文字反爬的机制——猫眼电影实时票房反爬 虽然 ...

  3. (WPF&Silverlight)silverlight自定义控件

    2个半小时弄懂了自定义控件是怎么回事儿. 在silverlight中创建一个UserControl,把上面sliderbar的外观和功能都封装在里面. 以自定义控件mapslider控件为例: 1.首 ...

  4. HBase(0.94.5)的Compact和Split源码分析

    经过对比,0.94.5以后版本主要过程基本类似(有些新功能和细节增加) 一.       Compact 2.1.   Compact主要来源 来自四个方面:1.Memstoreflush时:2.HR ...

  5. WebView的初体验

    使用安卓自带控件可以实现不通过浏览器即可上网的功能 突然就觉得安卓好强大,是不是我太无知了,太容易满足了 1.在layout中添加VebView控件 2.在Activity中设置WebView的属性 ...

  6. TCP/IP网络编程之优于select的epoll(一)

    epoll的理解及应用 select复用方法由来已久,因此,利用该技术后,无论如何优化程序性能也无法同时接入上百个客户端.这种select方式并不适合以web服务端开发为主流的现代开发环境,所以要学习 ...

  7. 在Foxmail中添加阿里云企业邮箱账号

    1.安装完成Foxmail之后,新建账号 输入阿里云邮箱地址和密码,点击创建 接受服务器类型你可以选择POP3或者IMAP,在这里我选择的是POP3 点击创建,大功告成. 为什么要写这篇文章呢? 因为 ...

  8. leetcode 【 Plus One 】python 实现

    题目: Given a non-negative number represented as an array of digits, plus one to the number. The digit ...

  9. SQL语句Not IN优化方案

    总结网友们在CSDN社区上对于not in的优化策略,整理如下,备查.    select * from emp where emp_no not in (select emp_no from emp ...

  10. IOS开发学习笔记023-UIToolBar的使用

    这里使用代码实现 大概过程: 1.创建工具条 2.创建插入条 3.添加头像.标签.删除按钮 4.点击头像获取标签信息 做一个简单的联系人列表,可以添加删除联系人,现在还没有添加头像和文字,接下来慢慢添 ...