Java easy HashMap, find the location with most edges


  • 0

    The max number of bricks to cross is the number of rows, whenever we see a edge, the number of crossed bricks reduce by 1. Thus we just need keep track of the location where we see maximum number of edges.

        public int leastBricks(List<List<Integer>> wall) {
            HashMap<Integer, Integer> map = new HashMap<>();
            int max = 0;
            for (int i = 0; i < wall.size(); i++) {
                List<Integer> row = wall.get(i);
                int edge = 0;
                for (int j = 0; j < row.size()-1; j++) {
                    edge += row.get(j);
                    int count = map.getOrDefault(edge, 0)+1;
                    map.put(edge, count);
                    max = Math.max(max, count);
                }
            }
            return wall.size()-max;
        }
    

    Using buckets to store all the x coordinates leads to MLE, which is intentionally designed by OJ.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.