20200922: Leetcode 35-week bi-weekly contest problem solution record (part 2)

Leetcode 35-week bi-weekly contest problem solution record (below)

topic

3.1590. Make array sum divisible by P
Insert picture description here
4.1591. Strange printer II
Insert picture description here

Ideas and algorithms

  1. The third question, prefixes and derivative questions, requires a prefix knowledge of the congruence theorem, not much bb, the rest is prefixes and adding some simple processing skills.
  2. The fourth question, the coloring problem, will not be repeated, see the code

Code

3.1590. Make array sum divisible by P

class Solution {
    
    
    public int minSubarray(int[] nums, int p) {
    
    
        int n = nums.length;
        Map<Integer, Integer> hashmap = new HashMap<>();
        hashmap.put(0,0);
        int[] presum = new int[n+1];
        int sum = 0;
        int ans = Integer.MAX_VALUE;    
        for (int i = 0; i < n ; i++){
    
    
            int now = nums[i];
            sum = (int)((long)(sum+now)%p);
            presum[i+1] = (int)((long)(presum[i]+now)%p);
        }
        int res = sum%p;

        for (int i = 1; i<=n ;i++){
    
    
            int cur = presum[i];
            hashmap.put(cur,i);
            int target = (int)((long)(cur-res+p)%p);
            if (hashmap.containsKey(target)){
    
    
                ans = Math.min(ans, i-hashmap.get(target));
            }
        }
        if (ans == Integer.MAX_VALUE || ans == n)return -1;
        return ans;
    }
}

4.1591. Strange Printer II

class Solution {
    
    
    class Node {
    
    
        int left;
        int up;
        int right;
        int bottom;

        Node(int a, int b, int c, int d) {
    
    
            left = a;
            up = b;
            right = c;
            bottom = d;
        }
    }

    public boolean isPrintable(int[][] targetGrid) {
    
    
        Map<Integer, Node> colorPos = new HashMap<>();
        // 找到每种color的左上右下角
        int m = targetGrid.length;
        int n = targetGrid[0].length;
        for (int i = 0; i < m; i++) {
    
    
            for (int j = 0; j < n; j++) {
    
    
                int color = targetGrid[i][j];
                if (colorPos.containsKey(color)) {
    
    
                    Node node = colorPos.get(color);
                    node.bottom = Math.max(node.bottom, i);
                    node.up = Math.min(node.up, i);
                    node.left = Math.min(node.left, j);
                    node.right = Math.max(node.right, j);
                } else {
    
    
                    colorPos.put(color, new Node(j, i, j, i));
                }
            }
        }
        Set<Integer> colors = new HashSet<>(colorPos.keySet());
        while (!colors.isEmpty()) {
    
    
            // 找到能删除的颜色,删除该颜色
            Set<Integer> deletion = new HashSet<>();  
            // 边forloop 一个set边删除,会有ConcurrentModificationException
             for (int c : colors) {
    
    
                if (isOneColor(targetGrid, colorPos.get(c), c)) {
    
    
                    deletion.add(c);
                 }
            }
            if (deletion.isEmpty()) {
    
    
                return false;
            }
            colors.removeAll(deletion);
        }
        return true;
    }
    
    private boolean isOneColor(int[][] targetGrid, Node boundary, int color) {
    
    
        // 检查边界长方形能不能完全删除
        for (int i = boundary.up; i <= boundary.bottom; i++) {
    
    
            for (int j = boundary.left; j <= boundary.right; j++) {
    
    
                if (targetGrid[i][j] > 0 && targetGrid[i][j] != color) {
    
    
                    return false;
                }
            }
        }
        // 把这种color清空为0
        for (int i = boundary.up; i <= boundary.bottom; i++) {
    
    
            for (int j = boundary.left; j <= boundary.right; j++) {
    
    
                targetGrid[i][j] = 0;
            }
        }
        return true;
    }
}

Complexity analysis

Guess you like

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