Solution to the 279th weekly game of LeetCode

Posted by bqallover on Sun, 06 Feb 2022 06:43:41 +0100

6000. Sort parity subscripts separately

Title Description: give you an array, sort the values on the odd subscript in the array according to non increasing, sort the values on the even subscript according to non decreasing, and return the sorted array.

Idea: simulate according to the meaning of the topic.

Time complexity: \ (O(nlogn) \)

Reference code:

class Solution {
public:
    vector<int> sortEvenOdd(vector<int>& nums) {
        vector<int>lr , rs;
        int n = nums.size();
        for(int i = 0 ; i < n ; ++i){
            if(i % 2 == 0) lr.push_back(nums[i]);
            else rs.push_back(nums[i]);
        }
        sort(rs.begin() , rs.end() , [&](const int& a , const int& b){
            return a > b;
        });
        sort(lr.begin() , lr.end());
        vector<int>res;
        int p = 0 , q = 0;
        while(p < lr.size() || q < rs.size()){
            if(p < lr.size()) res.push_back(lr[p]);
            if(q < rs.size()) res.push_back(rs[q]);
            ++p;++q;
        }
        return res;
    }
};

6001. Rearrange the minimum value of the number

Title Description: give you an integer \ (- 10^{15} \leq num \leq 10^{15} \) to rearrange the numbers in the integer without leading \ (0 \), and find the minimum value after rearrangement.

Idea: simulate according to the meaning of the topic.

Time complexity: \ (O(n) \), \ (n \) is the number of digits of a number

Reference code:

class Solution {
private:
    long long solve(long long num , int flag){
        if(num == 0) return num;
        string s = to_string(num);
        sort(s.begin() , s.end());
        vector<int>cnt(10 , 0);
        for(auto c : s) cnt[c - '0']++;
        long long res = 0;
        if(flag == 1){
            for(int i = 1 ; i < 10 ; ++i){
                if(cnt[i] == 0) continue;
                res = i;
                --cnt[i];
                break;
            }
            for(int i = 0 ; i < 10 ; ++i){
                while(cnt[i]){
                    res = res * 10 + i;
                    --cnt[i];
                }
            }
        }
        else{
            for(int i = 9 ; i >= 0 ; --i){
                while(cnt[i]){
                    --cnt[i];
                    res = res * 10 + i;
                }
            }
        }
        return res;
    }    
public:
    long long smallestNumber(long long num) {
        if(num < 0) return -1* solve(-num , 0);
        return solve(num , 1);
    }
};

6002. Design bit set

Title Description: design a bitset class to realize the following given operations:

  • Bitset(int size) initializes Bitset with size bits, all of which are 0.
  • void fix(int idx) updates the value on the bit with subscript idx to 1. If the value is already 1, nothing will change.
  • void unfix(int idx) updates the value on the bit with subscript idx to 0. If the value is already 0, nothing will change.
  • void flip() flips the value on each bit in the Bitset. In other words, all bits with a value of 0 will become 1 and vice versa.
  • boolean all() checks whether the value of each bit in Bitset is 1. If this condition is met, return true; Otherwise, false is returned.
  • boolean one() checks whether the value of at least one bit in Bitset is 1. If this condition is met, return true; Otherwise, false is returned.
  • int count() returns the total number of bits with a value of 1 in Bitset.
  • String toString() returns the current composition of Bitset. Note that in the result string, the character at the ith subscript should be consistent with the ith bit in Bitset.

Idea: toString method is required, so consider using string type as the bottom layer. Then use the flag to mark the flip operation, and use the variable to store the number of occurrences of character 1 in the string, which is actually flipped only when toString and flag is 1.

Time complexity: \ (O(n)\),\(n \) refers to the number of operations

Reference code:

class Bitset {
private:
    string s;
    int cnt , flag, sz;
public:
    Bitset(int size) {
        s = string(size , '0');
        flag = 0;
        cnt = 0;
        sz = size;
    }
    
    void fix(int idx) {
        if(s[idx] == '1'){
            if(flag) s[idx] = '0' , ++cnt;
            else ;
        }
        else{
            if(flag) ;
            else s[idx] = '1' , ++cnt;
        }
        return ;
    }
    
    void unfix(int idx) {
        if(s[idx] == '0'){
            if(flag) --cnt , s[idx] = '1';
            else ;
        }
        else{
            if(flag);
            else s[idx] = '0' , --cnt;
        }
    }
    
    void flip() {
        flag^= 1;
        cnt = sz - cnt;
    }
    
    bool all() {
        return cnt == sz;
    }
    
    bool one() {
        return cnt != 0;
    }
    
    int count() {
        return cnt;
    }
    
    string toString() {
        if(flag == 0) return s;
        cnt = 0; flag ^= 1;
        for(auto& c : s){
            if(c == '0') c = '1' , ++cnt;
            else c = '0';
        }
        return s;
    }
};

6003. Minimum time required to remove all cars carrying prohibited goods

Title Description: give you a 01 string with a length of \ (n \). You need to delete all \ (1 \) in the string. The cost of deleting a character from the left or right of the string is 1, and the cost of deleting a character from the middle is 2. Ask the minimum price.

Idea: obvious dp. Considering one side, define the state \ (f_i \) to represent the minimum cost to the end of the \ (I \) character, and its transition equation is:

\[f_i = \begin{cases} f_{i - 1} & s_i = 0\\ min(i , f_{i - 1} + 2) & s_i = 1 \end{cases} \]

Consider redefining the state \ (g_i \) on both sides to represent the minimum cost of the \ (I \) character from right to left. The final answer is:

\[\mathop{min}\limits_{i = 1}^{n + 1} f_{i - 1} + g_i \]

Time complexity: \ (O(n) \)

Reference code:

class Solution {
private:
    int solve(string& s){
        int cnt = 0;
        for(auto c : s) cnt += c == '1';
        if(cnt == 0) return 0;
        int n = s.size();
        vector<int> f(n + 1  , 0) , g(n + 2 , 0);
        for(int i = 1 ; i <= n ; ++ i){
            if(s[i - 1] == '0') f[i] = f[i - 1];
            else f[i] = min(f[i - 1] + 2 , i);
        }
        for(int i = n ; i >= 1 ; --i){
            if(s[i - 1] == '0') g[i] = g[i + 1];
            else g[i] = min(g[i + 1] + 2 , n - i + 1);
        }
        int res = INT_MAX;
        for(int i = 1 ; i <= n + 1; ++i){
            res = min(res , f[i - 1] + g[i]);
        }
        return res;
    }
public:
    int minimumTime(string s) {
        return solve(s);
    }
};