[daily question] game 279 weekly question - weekly game is still interesting

Posted by evanesq on Sun, 06 Feb 2022 20:43:19 +0100

⭐ New pit in winter vacation -- daily question notes of code Fox

⭐ Problem solving ideas

  1. Save in groups by subscript parity and sort them separately
  2. Get each digit, sort, and process according to the positive and negative classification (negative numbers require the largest positive number after rearrangement, positive numbers require the smallest and no leading zero)
  3. The difficulty is to consider optimizing the inversion operation - save an inversion array. The inversion operation does not need to reprocess each bit, but just exchange the inversion array with the current array, otherwise it will timeout
  4. DP, save with L[i], only consider the minimum cost of processing node i from the left, the same as R[i], and then go through it again to judge whether L[i]+R[i+1] is the minimum cost (note that L[n] and R[0] judge separately, which means only processing from left to right or processing from right to left); The idea of finding DP array L and R: since there are only two operations - for a problematic carriage - edge removal operation (all the front carriages are required to be removed, and the cost is the number of carriages including this carriage and the front carriage), intermediate removal operation (the cost is the minimum value processed by the previous carriage + 2, and what operation of the previous carriage does not affect the operation of the current carriage at all); For a normal car - the cost of the car before direct integration is enough

6000. Sort parity subscripts separately - week 279 question 1

Give you an integer array nums with subscript starting from 0. Rearrange the values in nums according to the following rules:

  1. Arranges all values on the num odd subscript in non increasing order.
    • For example, if num = [4, * * * 1 * * *, 2, * * * 3 * * *] before sorting, the value of odd subscript will become [4, * * * 3 * * *, 2, * * * 1 * * *] after sorting. The values of odd subscripts 1 and 3 are rearranged in non increasing order.
  2. Arranges all values on the num even subscript in non decreasing order.
    • For example, if num = [* * * 4 * * *, 1, * * * 2 * * *, 3] before sorting, the value of even subscript will become [* * * 2 * * *, 1, * * * 4 * * *, 3] after sorting. The values of even subscripts 0 and 2 are rearranged in non decreasing order.

Returns an array formed after rearranging the values of nums.

class Solution {
    public int[] sortEvenOdd(int[] nums) {
        List<Integer> j=new ArrayList<>();
        List<Integer> o=new ArrayList<>();
        for(int i=0;i<nums.length;i++){
            if(i%2==0){
                o.add(nums[i]);
            }
            else{
                j.add(nums[i]);
            }
        }
        
        Collections.sort(o);
        Collections.sort(j,(a,b)->(b-a));
        
        for(int i=0;i<nums.length;i++){
            if(i%2==0){
                nums[i]=o.get(i/2);
            }
            else{
                nums[i]=j.get(i/2);
            }
        }
        return nums;
    }
}

6001. Minimum value of rearrangement number - Mid - game 279 weekly question 2

Give you an integer num. Rearrange the digits in num to minimize their value without any leading zeros.

Returns the smallest rearranged number without leading zeros.

Note that after rearranging the numbers, the symbol of num will not change.

class Solution {
    public long smallestNumber(long num) {
        if(num==0){
            return 0;
        }
        
        boolean T=false;
        if(num>0){
            T=true;
        }
        else{
            num=-num;
        }
        
        List<Long> l=new ArrayList<>();
        while(num!=0){
            l.add(num%10);
            num/=10;
        }
        
        Collections.sort(l);
        if(T==false){
            int c=l.size()-1;
            while(c>=0){
                num=num*10+l.get(c);
                c--;
            }
            return -num;
        }
        else{
            int c=0;
            while(l.get(c)==0){
                c++;
            }
            num+=l.get(c);
            c++;
            for(int i=0;i<c-1;i++){
                num*=10;
            }
            while(c<l.size()){
                num=num*10+l.get(c);
                c++;
            }
            return num;
        }
    }
}

6002. Design bit set Mid game 279 week 3

Bit set Bitset is a data structure that can store bits in a compact form.

Please implement Bitset class.

  • 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.
