LeetCode 71 Biweekly Contest

Broken thoughts

It was absent-minded because the game started ten minutes after the start of all sorts of things.
Because of the last rush to brush rankings, the mood is very urgent, ideas are not sorted out on the code
As a result, only the first three questions are AC ed, and the last one is really in no mood


5984. Minimum sum of four digits after splitting bits

Solving problems

Obviously greedy, for four digits, save your values with a 4-length array a
The sum of two digits required for a title is the smallest.
First sort array a from smallest to largest,
Two small digits must be the ten digits of two numbers, and two large digits must be the digits of two numbers

Code

class Solution {
public:
    int a[4];
    int minimumSum(int num) {
        a[0] = num / 1000;
        a[1] = num / 100 % 10;
        a[2] = num % 100 / 10;
        a[3] = num % 10;
        
        sort(a, a + 4);
        
        return (a[0] * 10 + a[2]) + (a[1] * 10 + a[3]);
    }
};

5985. Divide the array according to a given number

Solving problems

Two extra vector s are opened, scanned once, and the number less than p is put into a
Count equal to p
Place b for numbers greater than p
Finally, put all the elements in a;

Code

class Solution {
public:
    vector<int>a, b;
    int cnt = 0;
    vector<int> pivotArray(vector<int>& nums, int pivot) {
        for (int i = 0; i < nums.size(); i++) {
            if (nums[i] < pivot)  a.push_back(nums[i]);
            else if (pivot == nums[i]) cnt++;
            else b.push_back(nums[i]);
        }

        for (int i = 0; i < cnt; i++) a.push_back(pivot);
        for (auto& x : b)   a.push_back(x);
        return a;
    }
};

5986. Minimum cost of setup time

Solving problems

Minutes and seconds range from 0 to 99;
We can enumerate minute numbers from 0 to 99, and seconds are naturally derived from the formula int j = targetSeconds - i * 60
If the calculated number of seconds is not in the normal range, the next cycle will occur, otherwise the leading 0 will be removed and the ratio will begin to increase.
Conversely, enumerating seconds from 0 to 99 does not calculate minutes

TIPS: For this problem, we should try to avoid calculating a leading 0 because leading 0 only adds extra cost

Code

class Solution {
int ans = 1e9;
int c[4];
public:
    int minCostSetTime(int startAt, int moveCost, int pushCost, int targetSeconds) {
        for (int i = 0; i <= 99; i++) {
            int j = targetSeconds - i * 60;
            if (j < 0 || j > 99)    continue;
            c[0] = i / 10; c[1] = i % 10;
            c[2] = j / 10; c[3] = j % 10;

            bool flag = false;
            int res = 0, d = startAt;
            for (int k = 0; k < 4; k++) {
                if (c[k])  flag = true;  // Start operation only if not leading 0
                if (flag) {
                    if (c[k] != d)
                        res += moveCost, d = c[k];
                    res += pushCost;  
                }
            }
            ans = min(ans, res);
        }
        return ans;
    }
};

5987. Minimum difference of sum after deleting an element

Title Description

Solving problems

Can be referred to This puzzle
In short, since we only end up with 2 * n elements and split them in half,
The difference between the first half and the second half is minimum=>The first half is minimum and the second half is maximum.
So we can use a small root heap to find the minimum sum of prefixes and a large root heap to find the maximum sum of suffixes.

When the number of small/large root heaps reaches n,
For the next num[i], you can join the heap, pop up the top of the heap, and update the minimum prefix and/or maximum suffix sum for an interval

Code

class Solution {
public:
    long long minimumDifference(vector<int>& nums) {
        int m = nums.size(), n = m / 3;
        priority_queue<int, vector<int>, greater<int>> minQ;
        long long sum = 0;

        for (int i = m - n; i < m; i++) {
            minQ.push(nums[i]);
            sum += nums[i];
        }

        vector<long long> sufMax(m - n + 1);
        sufMax[m - n] = sum;
        for (int i = m - n - 1; i >= n; i--) {
            minQ.push(nums[i]);
            sum += nums[i] - minQ.top();
            minQ.pop();
            sufMax[i] = sum;  // Update Suffix Maximum
        }

        priority_queue<int> maxQ;
        long long preMin = 0;
        for (int i = 0; i < n; i++) {
            maxQ.push(nums[i]);
            preMin += nums[i];
        }

        long long ans = preMin - sufMax[n]; 
        for (int i = n; i < m - n; i++) {
            maxQ.push(nums[i]);  // Add heap first
            preMin += nums[i] - maxQ.top();  
            maxQ.pop();  // Eject Maximum
            ans = min(ans, preMin - sufMax[i + 1]);
        }
        return ans;
    }
};

Keywords: Algorithm leetcode greedy algorithm

Added by kingleo on Sun, 06 Feb 2022 19:25:33 +0200