[Leetcode] 71st biweekly match

foreword

In the second weekly competition I participated in, I answered the first two questions, and I didn't do the third question because I was asked to eat. It's not difficult to look back. But whether it can be done in the official game is not necessarily hahaha.

come on!

5984. Minimum sum of four digits after splitting digits

Topic display

insert image description here

Interpretation of the title

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 the number of digits, and finally return the minimum sum of the composed numbers.

Problem solving ideas

First of all, if you want the number and the smallest, then we must be a combination of two digits, and then the smallest two digits of the four numbers are the first of the two numbers, and the others are the second.

We directly use the remainder to split this four-digit integer into four numbers, store them in an array, and then sort them. You can combine them in pairs according to the size rules.

Code

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 the

insert image description here

5985. Divide Array by Given Number

Topic display

insert image description here

Interpretation of the title

The title means that given an array and a value pivot, it is required to sort this array. The sorting requirement is that the pivot smaller than this value is placed on the left side of the pivot, and the larger value is placed on the right side of the pivot. The relative order requirements for other numbers remain unchanged.

Problem solving ideas

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

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

We use for loop + if judgment to achieve each filling.

Code

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 the

insert image description here

5986. Minimum cost to set time

Topic display

insert image description here

Interpretation of the title

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

Problem solving ideas

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

One is normal minutes and seconds (both are less than 60)

Another is to subtract 1 from normal minutes and add 60 to seconds.

However, both of them must ensure that the minutes and seconds cannot exceed 99, and we have to verify each format.

After the cost is finally calculated, return an answer with a lower cost.

Code

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 the

insert image description here

Epilogue

The fourth question is given up, and it is enough to understand the first three questions at the beginning. don't scold haha

Guess you like

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