:rtype: int"""res=0foriinrange(1,len(prices)): temp= prices[i] - prices[i-1]iftemp <=0:continueelse: res+=tempreturnres 参考: 下面的更快,因为索引查找的次数少一些! classSolution(object):defmaxProfit(self, prices):""":type prices: List[int] :rtype: int"""iflen(prices) ==0:...
Can you solve this real interview question? Best Time to Buy and Sell Stock - You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choo
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). 2...
输入:prices = [7,1,5,3,6,4] 输出:7 解释:在第2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4。随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6 - 3 = 3。最...
//#309Description: Best Time to Buy and Sell Stock with Cooldown | LeetCode OJ 解法1:用一个有限状态机来考虑,昨天今天、有股没股。这两者的组合和状态转换搞清楚了,题目就做出来了。 // Solution 1: Use a finite state machine to deal with the problem. Today or yesterday, got stocks in hand...
假设您有一个数组,其中第 i 个元素是给定股票在第 i 天的价格。 设计一个算法来找到最大的利润。 您可以根据需要完成任意数量的交易(即多次买入和卖出一股股票)。 但是,您不得同时进行多项交易(即,您必须先卖出股票,然后才能再次购买)。 所需:1积分电信网络下载...
Section 44: Facebook Dynamic Programming Question: Best Time To Buy And Sell Stocks (Easy) Lecture 191 Explanation behind the problem Lecture 192 Intuition behind the problem Lecture 193 Walkthrough over pseudocode Lecture 194 Optimal solution explanation Lecture 195 Coding the solution Section 45: Am...
每个题型只有几题,如palindrome、combination、randomization、buy and sell stocks、maze、wiggle sort等等。把它们打印出来,先后分题型和大类(array、matrix、linked list、geometry等)放好,贴上标签。而打印出来的好处是,可以在上面写写画画,留下做题时的思考过程和易错点,以便日后回顾。就算过了几年跳槽的时候也能...
1981.Minimize-the-Difference-Between-Target-and-Chosen-Elements (M+) 2291.Maximum-Profit-From-Trading-Stocks (M) 2518.Number-of-Great-Partitions (H-) 2585.Number-of-Ways-to-Earn-Points (M) 2902.Count-of-Sub-Multisets-With-Bounded-Sum (H) 3489.Zero-Array-Transformation-IV (H-) 键盘型 ...
0188 Best Time to Buy and Sell Stock IV 37.8% Hard 0189 Rotate Array Go 39.2% Medium 0190 Reverse Bits Go 51.9% Easy 0191 Number of 1 Bits Go 64.5% Easy 0192 Word Frequency 25.6% Medium 0193 Valid Phone Numbers 26.0% Easy 0194 Transpose File 25.3% Medium 0195 Tenth Line 32....