121. Best Time to Buy and Sell Stock (一) 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 transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Example 1:

Input: [7, 1, 5, 3, 6, 4]
Output: 5

max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price) 

Example 2:

Input: [7, 6, 4, 3, 1]
Output: 0

In this case, no transaction is done, i.e. max profit = 0.

仿佛是怕我们看不懂么   特意给出了两个例子  其实这个题目的意思是给你一个数组 数组的第i个元素意味着商品在第i天的价格 如何选择最佳时间买入和售出商品获得最大利润(只允许一次交易)

那无疑是尽量在最低的时候买入 最高的时候卖出  同时买入时间肯定要先于卖出时间 感觉一次遍历够用了 只需要维护一个最小值即可

思路 从第一个元素开始遍历 同时维护一个已遍历元素中的最小值  和最大收益值 便利完返回收益即可

有了思路 接下里就是码代码了 这里养成一个习惯吧 尽量在文本编辑器 或者直接在leetcode的代码输入框写代码 不过分依赖工具 除非找不出问题再用工具调试

public class Solution {
    public int maxProfit(int[] prices) {
        int min=Integer.MAX_VALUE;
        int profit=0;
        for(int i=0;i<prices.length-1;i++){
        min=prices[i]<min?prices[i]:min;
        profit=(prices[i+1]-min)>profit?prices[i+1]-min:profit;
        }
        return profit;
    }
}

  依旧是一次过。

惯例  看看详情

还可以 2ms  o(1)的空间复杂度 o(n)的时间复杂度 个人认为没有最优解了  时间比我短的可能是测试用例不一样这里就不去看别人的discuss了  没必要   转战下一题

时间: 2024-12-26 18:14:42

121. Best Time to Buy and Sell Stock (一) leetcode解题笔记的相关文章

188. Best Time to Buy and Sell Stock IV leetcode解题笔记

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 at most k transactions. Note:You may not engage in multiple transactions at the same time (ie, yo

leetCode 121. Best Time to Buy and Sell Stock 数组

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 algor

【leetcode】121. Best Time to Buy and Sell Stock

@requires_authorization @author johnsondu @create_time 2015.7.19 21:01 @url [best time to buy and sell stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) /************************ * @description: dynamic programming. * 相邻元素做差,然后转化

Best Time to Buy and Sell Stock III leetcode java

题目: 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 at most two transactions. Note: You may not engage in multiple transactions at the same time

Best Time to Buy and Sell Stock II leetcode java

题目: 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)

[email&#160;protected] [121]Best Time to Buy and Sell Stock

Best Time to Buy and Sell Stock Total Accepted: 69292 Total Submissions: 206193 Difficulty: Medium 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

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 maximum profit. Exam

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 transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit

Leetcode 121. Best Time to Buy and Sell Stock JAVA语言

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