Follow left theological algorithm

Posted by xadmin on Wed, 19 Jan 2022 04:56:05 +0100

Only as personal study notes, don't spray!

1. Given an array and a reference value, it is required that less than the reference value is on the left of the array, equal to the reference value is in the middle, and greater than the reference value is on the right

public static int[] hierarchy(int[] nums,int equivalent){
       /* 1.If the current value is equal to the judgment value, skip to the next one;
       * 2.The current value is less than the judgment value. The current value is exchanged with the value after the less than area. The less than area moves one to the right and jumps to the next;
       * 3.The current value is greater than the judgment value. The current value is exchanged with the previous value in the greater than area, and the greater than area is shifted to the left.
       * */
        if(nums==null){
            return null;
        }
        int l=-1;
        int r=nums.length;
        int i=l+1;
        while (i<r){
            if(nums[i]<equivalent){
                change(nums,i,l+1);
                l++;
                i++;
            }else if(nums[i]>equivalent){
                change(nums,i,r-1);
                r--;
            }else {
                i++;
            }
            System.out.println("i="+i+" l="+l+" r="+r);
        }
        return nums;
    }

    public static  void  change(int[] nums,int i,int j){
        int temp=nums[i];
        nums[i]=nums[j];
        nums[j]=temp;
    }

Problem solving ideas:

The left is less than the right subscript l of the area, and the right is greater than the left subscript r = nums length

(1) if the current value is equal to the judgment value, skip to the next one;

(2) The current value is less than the judgment value. The current value is exchanged with the value after the less than area. The less than area moves one to the right and jumps to the next;

(3) The current value is greater than the judgment value. The current value is exchanged with the previous value in the greater than area, and the greater than area is shifted to the left.

2. Variants of the Dutch flag
Force buckle https://leetcode-cn.com/problems/first-missing-positive/

Give you an unordered integer array nums. Please find the smallest positive integer that does not appear in it. Please implement a solution with time complexity of O(n) and only use constant level additional space.

public static int getMinPositiveInteger(int[] nums){
       int L=0;
       int R=nums.length;
        while (L!=R){
            if(nums[L]==L+1){
                L++;
            }else if(nums[L]<=L || nums[L]>R || nums[L]==nums[nums[L]-1] ){
                change(nums,L,--R);
            }else {
                change(nums,L,nums[L]-1);
            }
        }
        return L+1;
    }

    public static void change(int[] nums,int i,int j){
        int temp=nums[i];
        nums[i]=nums[j];
        nums[j]=temp;
    }

Problem solving ideas:
Inspired by the Dutch flag, the right subscript of the effective area is L, and the left subscript of the garbage area is R,

In the best case, the array is 1 ~ num length=R,

If it is greater than R, it is garbage data

If num [L] is less than L, it also belongs to garbage data

If a qualified value has been placed at the position of num [num [l] - 1], it indicates that the value is repeated at this time, and it is classified as garbage data

If num[L]==L+1, the next

In other cases, exchange the corresponding position values

3. Given an array, there are only "g" and "B". You can put all B on the left, all G on the right, or all G on the right and all B on the right, but you can only exchange between leading characters. How many times do you need to exchange at least

public static int minStep( String s){
        if(s == null || s.equals("")){
            return 0;
        }
        int stepB=0;
        int b=0;
        int stepG=0;
        int g=0;
        final char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if(chars[i]=='B'){
                stepB+=i-(b++);
            }else{
                stepG+=i-(g++);
            }
        }
        return Math.max(stepB,stepG);


    }

Problem solving ideas:
When a letter is homing, it means that both B and g have been homing, so you only need to take the small value of the steps required for the letter B on the left and the steps required for the letter G on the left.

What letter (b++/g + +) does a variable record currently meet the conditions, and the number of steps (I - (B + + / I - (G + +) is required for the next qualified letter to return

4. There are multiple line segments. The start time point and end time point are integers. How many line segments overlap at most (the overlapping segment is greater than 1)?

public static int getMaxCoincideLine(int[][] lines){
        //Sort by start time
        Arrays.sort(lines, Comparator.comparingInt(a -> a[0]));
        final PriorityQueue<Integer> queue = new PriorityQueue<>();
        int max=0;
        for (int[] line : lines) {
            final int start = line[0];
            final int end = line[1];
            while (!queue.isEmpty() && start>queue.peek()){
                queue.poll();
            }
            queue.add(end);
            int size=queue.size();
            max=Math.max(max,size);
        }
        return max;

    }

Problem solving ideas:
Using a special data structure, the small root heap (PriorityQueue in java) has a minimum number at the top of the heap,

The line segments are arranged in positive order according to the start time point, traversed and placed in the small root heap, and those in the heap that are smaller than the start time point of the current line segment pop up (because the heap is the end time point of the current line segment, which is smaller than the start point of the current line segment, it means that the line segment has ended and is not within the range of the current line segment), and then the end time point of the current line segment is placed in the small root heap. At this time, the number in the heap is the number of overlapping line segments.

Topics: Algorithm leetcode