性能如下: Runtime:4ms, faster than98.50% of C++ onlinesubmissionsforBestTime toBuyandSell Stock. Memory Usage:9.5MB, less than86.24% of C++ onlinesubmissionsforBestTime toBuyandSell Stock.
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 = 0; int n = prices.size(); for(int i = 0; ...
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 transaction (ie, buy one and sell one share of the stock), design an algorithm to find the ...
leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/ 解决方案 我们需要找出给定数组中两个数字之间的最大差值(即,最大利润)。此外,第二个数字(卖出价格)必须大于第一个数字(买入价格)。 形式上,对于每组 i 和j (其中 j>i )我们需要找出 max(prices[j]−prices[i])。 方法一:暴力法...
121. Best Time to Buy and Sell Stock problem: 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 ...
题目 给定一个数组 prices 其中 prices[i] 代表给定股票在第 i 天的价格。你希望通过一次买卖来最大化你的利润。返回你可以获得的最大利润,如果不能获取利润,则...
121. Best Time to Buy and Sell Stock code class Solution { public: int maxProfit(vector<int>& prices) { int res = 0; for(int i=0; i<prices.size(); i++) { int buy_price = prices[i]; for(int j=i+1; j<prices.size(); j++) ...
*[121]Best Time to Buy and Sell Stock * *https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ * *algorithms *Easy(46.34%) *Total Accepted:480.5K *Total Submissions:1M *Testcase Example:'[7,1,5,3,6,4]' ...
int res = 0, buy = Integer.MAX_VALUE;for (int price : prices) { buy = Math.min(buy, price);res = Math.max(res, price - buy);} return res;} } 类似题⽬:Best Time to Buy and Sell Stock with Cooldown 到此这篇关于C++实现LeetCode(121.买卖股票的最佳时间)的⽂章就介绍到这了,...
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/discuss/108870/Most-consistent-ways-of-dealing-with-the-series-of-stock-problems 读完本文,你可以去力扣拿下如下题目: 买卖股票的最佳时机 买卖股票的最佳时机 II ...