[Algorithm Beaten Diary] day06——1004. The maximum number of consecutive 1's III, 1658. The minimum number of operations to reduce x to 0

 

 1004. Maximum number of consecutive 1’s III

1004. Maximum number of consecutive 1’s III

Topic description: 

Given a binary array  nums and an integer  k return   the largest consecutive number in the array k if at  most  it can be flipped  .01

 Problem-solving ideas:

First of all, the question requires us to find the length of the subarray with the most consecutive 1s after flipping up to k 0s (you can flip [0, k] 0s, not all of them)

We can use the idea of ​​left and right sliding windows to set a zero to record the number of 0s. Right keeps moving to the right. When nums [right] is equal to 0, zero++, when the number of zeros is greater than k, left goes right first. When nums[left] is equal to 0, zero--until zero<=k, and then updates the size of length.

Solution code:

class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int length=0;
        int n=nums.size();
        int zero=0;
        int left=0,right=0;
        while(right<n)
        {
            if(nums[right]==0)
                zero++;
            while(zero>k)
            {
                if(nums[left++]==0)
                zero--;
            }
            length=max(length,right-left+1);
            right++;
        }
        return length;
    }
};

 1658. Minimum number of operations to reduce x to 0

1658. Minimum number of operations to reduce x to 0

Topic description: 

You are given an array of integers  nums and an integer  x . On each operation, you should remove  nums the leftmost or rightmost element of the array and then subtract that element's value x from it . Note that the array needs to  be modified  for subsequent operations.

If it can   be reduced  x to exactly0  , return  the minimum operand  ; otherwise, return  -1 .

Problem-solving ideas:

This question is to continuously subtract a number from the left and right sides to reduce x to 0. We can find that the arrays subtracted from the left and right sides are two continuous intervals, which means that the entire large array is divided into three small arrays. We can change our thinking : It turns into finding the longest length where the sum of the intermediate arrays is equal to target (sum of large arrays - x), that is, it becomes a subarray problem.

It is worth noting that length should be initialized to -1, not 0, because there are two situations when length=0

  • When the array is [5,6,7,8,9], and x=4, each number in the middle array is greater than x
  • Just length=0, every element has to be released

Solution code:

class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int sum=0;
        for(int i=0;i<nums.size();i++)
        sum+=nums[i];
        int target=sum-x;
        if(target<0)  return -1;
        int n=nums.size();
        int length=-1;
        for(int left=0,right=0,num=0;right<n;right++)
        {
            num+=nums[right];
            while(num>target)
            num-=nums[left++];
            if(num==target)
            length=max(length,right-left+1);
        }
        if(length==-1)return length;
        else return n-length;
    }
}; 

Guess you like

Origin blog.csdn.net/m0_69061857/article/details/132891809