leetcode Best Time to Buy and Sell Stock II
来源:互联网 发布:php动态网页设计 编辑:程序博客网 时间:2024/06/05 11:03
Best Time to Buy and Sell Stock II
Total Accepted: 4391 Total Submissions: 12451My SubmissionsSay 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).
We should buy the stock at the starting point of ascending status, and sell the stock at the ending point of ascending status. The code is :
class Solution {public: int maxProfit(vector<int> &prices) { // Note: The Solution object is instantiated only once and is reused by each test case. int minP,i,j,res=0,buy,sell; if(prices.size()==0) return 0; else{ buy=prices[0]; for(i=0;i<prices.size();i++){ if( i+1<prices.size()&&prices[i+1]<prices[i] ){ res+=prices[i]-buy; buy=prices[i+1]; } } res+=prices[prices.size()-1]-buy; } return res; }};
- [LeetCode]Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock II
- Leetcode: Best Time to Buy and Sell Stock II
- [Leetcode] Best Time to Buy and Sell Stock II
- LeetCode : Best Time to Buy and Sell Stock II
- LeetCode Best Time to Buy and Sell Stock II
- [LeetCode] Best Time to Buy and Sell Stock II
- leetcode 61: Best Time to Buy and Sell Stock II
- [LeetCode] Best time to buy and sell a stock II
- 【leetcode】Best Time to Buy and Sell Stock II
- LeetCode - Best Time to Buy and Sell Stock II
- LeetCode之Best Time to Buy and Sell Stock II
- [LeetCode]Best Time to Buy and Sell Stock II
- [Leetcode]Best Time to Buy and Sell Stock II
- [leetcode]Best Time to Buy and Sell Stock II
- [LeetCode] Best Time to Buy and Sell Stock II
- LeetCode-Best Time to Buy and Sell Stock II
- [leetcode] Best Time to Buy and Sell Stock II
- 最小生成树 -- Prim算法
- 基于TI8168平台的16路D1分辨率的DVR方案
- Apache Maven Fundamentals (part 1 of 4)
- esui控件validatebox 通过正则判断输入 json传值
- linux系统下库函数和系统调用都对文件的基本操作
- leetcode Best Time to Buy and Sell Stock II
- android编译模块
- 让工作更轻松——EA类图与代码同步
- 机制与策略
- java 实现图片的任意拖动
- 30天自制OS笔记(一)前10天内容中遇到的问题
- WCS学习笔记(第八天) - 创建entity bean实现
- F28335_DMA使用
- cocos2d-x 使用 CCScale9Sprite 实现微信对话框