Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina.com 领扣-121/122 最佳买卖时机 Best Time to Buy and Sell MD 目录 目录买卖股票的最佳时机 -121题目暴力法贪心算法(波峰波谷法)优化买卖股票的最佳时机 II -122题目波峰波谷法波峰波谷法优化累加法买卖股票的最佳时机 III -123动态规划 二维数组…
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 at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.…
题目1----121. 买卖股票的最佳时机I: 链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/ 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 解答: 限制只能买入卖出一次 DP1: buy[i]记录截止到第i天是买入的状态的最小花费(值为负数) class Solution { public:…
121题目描述: 解题:记录浏览过的天中最低的价格,并不断更新可能的最大收益,只允许买卖一次的动态规划思想. class Solution { public: int maxProfit(vector<int>& prices) { if(prices.size() == 0) return 0; int min_prices = prices[0]; int max_profit = 0; for(int i = 1 ; i < prices.size(); i++ ){ if…
No.121 MaxProfit 买卖股票的最佳时机 题目 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票. 示例 输入: [7,1,5,3,6,4] 输出: 5 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 . 注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价…
class Solution {public:    /**     * @param prices: Given an integer array     * @return: Maximum profit     */    int maxProfit(vector<int> &prices) {        // write your code hereif(prices.empty())return 0;int maxpro=0;int minprofit=prices[0]…
Hard! 题目描述: 给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你最多可以完成 两笔 交易. 注意: 你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票). 示例 1: 输入: [3,3,5,0,0,3,1,4] 输出: 6 解释: 在第 4 天(股票价格 = 0)的时候买入,在第 6 天(股票价格 = 3)的时候卖出,这笔交易所能获得利润 = 3-0 = 3 .   随后,在第 7 天(股票价格 = 1)的时候买入,…
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) wit…
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. Exam…
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).…