[Leetcode] game 71 biweekly

preface

In the second week of the competition, a got the first two questions. The third question was not done because he ordered to eat. It's not difficult to look back. But whether it can be done in the official competition is not necessarily ha ha.

come on.

5984. Minimum sum of the last four digits after splitting

Topic display

Interpretation of topic meaning

The meaning of this question is very simple. It is to give you a four digit integer, and then let you split the four digit integer into two numbers. There is no requirement for digits. Finally, it is required to return the minimum sum of the numbers.

Problem solving ideas

First of all, if we want the number and the smallest, we must be a combination of two digits. Then the smallest two digits in the four digits should be the first digit of the two digits, and the others should be the second digit.

We directly use the remainder to divide this four digit integer into four numbers, store them in the array, and then sort them. It can be combined in pairs according to the size rule.

code implementation

class Solution {
    public int minimumSum(int num) {
        int [] a = new int[4];
        for(int i=0;i<4;i++){
            a[i] = num%10;
            num/=10;
        }
        Arrays.sort(a);
        int n = a[0]*10+a[2];
        int m = a[1]*10+a[3];
        return m+n;
    }
}

results of enforcement

5985. Divide the array according to the given number

Topic display

Interpretation of topic meaning

Given an array and a value pivot, it is required to sort the array. The sorting requirement is that the smaller value of pivot is placed on the left of pivot, and the larger value is placed on the right. The relative order of other numbers shall remain unchanged.

Problem solving ideas

Since the relative order of other numbers is required to remain unchanged, we can directly create a new array of the same size to fill the array.

We fill in three times. We fill in the values less than pivot, equal to pivot, and greater than pivot into the array. After three times of filling, the new array is the result array we want to return.

We use for loop + if judgment for each filling.

code implementation

class Solution {
    public int[] pivotArray(int[] nums, int pivot) {
        int len = nums.length;
        int [] a = new int[len];
        int slow=0;
        for(int i=0;i<len;i++){
            if(nums[i]<pivot){
                a[slow++]=nums[i];
            }
        }
        for(int i=0;i<len;i++){
            if(nums[i]==pivot){
                a[slow++]=nums[i];
            }
        }
        for(int i=0;i<len;i++){
            if(nums[i]>pivot){
                a[slow++]=nums[i];
            }
        }
        return a;
    }
}

results of enforcement

5986. Minimum cost of setting time

Topic display

Interpretation of topic meaning

This question is to adjust the time. The meaning of the question is to give us a time in seconds. Let's adjust the time on a display to the required length of time. The format is four digits. The first two digits represent minutes and the last two digits represent seconds. The maximum is 99 minutes and 99 seconds. Every time we adjust the number to the number we need and input this number, we need to consume the "cost". Finally, we have to return the minimum cost.

Problem solving ideas

The maximum time is 99 minutes and 99 seconds, so there are two expressions for the duration we get:

One is the normal minute and second (both less than 60)

Another is to subtract 1 from the normal minute and add 60 to the second.

However, both of them should ensure that the minutes and seconds can not exceed 99. We have to verify each format.

Finally, after calculating the cost, return the answer with less cost.

code implementation

class Solution {
    public int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {
        int min = targetSeconds/60;
        int sec = targetSeconds%60;
        int res1 = CostSetTime(startAt,moveCost,pushCost,min,sec);
        min-=1;
        sec+=60;
        int res2 = CostSetTime(startAt,moveCost,pushCost,min,sec);
        return Math.min(res1,res2);
    }
    public int CostSetTime(int startAt, int moveCost, int pushCost, int min,int sec){
        if(min>99||sec>99) return Integer.MAX_VALUE;
        int res=0;
        int [] a = new int[]{min/10,min%10,sec/10,sec%10};
        int n=0;
        while(a[n]==0) n++;
        while(n<4){
            if(startAt==a[n]) res+=pushCost;
            else {
                res=res+pushCost+moveCost;
                startAt=a[n];
            }
            n++;
        }
        return res;
    }
}

results of enforcement

epilogue

The fourth question is given up. Just start to understand the first three questions. Don't scold hahaha

Keywords: Algorithm leetcode

Added by thecard on Tue, 08 Feb 2022 13:49:00 +0200