Best Time to Buy and Sell Stock II 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. You may complete as many transactions as you like (ie, buy one and sell one share of the s...
length<2){ return 0; } int sumProfit = 0; int current = prices[0]; for (int price:prices){ if (price>current){ sumProfit+=price-current; } current = price; } return sumProfit; } } 题目信息 You are given an integer array prices where prices[i] is the price of a given stock...
Runtime: 44 ms, faster than 67.87% of Python3 online submissions for Best Time to Buy and Sell Stock II. Memory Usage: 13.9 MB, less than 5.06% of Python3 online submissions for Best Time to Buy and Sell Stock II. 改进版本2: 可以看到这样写大大简化了代码量,但是性能方面并没有提升多少...
9 8 7 3 2 但是对于我们的代码,持续下跌的话,buy 和 sell - 1 就相等了,所以每次累计就是 0,不影响结果 解法二 其实不用考虑那么多,再直接点,只要当前天相对于前一天上涨了,我们就前一天买入,当前天卖出。 public int maxProfit(int[] prices) { int profit = 0; for (int i = 1; i < prices....
122 Best Time to Buy and Sell Stock II 原题地址。这道题是在上题的基础上,然后不限制交易次数,但是每次交易不能同时进行,即买入后要卖出才能进行下一次买入的操作。 第一道题已经把基本的、通用的结果步骤写出来了,我解题的时候大概也是遵循这样的步骤来解,所以第二题就不详细啰嗦的写了。
Leetcode Best Time to Buy and Sell Stock II Say you have an array for which theith element is the price of a given stock on dayi. 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 ...
本题跟之前那道Best Time to Buy and Sell Stock 最佳的买卖股票时间很类似,但都比较容易。这道题由于可以无限次买入和卖出。我们都知道炒股想挣钱当然是低价买入高价抛出,那么这里我们只需要从第二天开始,如果当前价格比之前价格高,则把差值加入利润中,因为我们可以昨天买入,今日卖出,若明日价更高的话,还...
Best Time to Buy and Sell Stock II Say you have an array for which theith element is the price of a given stock on dayi. 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)...
Google – We got Act I Right, Now what about Act II? November 5, 2012 Expert stock picks - who is doing best YTD? August 1, 2012 When Stock Research and Big Macs Collide July 27, 2012 Crafting a Chaotic Stock Portfolio July 23, 2012 Why You Should Buy Google In Advance Of Earnings...
be several weeks after the terms of the trade were agreed upon between the buyer and seller. In case of multiple trades closed on the same date, the price displayed is of either (i) the trade which terms were agreed upon most recently; or (ii) the trade with the higher notional value...