Java Solution:runtime beats 99.79 % of java submissions.


  • 0
    0
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    /**
     * Created by ZYC on 2017/5/16.
     */
    public class Solution {
        public int leastBricks(List<List<Integer>> wall) {
            List<Integer> intervals = new ArrayList<>();
            for (int i = 0; i < wall.size(); i++) {
                List<Integer> bricks = wall.get(i);
                int inter = 0;
                for (int j = 0; j < bricks.size() - 1; j++) {
                    inter += bricks.get(j);
                    intervals.add(inter);
                }
            }
            int[] intervalsArr = new int[intervals.size()];
            for (int i = 0; i < intervalsArr.length; i++) {
                intervalsArr[i] = intervals.get(i);
            }
            Arrays.sort(intervalsArr);
            int maxNum = 0;
            for (int i = 0; i < intervalsArr.length; ) {
                int curCnt = 1;
                int pos = i + 1;
                while (pos != intervalsArr.length && intervalsArr[pos] == intervalsArr[i]) {
                    curCnt++;
                    pos++;
                }
                if (curCnt > maxNum) {
                    maxNum = curCnt;
                }
                i = pos;
            }
            return wall.size() - maxNum;
        }
    }
    

Log in to reply
 

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