class Bitset {
    boolean[] bits;
    boolean[] bit;
    int count_all;
    int count_1;
    public Bitset(int size) {
        bits=new boolean[size];
        bit=new boolean[size];
        count_all=size;
        for(int i=0;i<count_all;i++){
            bit[i]=true;
        }
        
        count_1=0;
    }
    
    public void fix(int idx) {
        if(!bits[idx]){
            bits[idx]=true;
            count_1++;
        }
        bit[idx]=false;
    }
    
    public void unfix(int idx) {
        if(bits[idx]){
            bits[idx]=false;
            count_1--;
        }
        bit[idx]=true;
    }
    
    public void flip() {
        count_1=count_all-count_1;
        
        boolean[] mid=bits;
        bits=bit;
        bit=mid;
    }
    
    public boolean all() {
        return count_1==count_all;
    }
    
    public boolean one() {
        return count_1>=1;
    }
    
    public int count() {
        return count_1;
    }
    
    public String toString() {
        StringBuffer res=new StringBuffer();
        for(int i=0;i<count_all;i++){
            if(bits[i])
                res.append(1);
            else{
                res.append(0);
            }
        }
        return res.toString();
    }
}

/**
 * Your Bitset object will be instantiated and called as such:
 * Bitset obj = new Bitset(size);
 * obj.fix(idx);
 * obj.unfix(idx);
 * obj.flip();
 * boolean param_4 = obj.all();
 * boolean param_5 = obj.one();
 * int param_6 = obj.count();
 * String param_7 = obj.toString();
 */

6003. Minimum time required to remove all cars carrying prohibited goods - Hard - game 279 week 4

Give you a binary string s with subscript starting from 0 to represent a train carriage sequence. s[i] = '0' indicates that the I carriage does not contain prohibited goods, while s[i] = '1' indicates that the I carriage contains prohibited goods.

As a conductor, you need to clean up all the cars carrying prohibited goods. You can perform any of the following three operations unlimited times:

  1. It takes 1 unit of time to remove a carriage from the left end of the train (i.e. remove s[0]).
  2. It takes 1 unit time to remove a carriage from the right end of the train (i.e. remove s[s.length - 1]).
  3. It takes 2 units of time to remove a car from any position in the train car sequence.

Returns the minimum unit time required to remove all cars carrying prohibited goods.

Note that an empty train carriage sequence is considered to have no carriage containing prohibited goods.

class Solution {
    public int minimumTime(String s) {
        int n=s.length();
        int[][] l=new int[n][2];
        if(s.charAt(0)=='1'){
            l[0][1]=2;
            l[0][0]=1;
        }
        
        for(int i=1;i<n;i++){
            if(s.charAt(i)=='1'){
                l[i][0]=i+1;
                l[i][1]=Math.min(l[i-1][0],l[i-1][1])+2;
            }
            else{
                l[i][0]=l[i-1][0];
                l[i][1]=l[i-1][1];
            }
        }
        
        int[][] r=new int[n][2];
        if(s.charAt(n-1)=='1'){
            r[n-1][1]=2;
            r[n-1][0]=1;
        }
        
        for(int i=n-2;i>=0;i--){
            if(s.charAt(i)=='1'){
                r[i][0]=n-(i);
                r[i][1]=Math.min(r[i+1][0],r[i+1][1])+2;
            }
            else{
                r[i][0]=r[i+1][0];
                r[i][1]=r[i+1][1];
            }
        }
        
        for(int i=0;i<n;i++){
            r[i][0]=Math.min(r[i][0],r[i][1]);
            l[i][0]=Math.min(l[i][0],l[i][1]);
        }
        
        int ans=Math.min(l[n-1][0],r[0][0]);
        
        for(int i=0;i<n-1;i++){
            if(ans>(l[i][0]+r[i+1][0])){
                ans=(l[i][0]+r[i+1][0]);
            }
        }
        
        return ans;
    }
}

ending

Title Source: LeetCode link: https://leetcode-cn.com/problems

⭐ Pay attention to the author, take you to brush the questions, and learn the most commonly used algorithm skills from simple algorithm questions (one question per day in winter vacation)
⭐ Pay attention to the author's problem brushing - simple to advanced, which makes you unknowingly become a ruthless problem brushing machine. Please send a private letter if you have any questions

Topics: Algorithm leetcode