Solution to the 276th weekly game of LeetCode

Posted by itshim on Wed, 19 Jan 2022 05:18:05 +0100

5980. Split the string into groups of length k

Topic Description: given a string \ (s \) and an integer \ (k \), divide the string into substrings with a growth of \ (k \). If the length of the last substring is less than \ (k \), fill it with \ (fill \).

Idea: simulate according to the meaning of the topic

Time complexity: \ (O(n) \)

Reference code:

class Solution {
public:
    vector<string> divideString(string s, int k, char fill) {
        vector<string> res;
        string t;
        for(auto c : s){
            t += c;
            if(t.size() == k) res.push_back(t) , t.clear();
        }
        if(t.size() == 0) return res;
        while(t.size() < k) t += fill;
        res.push_back(t);
        return res;
    }
};

5194. Minimum number of actions to get the target value

Title Description: give you an integer \ (x = 1 \), each time you can perform one of the following operations:

  • \(x = x + 1\)
  • \(x = 2 * x\)

Operate maxDoubles at most twice. Ask the minimum number of operations to change \ (x \) to \ (target \).

Idea: consider doing it backwards. If the current \ (target \) is a count, subtract one. If it is an even number, divide it by \ (2 \) until \ (target \) becomes \ (1 \) or the number of operations 2 is used up. All the remaining operations are in operation 1

Complexity: \ (O(logn) \), \ (n = target \).

Reference code:

class Solution {
public:
    int minMoves(int target, int maxDoubles) {
        int res = 0;
        while(target != 1 && maxDoubles != 0){
            if(target & 1) target --;
            else target >>= 1, --maxDoubles;
            ++res;
        }
        res += target - 1;
        return res;
    }
};

5982. Solving intellectual problems

Title Description: give you a two-dimensional integer array questions starting from \ (0 \), where questions[i] = [pointsi, brainpoweri]. This array represents a series of questions in an exam. You need to solve them in order (that is, start from question \ (0 \), and choose to solve or skip the operation for each question. Solving the problem \ (I \) will give you a score of \ (points_i \), but you will not be able to solve the next \ (brainpower_i \) (that is, you can only skip the next \ (brainpower_i \). If you skip the question \ (I \), you can decide which action to use for the next question.

For example, here are your questions = [[3,2], [4,3], [4,4], [2,5]]:

  • If the problem \ (0 \) is solved, you can get \ (3 \) points, but you can't solve the problems \ (1 \) and \ (2 \).
  • If you skip the problem \ (0 \) and solve the problem \ (1 \), you will get \ (4 \) points, but you can't solve the problems \ (2 \) and \ (3 \).

Please return to the highest score you can get in this exam.

Idea: obvious \ (dp \). Let the status \ (f_i \) represent the highest score that can be obtained by selecting the \ (I \) task, then the transfer equation is:

\[f_i = max(f_i , f_{i - 1})\\ f_{i + j + 1} = max(f_{i+ j + 1} , f_i + position_i)\;j = brainpower_i \]

Time complexity: \ (O(n) \)

Reference code:

class Solution {
public:
    long long mostPoints(vector<vector<int>>& questions) {
        int n = questions.size();
        vector<long long>f(n + 1 , 0);
        for(int i = 0 ; i < n ; ++i){
            int j = questions[i][1] , val = questions[i][0];
            if(i != 0) f[i] = max(f[i] , f[i - 1]);
            int k = min(n , i + j + 1);
            f[k] = max(f[k] , f[i] + val);
        }
        return f[n];
    }
};

5983. Maximum time to run N computers simultaneously

Title Description: give you \ (n \) computers and \ (m \) batteries. Each battery can run one computer for \ (batteries_i \) minutes. Ask the maximum number of minutes that \ (n \) computers can run at the same time.

Data range: \ (1 \ Leq n \ Leq m, 1 \ Leq batteries_i \ Leq 10 ^ 9 \)

Idea: the obvious dichotomy answer is that the final running time is mid, then the power supply time of each battery is min (mid, batteries_i), and the total power supply time is \ (sum = \ sum \ limits_{I = 1} ^ {m} min (mid, batteries_i) \). Just check \ (\ frac{sum}{mid} \geq n \).

Time complexity: \ (O(mlog10^{14})\) \(10^{14} \) is the maximum sum.

Reference code:

class Solution {
public:
    long long maxRunTime(int n, vector<int>& batteries) {
        auto check = [&](long long mid)->bool{
            long long sum = 0;
            for(auto& batterie : batteries) sum += min(mid , 1ll * batterie);
            return sum / mid >= n;
        };
        long long lr = 1 , rs = 1e14, res = 1;
        while(lr <= rs){
            long long mid = lr + rs >> 1;
            if(check(mid)) res = mid , lr = mid + 1;
            else rs = mid - 1;
        }
        return res;
    }
};