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

解题思路:由于可以进行无限次的交易,那么只要是递增序列,就可以进行利润的累加。

代码:


class Solution:
# @param prices, a list of integer
# @return an integer
def maxProfit(self, prices):
maxprofit = 0
for i in range(1, len(prices)):
if prices[i] >= prices[i-1]:
maxprofit += prices[i] - prices[i-1]
return maxprofit

时间: 2025-01-02 14:59:25

[leetcode]Best Time to Buy and Sell Stock II @ Python的相关文章

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

LeetCode Best Time to Buy and Sell Stock II

Best Time to Buy and Sell Stock II Total Accepted: 41127 Total Submissions: 108434 My Submissions Question Solution 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

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

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

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

[leetcode]Best Time to Buy and Sell Stock III @ Python

原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 题意: 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 at most two transa

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

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

LeetCode Best Time to Buy and Sell Stock II (简单题)

题意: 股票买卖第2题.给出每天的股票价格,每次最多买一股,可以多次操作,但是每次在买之前必须保证身上无股票.问最大的利润? 思路: 每天的股票价格可以看成是一条曲线,能卖掉就卖掉,那么肯定是在上升的时候就可以卖掉,但是在不卖的时候要保证自己身上的那只股票的价格是最低价买进的. 1 class Solution { 2 public: 3 int maxProfit(vector<int>& prices) 4 { 5 int pre=2147483647, ans=0; 6 for(