Algorithm: Maximum difference in the array---"Challenge Method"


1. Topic:

Given an integer array nums, find the maximum difference between two numbers in the given array. Requirements, the second number must be greater than the first number.


2. Analysis features:

  • 求最大差值 ==> 最大值 - 最小值
  • It only needs to traverse the price array once, record the historical minimum value, and consider the non-minimum value as the maximum value.

3. Code:

    public int maxProfit(int nums[]) {
    
    
        int minNum = Integer.MAX_VALUE;
        int maxNum = 0;
        for (int i = 0; i < nums.length; i++) {
    
    
            if (nums[i] < minNum) {
    
    
                minNum = nums[i];
            } else if (nums[i] - minNum > maxNum) {
    
    
                maxNum = nums[i] - minNum;
            }
        }
        return maxNum;
    }

4. Complexity analysis:

  • Time complexity: O(n), only need to traverse once.
  • Space complexity: O(1), only constant variables are used.

5. Summary:

Using the idea of ​​​​fighting, when traversing, consider the current value to be the minimum value, then record the minimum value, otherwise consider the current value to be the maximum value, and update.


6. Other solutions – violence

    public int maxProfit(int[] nums) {
    
    
        int maxNum = 0;
        for (int i = 0; i < nums.length - 1; i++) {
    
    
            for (int j = i + 1; j < nums.length; j++) {
    
    
                int subValue = nums[j] - nums[i];
                if (subValue > maxNum) {
    
    
                    maxNum = subValue;
                }
            }
        }
        return maxNum;
    }

6-1. Complexity analysis


7. Changes in the topic

Given an array prices, its i-th element prices[i] represents the price of a given stock on day i.

You can only choose to buy the stock on one day and sell the stock on a different day in the future . Design an algorithm to calculate the maximum profit you can make.

Returns the maximum profit you can make from this trade. If you can't make any profit, return 0.

Let's pretend we are buying stocks. Over time, every day we have the choice to sell the stock or not. So, assuming that on day i, if we want to sell stocks today, how much money can we make?

Obviously, if we are really buying and selling stocks, we will definitely think: If only I bought the stock at an all-time low! Great, in the question, we only need to use a variable to record the lowest price in history, minprice, and we can assume that our stock was bought on that day. Then the profit we can get from selling the stock on day i is prices[i] - minprice.

Therefore, we only need to traverse the price array once, record the historical low, and then consider this question every day: If I bought at the historical low, how much money can I make by selling today? When all days are considered, we get the best answer.


7-1. One traversal

    public int maxProfit(int prices[]) {
    
    
        int minprice = Integer.MAX_VALUE;
        int maxprofit = 0;
        for (int i = 0; i < prices.length; i++) {
    
    
            if (prices[i] < minprice) {
    
    
                minprice = prices[i];
            } else if (prices[i] - minprice > maxprofit) {
    
    
                maxprofit = prices[i] - minprice;
            }
        }
        return maxprofit;
    }
■ Complexity analysis:
  • Time complexity: O(n), only need to traverse once.
  • Space complexity: O(1), only constant variables are used.

7-2. Violence law

public class Solution {
    public int maxProfit(int[] prices) {
        int maxprofit = 0;
        for (int i = 0; i < prices.length - 1; i++) {
            for (int j = i + 1; j < prices.length; j++) {
                int profit = prices[j] - prices[i];
                if (profit > maxprofit) {
                    maxprofit = profit;
                }
            }
        }
        return maxprofit;
    }
}
■ Complexity analysis:




If this article is helpful to you, please remember to give Yile a like, thank you!

Guess you like

Origin blog.csdn.net/weixin_45630258/article/details/132737088