关键词:
题目1:Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.
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.
分析:public class Solution { public int maxProfit(int[] prices) { int size = prices.length; if (size == 0){ return 0; } int maxPrice = prices[size-1];//初始化最大price int maxMoney = 0;//初始化利润值 for (int i=size-1; i>=0; --i){ maxPrice = maxPrice > prices[i] ? maxPrice : prices[i];//假设第i天的值大于最大price,则更新最大price的值 maxMoney = maxMoney > (maxPrice - prices[i]) ?maxMoney : (maxPrice - prices[i]);//更新最大利润值 } return maxMoney; } }
题目2: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).
AC代码:
public class Solution { public int maxProfit(int[] prices) { int profit = 0; int size = prices.length; if (size < 2){ return profit; } for (int index=1; index<size; ++index){ int value = prices[index] - prices[index-1]; if (value > 0){ profit += value; } } return profit; } }
题目3:
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 transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
用right[i]来表示[i,...,n-1]上的最大利润
public class Solution { public int maxProfit(int[] prices) { int size = prices.length; if (size < 2) return 0; int[] left = new int[size]; int[] right = new int[size]; int minValue = prices[0]; int maxValue = prices[size-1]; for (int i=1; i<size; ++i){ left[i] = left[i-1] > (prices[i] - minValue) ? left[i-1] : (prices[i] - minValue); minValue = minValue < prices[i] ? minValue : prices[i]; } for (int i=size-2; i>=0; --i){ right[i] = right[i+1] > (maxValue - prices[i]) ? right[i+1] : (maxValue - prices[i]); maxValue = maxValue > prices[i] ? maxValue : prices[i]; } int profit=0; for (int i=0; i<size; ++i){ profit = profit > (left[i] + right[i]) ?profit : (left[i] + right[i]); } return profit; } }
121.besttimetobuyandsellstock(代码片段)
121.BestTimetoBuyandSellStock0.参考文献序号文献1[LeetCode:BestTimetoBuyandSellStockIIIIII]1.题目Sayyouhaveanarrayforwhichtheithelementisthepriceofagivenstockondayi.Ifyouwereonlypermittedtocompleteatmostonetrans 查看详情
besttimetobuyandsellstock
Sayyouhaveanarrayforwhichthe ith elementisthepriceofagivenstockonday i.Ifyouwereonlypermittedtocompleteatmostonetransaction(ie,buyoneandselloneshareofthestock),designanalgorithmtofindth 查看详情
121.besttimetobuyandsellstock
Sayyouhaveanarrayforwhichtheithelementisthepriceofagivenstockondayi.Ifyouwereonlypermittedtocompleteatmostonetransaction(ie,buyoneandselloneshareofthestock),designanalgorithmtofindthemaximumprofit.Exa 查看详情
121.besttimetobuyandsellstock
Sayyouhaveanarrayforwhichthe ith elementisthepriceofagivenstockonday i.Ifyouwereonlypermittedtocompleteatmostonetransaction(ie,buyoneandselloneshareofthestock),designanalgorithmtofindth 查看详情
121.besttimetobuyandsellstock
Sayyouhaveanarrayforwhichthe ith elementisthepriceofagivenstockonday i.Ifyouwereonlypermittedtocompleteatmostonetransaction(ie,buyoneandselloneshareofthestock),designanalgorithmtofindth 查看详情
121.besttimetobuyandsellstock
Sayyouhaveanarrayforwhichthe ith elementisthepriceofagivenstockonday i.Ifyouwereonlypermittedtocompleteatmostonetransaction(ie,buyoneandselloneshareofthestock),designanalgorithmtofindth 查看详情
121.besttimetobuyandsellstock
Sayyouhaveanarrayforwhichthe ith elementisthepriceofagivenstockonday i.Ifyouwereonlypermittedtocompleteatmostonetransaction(ie,buyoneandselloneshareofthestock),designanalgorithmtofindth 查看详情
121.besttimetobuyandsellstock
121.BestTimetoBuyandSellStockSayyouhaveanarrayforwhichthe ith elementisthepriceofagivenstockonday i.Ifyouwereonlypermittedtocompleteatmostonetransaction(ie,buyoneandselloneshareofthesto 查看详情
14.besttimetobuyandsellstock
题目描述:Sayyouhaveanarrayforwhichthe ith elementisthepriceofagivenstockonday i.Ifyouwereonlypermittedtocompleteatmostonetransaction(ie,buyoneandselloneshareofthestock),designanalgorithmtof 查看详情
besttimetobuyandsellstock系列
// BestTimetoBuyandSellStock1classSolution{public:intmaxProfit(vector<int>&prices){if(prices.size()==0)return0;intmaxres=prices[prices.size()-1];intans=0;for(inti=prices.size()-1;i>= 查看详情
121.besttimetobuyandsellstock
#week8Sayyouhaveanarrayforwhichthe ith elementisthepriceofagivenstockonday i.Ifyouwereonlypermittedtocompleteatmostonetransaction(ie,buyoneandselloneshareofthestock),designanalgorithmto 查看详情
leetcode121besttimetobuyandsellstock
Sayyouhaveanarrayforwhichtheithelementisthepriceofagivenstockondayi.Ifyouwereonlypermittedtocompleteatmostonetransaction(ie,buyoneandselloneshareofthestock),designanalgorithmtofindthemaximumprofit.Exa 查看详情
121.besttimetobuyandsellstock(动态规划)
Sayyouhaveanarrayforwhichtheithelementisthepriceofagivenstockondayi.Ifyouwereonlypermittedtocompleteatmostonetransaction(ie,buyoneandselloneshareofthestock),designanalgorithmtofindthemaximumprofit.Exa 查看详情
121.买卖股票besttimetobuyandsellstock
Sayyouhaveanarrayforwhichthe ith elementisthepriceofagivenstockonday i.Ifyouwereonlypermittedtocompleteatmostonetransaction(ie,buyoneandselloneshareofthestock),designanalgorithmtofindth 查看详情
121.besttimetobuyandsellstock
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/#/solutions Sayyouhaveanarrayforwhichthe ith elementisthepriceofagivenstockonday i.Ifyouwereonlypermittedtocompleteatm 查看详情
[leetcode]besttimetobuyandsellstock
题目:BestTimetoBuyandSellStock给定一个数组,数组中一个元素表示一天的股价,求一次交易能得到的最大收益。思路:数组可能是多个升序降序组成,只要能找到一组极值,使它们的差最大就可以了。这样实际上就是每当找到一个... 查看详情
leetcode121besttimetobuyandsellstock
publicclassS121{publicintmaxProfit(int[]prices){//TLE/*if(prices.length<2)return0;intresult=Integer.MIN_VALUE;for(inti=prices.length-1;i>0;i--){prices[i]=prices[i]-prices[i-1];}for(inti=1;i<p 查看详情
leetcode121.besttimetobuyandsellstock解题小结
题目:Sayyouhaveanarrayforwhichthe ith elementisthepriceofagivenstockonday i.Ifyouwereonlypermittedtocompleteatmostonetransaction(ie,buyoneandselloneshareofthestock),designanalgorithmtofin 查看详情