zoukankan      html  css  js  c++  java
  • 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 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).

    解题思路:

    贪心求最大值

    1. class Solution {  
    2. public:  
    3.     int maxProfit(vector<int>& prices) {  
    4.         if(prices.size()==0||prices.size()==1) return 0;  
    5.           
    6.         int max_profit=0;  
    7.           
    8.         for(int i=0;i<prices.size()-1;i++){  
    9.             if(prices[i+1]-prices[i]>0) max_profit+=(prices[i+1]-prices[i]);        
    10.         }  
    11.         return max_profit;  
    12.           
    13.     }  
    14. };  
  • 相关阅读:
    MySQL(错误1064)
    如何判断是手机还是电脑访问网站
    Oracle表分区
    分离Date数据
    多对多
    一对多
    SQLalchemy基础
    paramiko上传下载
    paramiko
    automap
  • 原文地址:https://www.cnblogs.com/liangyc/p/8819903.html
Copyright © 2011-2022 走看看