Time Limit Exceeded Error


  • 0
    B

    Hi
    i have problem with this question.
    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
    The update(i, val) function modifies nums by updating the element at index i to val.

    Example:
    Given nums = [1, 3, 5]
    sumRange(0, 2) -> 9
    update(1, 2)
    sumRange(0, 2) -> 8

    my solution :
    public class NumArray {
    int[] nums;

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

    }

    anyone has any idea? thanks in advance


  • 0
    X

    @boshrahashemi you should solve the Problem 303 Range Sum Query - Immutable first, because there are many calls to sumRange function. so you should think some method to solve the problem


Log in to reply
 

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