LeetCode 302nd Weekly Competition (Java)

Question 1: 6120. How many pairs can an array form?

You are given an array nums of integers with indices starting at 0. In one step, you can perform the following steps:

Select two equal integers from nums
Remove these two integers from nums to form a pair
Please perform this operation on nums as many times as you can.

Returns an integer array answer with subscript starting from 0 and length 2 as the answer, where answer[0] is the number of pairs formed and answer[1] is the number of integers left after performing the above operation on nums as much as possible.

Example 1:

Input: nums = [1,3,2,1,3,2,2]
Output: [3,1]

public int[] numberOfPairs(int[] nums) {
        Arrays.sort(nums);
        int len=nums.length;
        int[] ans=new int[2];
        for (int i = 1; i < len; i++) {
            if (nums[i]==nums[i-1]){
                i++;
                ans[0]++;
            }
        }
        ans[1]=len-2*ans[0];
        return ans;
    }

  First, sort the array, then traverse the array, and judge whether the current number is the same as the previous number. If it is the same, then i should be manually increased by one, as deleting the current number pair, and recording the ans[0] of the logarithm ++, and finally ans[1] is the array length -2* logarithm, which is ans[0].

Problem 2: 6164. Maximum sum of pairs of digits and equal numbers

Given an array nums whose subscript starts from 0, the elements in the array are all positive integers. Please choose two subscripts i and j (i != j), and the digit sum of nums[i] is equal to the digit sum of nums[j].

Please find out all the subscripts i and j that meet the conditions, find out and return the maximum value that nums[i] + nums[j] can get.

Example 1:

Input: nums = [18,43,36,13,7]
Output: 54
 

public int maximumSum(int[] arr) {
        Node[] nodes=new Node[arr.length];
        for (int i = 0; i < arr.length; i++) {
            nodes[i]=new Node(arr[i],sum(arr[i]));
        }
        Arrays.sort(nodes);
        int max=-1;
        for (int i = 1; i <arr.length ; i++) {
            if (nodes[i].sum==nodes[i-1].sum){
                int a=nodes[i].n;
                int b=nodes[i-1].n;
                max=Math.max(a+b,max);
            }
        }
        return max;
    }
    public static int sum(int x){
        char[] c=String.valueOf(x).toCharArray();
        int t=0;
        for (int i = 0; i < c.length; i++) {
            t+=c[i]-'0';
        }
        return t;
    }

    static class Node implements Comparable<Node>{
        int n,sum;

        public Node(int n, int sum) {
            this.n = n;
            this.sum = sum;
        }

        @Override
        public int compareTo(Node o) {
            if (this.sum==o.sum) return o.n-this.n;
            return this.sum-o.sum;
        }
    }

It is necessary to package each number and its own digital sum into an object, and then sort according to the size of the digital sum, so as to find the number with the same digital sum, traverse the nodes, and judge whether the digital sum of the current node is equal to the digital sum of the previous node. If they are equal, compare and update the sum of the two numbers with the result max.

Question 3: 6121. Query the Kth smallest number after cutting the number

You are given an array nums of zero-based strings, where each string is equal in length and contains only numbers.

Then give you a two-dimensional integer array queries whose index starts from 0, where queries[i] = [ki, trimi] . For each queries[i] , you need:

Trim each number in nums to the rightmost trimi digits.
In the clipped numbers, find the subscript corresponding to the kith smallest number in nums. If two clipped numbers are the same size, the number with the smaller subscript is treated as the smaller number.
Restore each number in nums to its original string.
Please return an array answer whose length is equal to queries, where answer[i] is the result of the ith query.

hint:

Clipping to x digits means to keep removing the leftmost digits until x digits remain.
Strings in nums may have leading 0's.

Example 1:

Input: nums = ["102","473","251","814"], queries = [[1,1],[2,3],[4,2],[1,2]]
output : [2,2,1,0]

public int[] smallestTrimmedNumbers(String[] s, int[][] q) {
        int[] res=new int[q.length];
        for (int i = 0; i < q.length; i++) {
            Node[] node=new Node[s.length];
            for (int j = 0; j < s.length; j++) {
                String ss=s[j].substring(s[j].length()-q[i][1]);
                node[j]=new Node(ss,j);
            }
            Arrays.sort(node);
            res[i]=node[q[i][0]-1].idx;
        }
        return res;
    }
    static class Node implements Comparable<Node>{
        int idx;
        String n;

        public Node(String n, int idx) {
            this.n = n;
            this.idx = idx;
        }

        @Override
        public int compareTo(Node o) {
            if (this.n==o.n) return this.idx-o.idx;
            return this.n.compareTo(o.n);
        }
    }

Similar to the second question, it is necessary to encapsulate the cut string array and its own index value into an object, and then sort according to the lexicographical size of the strings in order to find the kth smallest number. Here is a small detail, That is, the kth smallest number is the idx of the node whose index is k-1 after sorting.

Question 4: 6122. The minimum number of deletions to make an array divisible

You are given two arrays of positive integers nums and numsDivide . You can remove any number of elements from nums.

Please return the minimum number of deletions that make the smallest element in nums divisible by all elements in numsDivide. Returns -1 if no such element can be obtained.

If y % x == 0 , then we say that the integer x divides y .

Example 1:

Input: nums = [2,3,2,4,3], numsDivide = [9,6,9,3,15]
Output: 2
 

public int minOperations(int[] arr, int[] d) {
        Arrays.sort(arr);
        int[] f = f(d);
        for (int i = 0; i < arr.length; i++) {
            if (i>0&&arr[i]==arr[i-1])continue;
            int cnt=0;
            while (cnt<f.length&&f[cnt]%arr[i]==0)cnt++;
            if (cnt==f.length) return i;
        }
        return -1;
    }
    public static int[] f(int[] d){
        TreeSet<Integer> set=new TreeSet<>();
        for (int i = 0; i < d.length; i++) {
            set.add(d[i]);
        }
        int[] arr=new int[set.size()];
        int cnt=0;
        for (int i:set){
            arr[cnt]=i;
            cnt++;
        }
        return arr;
    }

This problem is to sort the array first, so it is easier to find the smallest element, and traverse the array. If the current number is the same as the previous number, skip this loop. Otherwise, judge whether the current number can divide all the numbers in the array d. If it can be returned directly The index value of the current number, if no matching number is found after traversing, -1 will be returned. Here I first deduplicate and sort the d array to improve efficiency!

Summary: The difficulty of this weekly competition is moderate, and the types of questions are similar

Guess you like

Origin blog.csdn.net/TerryProgrammer/article/details/125831240