Sort by

recency

|

2451 Discussions

|

  • + 0 comments

    /* Any ideas of why my code is leading up to a large execution time. Thank you in advance */

    function arrayManipulation(n, queries) {

    if (n > 10 ** 7) {
        n = 10 ** 7
    };
    
    if (queries.length > 2 * (10 ** 5)){
        queries.length = 2 * (10 ** 5)
    };
    
    
    let array = [];
    array.length = n
    // value/ start / end
    array.fill(0, 0, n)
    
    let result = 0;
    
    
        queries.forEach((e)=> {
             let a = e[0] - 1; // queries[y][0]
             let b = e[1] - 1;  // queries[y][1]
             let k = e[2];      // queries[y][2]
    
    
              for (let i = a; i <= b; i++) {
                // add k to the values between array[i] && array[i]
                array[i] = array[i] + k;
    
    
    
            }    
    
    
        })
    
    
    result = Math.max(...array)
    
    return result
    

    }

  • + 0 comments

    Python code, All Test Case Passed

    def arrayManipulation(n, queries):
        arr = [0]*(n+1)
        for (start, end, value) in queries:
            arr[start-1] += value
            arr[end] -= value
    
        itsum = 0
        mnum = 0
        for i in range(n):
            itsum += arr[i]
            if mnum < itsum:
                mnum = itsum
             
        return mnum
    
  • + 0 comments

    Only consider the query indices.

    def arrayManipulation(n, queries):
        # Write your code here
        sweepline = {}
    
        # O(N)
        for (start, end, value) in queries:
            sweepline[start] = sweepline.get(start, 0) + value
            sweepline[end + 1] = sweepline.get(end + 1, 0) - value
    
        maxnum = float("-inf")
        curr = 0 
        
        for index in sorted(sweepline.keys()):
            curr += sweepline[index]
            maxnum = max(maxnum, curr)
        
        return maxnum
    
  • + 1 comment

    function arrayManipulation(n: number, queries: number[][]): number {

    let arr: number[] = Array(n+1).fill(0)
    let max = 0;
    
    queries.forEach((el)=>{
        let a = el[0]
        let b = el[1]
        let k = el[2]
    
        arr[a-1] = arr[a-1] + k
        arr[b] = arr[b] - k
    })
    let sum = 0;
    for (let i = 0; i< arr.length-1; i++){
        sum = sum + arr[i]
        sum > max ? max = sum : null
    }
    return max
    

    }

  • + 0 comments
        public static long arrayManipulation(int n, List<List<Integer>> queries) {
            long[] array = new long[n+2];
            for(int i = 0; i < queries.size(); i++) {
                long numberToAdd = queries.get(i).get(2);
                int startIndex = queries.get(i).get(0);
                int endIndex = queries.get(i).get(1);
                
                array[startIndex] += numberToAdd;     
                array[endIndex+1] -= numberToAdd;
            }
            long max = 0;
            for(int i = 1; i < array.length; i++) {
                array[i] += array[i-1];
                max = Math.max(array[i],max);
            }
            return max;
        }