首页 > 其他分享 >121. Best Time to Buy and Sell Stock [Easy]

121. Best Time to Buy and Sell Stock [Easy]

时间:2023-01-11 17:12:32浏览次数:37  
标签:Sell Buy int 121 right result prices day left

121. Best Time to Buy and Sell Stock

You are given an array prices where prices[i] is the price of a given stock on the ith day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

Constraints:

  • 1 <= prices.length <= 10^5
  • 0 <= prices[i] <= 10^4

Example

Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.

思路

分析题目

  • 求两数之差最大值
  • 只能高位减低位

那也就是说如果低位比高位大,那这个低位就应该被高位取代

题解

  • 无脑快速AC
    public int maxProfit(int[] prices) {
        ArrayList<Integer> data = new ArrayList<>();
        Arrays.stream(prices).forEach(data::add);
        int left, right, result;
        result = left = 0;
        right = left + 1;
	// 低位要小于高位且高位小于数组最大位数
        while (left < right && right < prices.length) {
	    // 如果当前低位比高位大,那高位应该取代低位位置
            if (prices[left] > prices[right]) {
                left = right;
                right = left + 1;
                continue;
            }
            result = Math.max(result, prices[right] - prices[left]);
            right++;
        }
        return result;
    }
  • 优化
    public int maxProfit(int[] prices) {
        ArrayList<Integer> data = new ArrayList<>();
        Arrays.stream(prices).forEach(data::add);
        int left, right, result;
        result = left = 0;
        right = left + 1;
        while (right < prices.length) {
            if (prices[left] > prices[right])
                left = right;
            else
                result = Math.max(result, prices[right] - prices[left]);
            right++;
        }
        return result;
    }

标签:Sell,Buy,int,121,right,result,prices,day,left
From: https://www.cnblogs.com/tanhaoo/p/17041221.html

相关文章

  • leetcode简单:[66, 67, 70, 83, 121, 141, 160, 169, ,206, 338]
    目录66.加一67.二进制求和70.爬楼梯83.删除排序链表中的重复元素121.买卖股票的最佳时机141.环形链表160.相交链表169.多数元素206.反转链表338.比特位计数66.......
  • Buy Low Sell High
    BuyLowSellHigh题解:反悔贪心买股票,一天要么买进,要么卖出,要么什么也不干,求最大利润,首先我们很显然知道了这是一道反悔贪心的题目,我们选择全部买入,但是关键点在于我们......
  • 【题解】CF808E Selling Souvenirs
    题意多重背包,但数据范围很大并且体积小于等于三。思路乱搞。很自然地考虑将物品按照体积分成三类。显然对于同一类的物品从最大开始取最优,那么有一个贪心的想法。直......
  • 基因数据处理121之SSW的score matrix调整,使得与SparkSW评分一致
    更多代码请见:​​https://github.com/xubo245​​基因数据处理系列1.解释SSW的评分矩阵是128*128的,是按char的int值来进行计算的。而blosum50是蛋白质的,而且不是按ABC顺序来......
  • leetcode-121-easy
    BestTimetoBuyandSellStockYouaregivenanarraypriceswhereprices[i]isthepriceofagivenstockontheithday.Youwanttomaximizeyourprofitb......
  • P1217 [USACO1.5]回文质数 Prime Palindromes
    题目题目描述因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以151是回文质数。写一个程序来找出范围[a,b](5<=a<b<=100,000,000)(一亿)间的......
  • 【LeetCode】121. 买卖股票的最佳时机
    暴力破解法递归算法动态规划classSolution{public: intmaxProfit(vector<int>&prices){ size_tnPriceSize=prices.size(); intnMaxProfit=0; if(nP......
  • S1 - Lesson 121 - 122
    Wordscustomer forgetforget-forgot,forgotforgetsth./sb.forgettodo...don'tforgetyourfriendswhenyoubecomerich.Iforgettolockthedoor. ......
  • 121_逆序输出字符串
    题干:编程实现将输入的字符串逆序输出。#逆序输出字符串str_data[::-1]#递归实现逆序,不能用占位defrev_str(str_data):str_length=len(str_data)ifstr_len......
  • 20221215 2. k8s 介绍
    kubernetes与dockerswarm对比DockerSwarmKubernetes开发者Docker公司谷歌发布年份20132014ControllerManagerMasterStorageVolumesPers......