然后sell往后移, 若prices[sell] < prices[buy],则将buy指向sell当前位置(buy = sell),否则计算当前股票买卖收益,并和之前计算的收益比较,取最大的值。 1//双指针法求股票买卖最佳时机问题23intmaxProfit_doublePtr(vector<int>&prices)4{5intbuy=0,sell=0;6intmaxProfit =0;7for(;sell<prices.size();++...
代码 package day_33;publicclassBuySellStock{publicintmaxProfit(int[]prices){intprofit=0;intbuyPrice=Integer.MAX_VALUE;for(inti=0;i<prices.length;i++){if(prices[i]<buyPrice){buyPrice=prices[i];}else{profit=Math.max(profit,prices[i]-buyPrice);}}returnprofit;}publicstaticvoidmain(Stringargs...
buy = Math.max(sell - prices[i], pre_buy); sell = Math.max(pre_buy + prices[i], sell); } return sell; } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 第三题:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/?tab=Description 在第...
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...
找第j天prices[buy] - dp[buy][k - 1]的最小值的时候,我们考虑了prices[0] - dp[0][k - 1]、prices[1] - dp[1][k - 1]、prices[2] - dp[2][k - 1]...,找第j + 1天prices[buy] - dp[buy][k - 1]的最小值的时候,我们又会从头考虑prices[0] - dp[0][k - 1]、prices[...
122. 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 (i.e., buy one and sell one share of the stock ...
LeetCode121. Best Time to Buy and Sell Stock JonesM https://leetcode.com/problems/best-time-to-buy-and-sell-stock/leetcode.com/problems/best-time-to-buy-and-sell-stock/ 解题思路 1. 暴力 O(n2) 复杂度,超时 class Solution { public: int maxProfit(vector<int>& prices) { int maxVal...
Browse Sneakers and buy or sell at market prices on StockX, the live marketplace for StockX Verified Sneakers
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.
Here are some ideas to help you identify the right time to either buy or sell a stock for your portfolio.