Simple solution java8, with hashmap and lambda


  • 0
    R

    I wanted to share this solution

    public class Solution {
        private Integer i=0,max=0;
        public int leastBricks(List<List<Integer>> wall) {
            Map<Integer,Integer> res = new HashMap<Integer,Integer>();
            wall.forEach((e)->{
                i=0;
                e.forEach((l)->{
                    i+=l;
                    res.putIfAbsent(i, 0);
                    res.replace(i, res.get(i)+1);
                }); 
                res.remove(i);
            });
            res.forEach((k,v)->{max=(max<v?v:max);});
            return wall.size()-max;
        }
    }
    

    i think that could be possible calculate max inside the innested foreach but i have to try


Log in to reply
 

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