20200827: 2020 Likou Week 33 Biweekly Contest Solution

Topic One

Topic 1: Thousand Separator Number

Give you an integer n, please add a dot (ie "." symbol) every third place as the thousands separator, and return the result in string format.

Example

Example 1:

Input: n = 987
Output: "987"

Example 2:

Input: n = 1234
Output: "1.234"

Example 3:

Input: n = 123456789
Output: "123.456.789"

Problem-solving ideas and code implementation

This question can be directly simulated. If you use the stack to implement it, it is best to start from the end, because the number we get is not necessarily a multiple of 3.

class Solution {
    
    
    public String thousandSeparator(int n) {
    
    
        String m = String.valueOf(n);
        int len = m.length();
        StringBuilder sb = new StringBuilder();
        int cnt = 0;
        for (int i = len - 1; i >= 0; i--) {
    
    
            sb.append(m.charAt(i));
            cnt++;
            if (cnt % 3 == 0 && i != 0) {
    
    
                sb.append(".");
            }
        }
        return sb.reverse().toString();
    }
}

Topic two

Problem 2: The minimum number of points that can reach all points

Give you a directed acyclic graph with n nodes numbered from 0 to n-1, and an edge array edges, where edges[i] = [fromi, toi] represents a directed edge from point fromi to point toi.

Find the smallest point set so that all points in the graph can be reached from these points. The problem guarantees that the solution exists and is unique.

You can return these node numbers in any order.

Example

Insert picture description here
Insert picture description here

Problem-solving ideas and code implementation

The meaning of this question is to get the union of all points whose current in-degree is 0.

class Solution {
    
    
    public List<Integer> findSmallestSetOfVertices(int n, List<List<Integer>> edges) {
    
    
        // 统计入度为0的点即可
        List<Integer> ans = new ArrayList<Integer>();
        int[] inDegree = new int[n];
        for (List<Integer> edge : edges) {
    
    
            inDegree[edge.get(1)]++;
        }

        for (int i = 0; i < n; i++) {
    
    
            if (inDegree[i] == 0) {
    
    
                ans.add(i);
            }
        }
        return ans;
    }

}

Topic Three

Topic 3: Get the minimum number of function calls to the target array
Insert picture description here

Give you an array arr with the same size as nums and the initial value is all 0, please call the above function to get the integer array nums.

Please return the minimum number of function calls that change arr to nums.

The answer is guaranteed to be within a 32-bit signed integer.
Insert picture description here

Problem-solving ideas and code implementation

class Solution {
    
    
    
    int oddCnt = 0;
    int evenCnt = 0;
    
    public int minOperations(int[] nums) {
    
    
        
        int ans = 0;
        for (int num : nums) {
    
    
            numCnt(num);
        }
        ans = oddCnt + evenCnt;
        return ans;
    }

    private void numCnt(int n){
    
    
        int cnt = 0;
        while (n > 0) {
    
    
            if (n % 2 == 1) {
    
    
                oddCnt++;
                n--;
            }
            else {
    
    
                cnt++;
                n /= 2;
            }
        }
        evenCnt = Math.max(evenCnt,cnt);
    }
}

Topic Four

Topic 4: Detecting the ring in a two-dimensional grid

Give you a two-dimensional character grid array grid, the size is mxn, you need to check whether there is a ring formed by the same value in the grid.

A ring is a path whose length is greater than or equal to 4 starting and ending in the same grid. For a given grid, you can move to one of the adjacent grids in the four directions up, down, left, and right. You can move on the premise that the two grids have the same value.

At the same time, you cannot go back to the grid where you were last moved. For example, the ring (1, 1) -> (1, 2) -> (1, 1) is illegal, because moving from (1, 2) to (1, 1) returns to the grid at the last move .

If there is a ring formed by the same value in the grid, please return true, otherwise return false.

Example

Insert picture description here
Insert picture description here

Problem-solving ideas and code implementation

This question dfs can't be done for a long time, refer to the solution of the big guy @xiaoxi666, the following is attached and collected.

class Solution {
    
    
                        
    public boolean containsCycle(char[][] grid) {
    
    
        int h = grid.length;
        int w = grid[0].length;
        DSU dsu = new DSU(w * h);
        for (int i = 0; i < h; ++i) {
    
    
            for (int j = 0; j < w; ++j) {
    
    
                char cur = grid[i][j];
                // 向右搜索
                if (j + 1 < w && cur == grid[i][j + 1]) {
    
    
                    if (dsu.union(i * w + j, i * w + j + 1)) {
    
    
                        return true;
                    }
                }
                // 向下搜索
                if (i + 1 < h && cur == grid[i + 1][j]) {
    
    
                    if (dsu.union(i * w + j, (i + 1) * w + j)) {
    
    
                        return true;
                    }
                }
            }
        }
        return false;
    }


    class DSU {
    
    
        int[] parent;

        public DSU(int N) {
    
    
            parent = new int[N];
            for (int i = 0; i < N; ++i) {
    
    
                parent[i] = i;
            }
        }

        public int find(int x) {
    
    
            if (parent[x] != x) {
    
    
                parent[x] = find(parent[x]);
            }
            return parent[x];
        }

        /**
         * 若合并前,x和y的parent相同,则表示形成环,返回true。
         *
         * @param x
         * @param y
         * @return
         */
        public boolean union(int x, int y) {
    
    
            int parentX = find(x);
            int parentY = find(y);
            if (parentX == parentY) {
    
    
                return true;
            }
            if (parentX < parentY) {
    
    
                parent[parentY] = parentX;
            } else {
    
    
                parent[parentX] = parentY;
            }
            return false;
        }
    }
}



Write at the end

Everything is very uncertain, but persisting in learning is a must, come on!

Guess you like

Origin blog.csdn.net/qq_36828395/article/details/108270961