Sort by

recency

|

2459 Discussions

|

  • + 0 comments

    ?

  • + 0 comments

    Array manipulation involves modifying, adding, or removing elements in an array to structure data effectively. It’s essential for optimizing code performance, especially with large datasets. Techniques like slicing and mapping are widely used in programming. Learn more about related topics like how to activate Glo SIM.

  • + 1 comment

    This is my elegant solution in java with 1-indexed lists:

    public static long arrayManipulation(int n, List<List<Integer>> queries) {
            // 1-indexed array
            List<Long> arr = new ArrayList<>() {
                {
                    add(Long.MIN_VALUE);
                    addAll(Collections.nCopies(n, 0L));
                }
            };
            for (List<Integer> query : queries) {
                Integer a = query.get(0);
                Integer b = query.get(1);
                Integer k = query.get(2);
                arr.set(a, arr.get(a) + k);
                if(b < n) {
                    arr.set(b + 1, arr.get(b + 1) - k);
                }
            }
            long answer = 0, sum = 0;
            for(int i = 1; i <= n; i++) {
                sum += arr.get(i);
                answer = Math.max(answer, sum);
            }
            return answer;
        }
    
  • + 0 comments

    Hi, I've seen many use the prefix sum algorithm to solve this problem, before looking into it I was trying with this:

        arr = [0] * n
        for start, end, value in queries:
            sub_arr = arr[start - 1:end]
            add_arr = [value] * len(sub_arr)
            sub_arr = [a+b for a,b in zip(sub_arr, add_arr)]
            arr = arr[0:start - 1] + sub_arr + arr[end:]
        return max(arr)
    

    but it says the max number is not the expected one, at the end, seems like with sum prefix we can recreate the same array I generate each query so I cannot see why the returned max value is not correct, if anyone has an answer, I would be glad to reaad i it, thanks.

  • + 0 comments

    Python Code Implementation Difference Array Instead Brute-Force Method

    `

    def arrayManipulation(n, queries): arr = [0] * (n + 1)
    for a, b, k in queries: arr[a - 1] += k if b < n: arr[b] -= k max_value = 0 current_value = 0 for i in range(n): current_value += arr[i] if current_value > max_value: max_value = current_value

    return max_value
    
    fptr.write(str(result) + '\n')
    fptr.close()
    

    `