Check-in button question 7

#左耳音风 ARST check-in activity restart#

Table of contents

 1. Topic

2. Problem solving method one

3. Problem solving method 2


 Interpretation of ARTS - Complete one ARTS per week:
● Algorithm: Do at least one LeetCode algorithm problem per week
● Review: Read and comment on at least one English technical article
● Tips: Learn at least one technical skill
● Share: Share one Technical articles with opinions and thoughts

It is hoped that through this event, a wave of people who love technology can be gathered, and the spirit of curiosity, exploration, practice, and sharing can be continued.
 


 1. Topic

Given an integer array nums of length n, please judge whether the array can be changed into a non-decreasing sequence by changing at most 1 element.

We define a non-decreasing array like this: For any i (0 <= i <= n-2) in the array, nums[i] <= nums[i + 1] is always satisfied.

Example 1:

Input: nums = [4,2,3]
Output: true
Explanation: You can make it a non-decreasing sequence by changing the first 4 to 1.


Example 2:

Input: nums = [4,2,1]
Output: false
Explanation: You can't make it into a non-decreasing array by changing only one element.

2. Problem solving method one

def checkPossibility(nums):
    for i in range(len(nums) - 1):
        if nums[i] > nums[i + 1]:
            # 如果当前元素比下一个元素大,则需要将当前元素减小或将下一个元素增大
            # 为了只改变一个元素,我们可以将当前元素减小到比下一个元素小的最小值
            # 或者将下一个元素增大到比当前元素大的最小值
            # 因此,我们只需要找到这两个数中的最小值即可
            return min(nums[i], nums[i + 1]) < max(nums[i], nums[i + 1])
    # 如果所有元素都满足非递减的条件,则返回 True
    return True

This code implements a function `checkPossibility`, which is used to judge whether the given integer array can be changed into a non-decreasing array in the case of changing at most 1 element.

The input parameter of the function is an integer array `nums`.

First, we use a loop to go through each element `i` in the array and check if the current element `nums[i]` is greater than the next element `nums[i + 1]`. If yes, it means that the current element needs to be modified so that the entire array can become a non-decreasing sequence.

In order to change only one element, we can reduce the current element `nums[i]` to the minimum value smaller than the next element `nums[i + 1]`, or the next element `nums[i + 1]` Grow to the minimum value greater than the current element `nums[i]`. Therefore, we just need to find the minimum of these two numbers.

Specifically, we can use the Python built-in function `min()` to find the minimum of two numbers. If this minimum value is less than or equal to another number, it means that they can be equal, that is, no operation is required; otherwise, we need to reduce the smaller number to be equal to the larger number.

Finally, return True if all elements satisfy the non-decreasing condition; otherwise, return False.

3. Problem solving method 2

Another workaround is to use the double pointer method. Specifically, we can define two pointers `left` and `right`, which point to the beginning and end of the array respectively. We then iterate through each element in the array from left to right and check if the current element is less than or equal to the element on the right. If yes, it means that the current element needs to be modified so that the entire array can become a non-decreasing sequence.

To change only one element, we can reduce the current element `nums[i]` to a minimum value smaller than the element on the right `nums[j]`, or increase the element on the right `nums[j]` to a value smaller than The minimum value of the current element `nums[i]`. Therefore, we just need to find the minimum of these two numbers.

Specifically, we can use the double pointer method to implement this algorithm. First, we set the `left` pointer to the first element of the array and the `right` pointer to the last element of the array. Then, we iterate over each element `i` in the array from left to right, and check if the current element is less than or equal to the element `nums[j]` on the right. If yes, it means that the current element needs to be modified, we need to move the `left` pointer to the next position, and update the minimum value on the left; otherwise, we need to move the `right` pointer to the previous position, and update the minimum value on the right . Finally, return True if all elements satisfy the non-decreasing condition; otherwise, return False.

The time complexity of this algorithm is O(n), where n is the length of the array.

def checkPossibility(nums):
    left, right = 0, len(nums) - 1
    for i in range(len(nums)):
        while left < right and nums[left] > nums[i]:
            # 如果左边的元素比当前元素大,则将左边的指针向右移动一位
            left += 1
        while left < right and nums[right] < nums[i]:
            # 如果右边的元素比当前元素小,则将右边的指针向左移动一位
            right -= 1
        if left >= right:
            # 如果左边的指针已经到达了数组的末尾,说明无法通过修改元素使得整个数组变成非递减数列
            return False
        # 将当前元素修改为左边和右边中的最小值,以满足非递减的条件
        nums[i] = min(nums[left], nums[right])
    return True

Guess you like

Origin blog.csdn.net/m0_49914128/article/details/132012483