【Leetcode】278th Weekly Match

foreword

The 278th weekly game is the last weekly game on Leetcode in 2021. It is also the first time I have participated in the weekly game. The first question is seconds, and then I may have a stomachache. Can't find it, keeps getting stuck. It wasn't until after the game, after lunch, that I found the bug, and in the end it really made me laugh.

In the future, I will try my best to participate in every weekly competition, and make persistent efforts. Come on in 2022!

5993. Multiply found value by 2

The first question of the 278th weekly game is a simple question

Topic display

insert image description here

Interpretation of the title

This question is relatively simple. The meaning of the question is to find the original number in the array nums. After finding it, double the original and continue to search until the doubled number cannot be found.

Problem solving ideas

Every time we find the original, we have to double the original, so the number we need to find will only get bigger and bigger each time.

Then we can sort the array first. After sorting, every time after the original is found, if the new original exists, it must be behind the current position in the array. We just need to search down, and we don't need to go back and start traversing the array again.

With the idea, let's implement the code!

Code

class Solution {
    
    
    public int findFinalValue(int[] nums, int original) {
    
    
        Arrays.sort(nums);
        for(int i:nums){
    
    
            if(i==original){
    
    
                original*=2;
            }
        }
        return original;
    }
}

Results of the

insert image description here

5981. All subscripts with the highest group score

The second question of the 278th game of the week is a medium

Topic display

insert image description here

Topic interpretation

Given an array, the elements in the array are either 1 or 0, then let's divide the array into left and right parts. The division method is divided according to the subscript of the array, including i=0 and i=n, ​​that is, between any two elements of the array, including before the first element and after the last element. Divide into two parts and finally sum up the total number of 0 in the left half and 1 in the right half, and record the number of points in the subscript of the array when the total is the largest. It is possible that the division method is not unique when the total number is the largest, so it is finally returned in the form of List.

Problem solving ideas

When you see this question, you may want to build a two-layer loop. The outer loop is the position where the array is divided each time. The inner loop is used to count the total number of 1s and 0s, and then use a hash table to save the corresponding number of divisions. position, then find the largest key value, and return a list of corresponding values. It sounds like running water, in one go.

However, it is not feasible. Because the maximum length of the array can reach 100000, it will time out. (Actually, it was written like this for the first time, woo woo woo)

Then we change the way of thinking and solve it when the time complexity is O(n).

We want to know the sum of the 0s on the left and the 1s on the right of each split position, and then count all the subscripts corresponding to the largest value.

In fact, we can solve this step in O(n) time complexity.

We first set up an array whose size is the size of the array nums plus 1 for storage, and the sum of the left 0 and right 1 numbers corresponding to each split position.

We first count the number of 1s on the right side of each split position, which we can solve with one loop.

Knowing the number of 1s on the right and the number of 0s on the left, we can traverse the array once:

When the current position of the nums array is 0, then the total number corresponding to the split position after this number is increased by 1 compared to the total number of the previous split position.

When the current position of the nums array is 1, then the total number corresponding to the split position after this number is reduced by 1 compared to the total number of the previous split position.

What is the maximum total number of records compared during this process.

After doing this, traverse the array of records again, and save all the subscripts equal to the maximum record number in the list.

The idea is ok, let's implement it with code next.

Code

class Solution {
    
    
    public List<Integer> maxScoreIndices(int[] nums) {
    
    
        int len=nums.length;
        int[] ar = new int[len+1];
        for(int i : nums){
    
    
            if(i==1) ar[0]++;
        }
        int max = ar[0];
        for(int i=1 ;i<=len;i++){
    
    
            if(nums[i-1]==0) ar[i]=ar[i-1]+1;
            else ar[i]=ar[i-1]-1;
            if(ar[i]>max) max=ar[i];
        }
        List<Integer> list= new ArrayList(); 
        for(int i=0 ;i<len+1 ;i++){
    
    
            if(ar[i]==max){
    
    
                list.add(i);
            }
        }
        return list;
    }
}

Results of the

insert image description here

Epilogue

The last two questions will not, hahaha

Guess you like

Origin blog.csdn.net/apple_51673523/article/details/122755335