Problem Description: Say you have an array for which theith element is the price of a given stock on dayi. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. Solution: 1publici...
同样不需要额外的空间,而且只需要O(n)的空间复杂度。思路就是我一开始没有钱,buy1是我第一次买股票之后剩的钱(肯定是负的,我白手起家,借钱买股票啊),sell1是我卖完第一次买的股票之后剩的钱(>=0),buy2是我用上回挣得钱(有可能需要找别人再借一点)买第二次股票,sell2是我卖完第二次股票之后剩下的钱。
用两个指针,buy表示第几天买入,sell表示第几天卖出开始buy,sell都指向0,表示不操作36729^b^ssell后移表示这天卖出,计算收益是6-3=336729^^bssell后移表示这天卖出,计算收益是7-3=436729^^bssell后移表示这天卖出,计算收益是2-3=-13672912^^bs此外,如上图,当前sell指向的价格小于了我们买入的价格,所以我们要...
If you haven't done the previous problem, Best TIme to Buy and Sell Stock I, I recommend going through that first to get a grasp on greedy algorithms. If you go through a couple of examples, regardless of a test case, you'll notice that as long as we buy and sell a stock that w...
122 Best Time to Buy and Sell Stock II 原题地址。这道题是在上题的基础上,然后不限制交易次数,但是每次交易不能同时进行,即买入后要卖出才能进行下一次买入的操作。 第一道题已经把基本的、通用的结果步骤写出来了,我解题的时候大概也是遵循这样的步骤来解,所以第二题就不详细啰嗦的写了。
121. Best Time to Buy and Sell Stock 121 这个比较简单,大致思想: 1.如果sale-buy<=0,则该价格适合买入。 2.否则判断该价格卖出的价格是否最好。 代码如下: publicintmaxProfit(int[]prices){if(prices==null||prices.length==0){return0;}intres=0;intbuy=prices[0];for(inti=1;i<prices.length;i...
When the market is up and you hold a stock that’s down it can play head games with you. The problem is there aren’t any surefire rules of thumb for when to sell a loser. There are lots of good books about when to buy a stock. You can use financial statement analysis, quantitativ...
If the Price of a Stock Plunges, Should I Sell or Buy More to Average Down? It depends. If a stock price plunges because of a significant and long-term change in the company's outlook, that's a good reason to sell. Virtually all stocks, even the bluest of theblue chips, experience...
In order to rate stocks as buy, sell, or hold, analysts research public financial statements, listen in on conference calls, and talk to managers and the customers of a company.
Read the full-text online article and more details about "When Should You Buy, Buy, Buy? When Insurers Sell, Sell, Sell; Revealed: Britain's Most Bizarre Stock Market Signal ... the Experts You Can Always Rely on to Get It Wrong" by Laurance, Ben - The Mail on Sunday (London, ...