Skip to content

Latest commit

 

History

History
189 lines (147 loc) · 5.11 KB

File metadata and controls

189 lines (147 loc) · 5.11 KB

English Version

题目描述

You are given an array nums consisting of positive integers.

You can perform the following operation on the array any number of times:

  • Choose any two adjacent elements and replace them with their sum.
    <ul>
    	<li>For example, if <code>nums = [1,<u>2,3</u>,1]</code>, you can apply one operation to make it <code>[1,5,1]</code>.</li>
    </ul>
    </li>
    

Return the minimum number of operations needed to turn the array into a palindrome.

 

Example 1:

Input: nums = [4,3,2,1,2,3,1]
Output: 2
Explanation: We can turn the array into a palindrome in 2 operations as follows:
- Apply the operation on the fourth and fifth element of the array, nums becomes equal to [4,3,2,3,3,1].
- Apply the operation on the fifth and sixth element of the array, nums becomes equal to [4,3,2,3,4].
The array [4,3,2,3,4] is a palindrome.
It can be shown that 2 is the minimum number of operations needed.

Example 2:

Input: nums = [1,2,3,4]
Output: 3
Explanation: We do the operation 3 times in any position, we obtain the array [10] at the end which is a palindrome.

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 106

解法

方法一:贪心 + 双指针

定义两个指针 $i$$j$,分别指向数组的首尾,用变量 $a$$b$ 分别表示首尾两个元素的值,变量 $ans$ 表示操作次数。

如果 $a \lt b$,我们将指针 $i$ 向右移动一位,即 $i \leftarrow i + 1$,然后将 $a$ 加上指针 $i$ 指向的元素的值,即 $a \leftarrow a + nums[i]$,同时将操作次数加一,即 $ans \leftarrow ans + 1$

如果 $a \gt b$,我们将指针 $j$ 向左移动一位,即 $j \leftarrow j - 1$,然后将 $b$ 加上指针 $j$ 指向的元素的值,即 $b \leftarrow b + nums[j]$,同时将操作次数加一,即 $ans \leftarrow ans + 1$

否则,说明 $a = b$,此时我们将指针 $i$ 向右移动一位,即 $i \leftarrow i + 1$,将指针 $j$ 向左移动一位,即 $j \leftarrow j - 1$,并且更新 $a$$b$ 的值,即 $a \leftarrow nums[i]$ 以及 $b \leftarrow nums[j]$

循环上述过程,直至指针 $i \ge j$,返回操作次数 $ans$ 即可。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度。

Python3

class Solution:
    def minimumOperations(self, nums: List[int]) -> int:
        i, j = 0, len(nums) - 1
        a, b = nums[i], nums[j]
        ans = 0
        while i < j:
            if a < b:
                i += 1
                a += nums[i]
                ans += 1
            elif b < a:
                j -= 1
                b += nums[j]
                ans += 1
            else:
                i, j = i + 1, j - 1
                a, b = nums[i], nums[j]
        return ans

Java

class Solution {
    public int minimumOperations(int[] nums) {
        int i = 0, j = nums.length - 1;
        long a = nums[i], b = nums[j];
        int ans = 0;
        while (i < j) {
            if (a < b) {
                a += nums[++i];
                ++ans;
            } else if (b < a) {
                b += nums[--j];
                ++ans;
            } else {
                a = nums[++i];
                b = nums[--j];
            }
        }
        return ans;
    }
}

C++

class Solution {
public:
    int minimumOperations(vector<int>& nums) {
        int i = 0, j = nums.size() - 1;
        long a = nums[i], b = nums[j];
        int ans = 0;
        while (i < j) {
            if (a < b) {
                a += nums[++i];
                ++ans;
            } else if (b < a) {
                b += nums[--j];
                ++ans;
            } else {
                a = nums[++i];
                b = nums[--j];
            }
        }
        return ans;
    }
};

Go

func minimumOperations(nums []int) int {
	i, j := 0, len(nums)-1
	a, b := nums[i], nums[j]
	ans := 0
	for i < j {
		if a < b {
			i++
			a += nums[i]
			ans++
		} else if b < a {
			j--
			b += nums[j]
			ans++
		} else {
			i, j = i+1, j-1
			a, b = nums[i], nums[j]
		}
	}
	return ans
}

TypeScript

...