1 minute read

3046. Split the Array

Solution

class Solution {
    public boolean isPossibleToSplit(int[] nums) {
        // calculate each element counts, 
        // if more than 2, then false.
        
        if (nums.length % 2 != 0) {
            return false;
        }
        
        Map<Integer, Integer> map = new HashMap<>();
        for(int num: nums) {
            if (!map.containsKey(num)) {
                map.put(num, 0);
            }
            
            if (map.get(num) == 2) {
                return false;
            }
            
            map.put(num, map.get(num) + 1);
           
        }
        
        return true;
        
    }
}

3047. Find the Largest Area of Square Inside Two Rectangles

class Solution {
    public long largestSquareArea(int[][] bottomLeft, int[][] topRight) {
        long max = 0;
        long res = 0;
        for (int i = 0; i < bottomLeft.length; i++) {
            for (int j = i + 1; j < bottomLeft.length; j++) {
                res = getIntersect(bottomLeft, topRight, i, j);
                if (res > max) {
                    max = res;
                }
            }
        }
        
        return max;
    }
    
    private long getIntersect(int[][] bottomLeft, int[][] topRight, int i, int j) {
        int x = 0, y = 1;
        
        long righterLeft = bottomLeft[i][x] > bottomLeft[j][x] ? bottomLeft[i][x] : bottomLeft[j][x];
        long lefterRight = topRight[i][x] < topRight[j][x] ? topRight[i][x] : topRight[j][x];
        
        if (lefterRight <= righterLeft) {
            return 0;
        }
        
        long xIntersect = lefterRight - righterLeft;
        
        long lowerUp = topRight[i][y] < topRight[j][y] ? topRight[i][y] : topRight[j][y];
        long upperBottom = bottomLeft[i][y] > bottomLeft[j][y] ? bottomLeft[i][y] : bottomLeft[j][y];
        
        if (lowerUp <= upperBottom) {
            return 0;
        }
        
        long yIntersect = lowerUp - upperBottom;
        
        long side = Math.min(yIntersect, xIntersect);
        
        return side * side;
    }
}