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. };  
  • 相关阅读:
    函数参数
    字符编码
    本周内容
    int,float,str,list,dict,元组
    python 基础变量
    Python学习(小笔记一)
    🌐 网络管理
    📓 LVM相关
    📹 进程管理(二)
    🎬进程管理
  • 原文地址:https://www.cnblogs.com/liangyc/p/8819903.html
Copyright © 2011-2022 走看看