leetcode 122-Best Time to Buy and Sell Stock II

来源:互联网 发布:windows phone微信 编辑:程序博客网 时间:2024/06/06 02:03

难度:easy

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

审题:此题是list的第i个元素表示当天的股票价格,此题中,股票可以多次买出买进,可以当天买进再卖出,但是必须保证盈利。

           1.手中只能是一只股票,必须先买入再卖出,卖出以后方能再买入。

           2.只要后一位比前一位小,price[i+1]>price[i],即可实现买入再卖出

           3. 所有的买入和卖出差就是总利润。

思路:用while或者for循环皆可实现。



原创粉丝点击