【Leetcode】356. Line Reflection

Subject address:

https://leetcode.com/problems/line-reflection/

Given a two-dimensional plane nnFor n points, ask whether there is a vertical straight line, so that after these points are mirror-reflected on this straight line, the obtained points coincide with the previous ones. If there are two points in the same position, we regard them as one point.

Find xx firstThe points with the largest and smallest x coordinates, letxxThe x coordinates arex 1 x_1x1And x 2 x_2x2, Then if they can coincide, the central axis of the mirror reflection must be x = x 1 + x 2 2 x=\frac{x_1+x_2}{2}x=2x1+x2, That is, if two points yyThe y coordinates are the same, they are symmetrical about this central axis if and only if theirxxThe sum of x coordinates isx 1 + x 2 x_1+x_2x1+x2. We can open a hash table, the key is yyy coordinate, value isyyas keyxxof all points in y coordinateA list of x coordinates, traverse all points and store them in the hash table, and then traverse the hash table, each time the traversal, sort the list of values, and then use the colliding double pointer to determine whetherxxThe sum of x coordinates is exactlyx 1 + x 2 x_1+x_2x1+x2That's it. Pay attention to skip the repeated points. code show as below:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution {
    
    
    public boolean isReflected(int[][] points) {
    
    
        if (points.length == 0) {
    
    
            return true;
        }
        
        int minX = Integer.MAX_VALUE, maxX = Integer.MIN_VALUE;
        for (int[] point : points) {
    
    
            minX = Math.min(minX, point[0]);
            maxX = Math.max(maxX, point[0]);
        }
        
        int sumX = minX + maxX;
        Map<Integer, List<Integer>> map = new HashMap<>();
        for (int[] point : points) {
    
    
            map.putIfAbsent(point[1], new ArrayList<>());
            map.get(point[1]).add(point[0]);
        }
        
        for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) {
    
    
            List<Integer> list = entry.getValue();
            list.sort(null);
            for (int i = 0, j = list.size() - 1; i <= j; i++, j--) {
    
    
            	// 从左向右找到重复点中的最后一个
                while (i + 1 < list.size() && list.get(i) == list.get(i + 1)) {
    
    
                    i++;
                }
                
                // 从右向左找到重复点中的最后一个
                while (j > 0 && list.get(j) == list.get(j - 1)) {
    
    
                    j--;
                }
                
                if (list.get(i) + list.get(j) != sumX) {
    
    
                    return false;
                }
            }
        }
        
        return true;
    }
}

Time complexity O (∑ xi log ⁡ xi) O(\sum x_i\log x_i)O ( xilogxi) x i x_i xiFor yyThe number of points with the same y , spaceO (n) O(n)O ( n )

Guess you like

Origin blog.csdn.net/qq_46105170/article/details/112558627