Which part wastes the most of the running time ?


  • 0
    H
    public NumArray{
        int[] numsarray;
        public NumArray(int[] nums) {
            this.numsarray = nums;
        }
    
        void update(int i, int val) {
            numsarray[i] = val;
        }
    
        public int sumRange(int i, int j) {
            int val = 0;
            for(int k = i; k <= j; k++){
                val += numsarray[k];
            }
            return val;
        }
    }
    

    Hi everyone, my dump code was not accepted cause time limitation exceeded. I want to know which part wastes the most of the running time? Is this problem only wants to test us how to optimize our program instead of logical?


  • 0
    S

    Your code ran slow, since the number of calls to update and sumRange function is distributed evenly. This discussion may useful, or just read this article.


  • 0
    H

    Thank you very much ! Guys like you make LeetCode better & better !!! :):):)


Log in to reply
 

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