Why my code is time limit exceeded

  • 1
    public class NumArray {
        int[] nums;
        public NumArray(int[] nums) {
            this.nums = nums;   
        public int sumRange(int i, int j) {
            int ret = 0;
            for(int k=i;k<=j;k++){
                ret +=nums[k];
            return ret;

  • 3

    hint: try to solve it in O(1) in every query. maybe a precompute will help.

Log in to reply

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