Why Time Limit Exceeded?


  • 0
    N
    public class NumMatrix {
        private int[][] matrix;
    public NumMatrix(int[][] matrix) {
        this.matrix = matrix;
    }
    
    public int sumRegion(int row1, int col1, int row2, int col2) {
            int result = 0;
            for(int i = row1; i <= row2; i++)
            {
                for(int j = col1; j <= col2; j++)
                {
                    result += matrix[i][j];
                }
            }
            return result;
    }
    

    }


  • 0
    F

    The time complexity of your sumRegion is O(n^2) in worst case. So, with many such queries, the total time of running them will be large


Log in to reply
 

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