leetcode122】的更多相关文章

题目: 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)…
#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 (ie, buy one and sell o…
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 multiple times).…
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 示例 1: 输入: [7,1,5,3,6,4] 输出: 7 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 .   随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(…
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 (ie, buy one and sell on…
public class Solution { public int MaxProfit(int[] prices) { var list = new List<KeyValuePair<int, int>>(); //记录所有的极大值和极小值 ) { ; } ) { ] - prices[] > ? prices[] - prices[] : ; } else { ; i < prices.Length - ; i++) { ]; var cur = prices[i…
题目:给定一个数组,它表示了一只股票的价格浮动,第i个元素代表的是股票第i天的价格.设计一个函数,计算出该股票的最大收益,注意,可以多次买入卖出,但下一次买入必须是在本次持有股票卖出之后.比如[1,7,2,3,6,7,6,7],最大收益为9,第1天买入,第2天卖出,然后第3天买入,第6天卖出,第7天买入,第8天卖出. 分析:该题目的难点在于可以多次买入和卖出. #1 找极值 股票交易取得良好收益的方法是低价买入,高价卖出,并且由于该股票可以多次买入卖出,所以应该在极小值点买入,极大值点卖出.如下…
题意:有一个数组,第i个数据代表的是第i天股票的价格,每天只能先卖出再买进(可以不卖出也可以不买进),求最大收益. 思路:自己去弄几个数组比划比划就知道了,比如[1,2,5,3,6],第一天买进,第二天卖出,再买进,第三天卖出,第四天买进,第五天卖出. 真正计算的就是前一天的价格和当天的价格的差值,[1,3,-2,3],大于0买进,否则不买. 代码: int maxProfit(vector<int>& prices) { int n = prices.size(); ]; ;i<…
题目: 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 multiple time…
leetcode121 Best Time to Buy and Sell Stock 说白了找到最大的两组数之差即可 class Solution { public: int maxProfit(vector<int>& prices) { ; ; i < prices.size(); i++){ ; j < prices.size(); j++){ m = max(m, prices[j] - prices[i]); } } return m; } }; leetcod…