LeetCode#121: Best Time to Buy and Sell Stock
class Solution {
public int maxProfit(int[] prices) {
if(prices == null || prices.length <= 0)
return 0;
int maxProfit = 0;
int minPrice = prices[0];
for(int i = 1; i < prices.length; i++) {
if(prices[i] < minPrice)
minPrice = prices[i] ;
else if(prices[i] - minPrice > maxProfit)
maxProfit = prices[i] - minPrice;
}
return maxProfit;
}
}
相關推薦
[LeetCode] 121. Best Time to Buy and Sell Stock Java
most length 如果 時間復雜度 ase 最大差值 new [1] cas 題目: Say you have an array for which the ith element is the price of a given stock on day i.
leetcode 121. Best Time to Buy and Sell Stock
clas which style ces pre max des sign har Say you have an array for which the ith element is the price of a given stock on day i. If you
[leetcode] 121. Best Time to Buy and Sell Stock 解題報告
leet 解題報告 情況 lee 動態規劃 turn fit mat etc 動態規劃,註意處理當前最大值小於0的情況 public int maxProfit(int[] prices) { if (prices == null || prices.le
Leetcode 121: Best Time to Buy and Sell Stock
output inpu return ive price interview not don style Say you have an array for which the ith element is the price of a given stock on da
[LeetCode] 121. Best Time to Buy and Sell Stock 買賣股票的最佳時間
IT diff ger cti 利潤 變量 lee algorithm clas Say you have an array for which the ith element is the price of a given stock on day i. If you
LeetCode-121. Best Time to Buy and Sell Stock
0.原題 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
[leetcode]121. 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 mos
LeetCode#121: Best Time to Buy and Sell Stock
class Solution { public int maxProfit(int[] prices) { if(prices == null || prices.length
leetcode 121 Best Time to Buy and Sell Stock(最大盈利)
題目要求(高頻題) 假設你有一個數組,其中第i個元素是第i天的股票價格。 如果您只被允許完成最多一筆交易(即買入並賣出一股股票),請設計演算法以找到最大利潤。 請注意,在購買之前不能出售股票。 解題思路 在陣列中尋找最大利潤,和leetcode 53 求最大子陣列是一種型別的題目
LeetCode--121. Best Time to Buy and Sell Stock && 122. Best Time to Buy and Sell Stock II
連續AC了5條medium有點疲憊,休息一下來兩條easy放鬆放鬆。 題目連結:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/和https://leetcode.com/problems/best-time-to-bu
[LeetCode]121. 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 trans
LeetCode——121. Best Time to Buy and Sell Stock(買股票系列1)
一、題目121: 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
LeetCode 121 Best Time to Buy and Sell Stock II 買賣股票的最佳時機 II
class Solution { public: int maxProfit(vector<int>& prices) { int sum=0; for(int i=1;i<prices.size();i++)
[leetcode]Dynamic Programming-121. Best Time to Buy and Sell Stock
ram tran input output algorithm ice dynamic c program one Say you have an array for which the ith element is the price of a given stock o
【leetcode】121-Best Time to Buy and Sell Stock
problem 121. Best Time to Buy and Sell Stock code class Solution { public: int maxProfit(vector<int>& prices) { int re
LeetCode:121. 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
LeetCode(Python版)——121. 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 on
【LeetCode-面試演算法經典-Java實現】【121-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.
[LeetCode] 22. Best Time to Buy and Sell Stock II Java
股票 log ive highlight transacti ever 方法 size 可能 題目: Say you have an array for which the ith element is the price of a given stock on day i
【算法分析與設計】【第一周】121.Best Time to Buy and Sell Stock&122. Best Time to Buy and Sell Stock II
部分 簡化 是我 -i 復雜 style 代碼 求一個 時間 原題來自:121:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ 122:https://leetcode.c