Sort by

recency

|

2485 Discussions

|

  • + 0 comments

    public static long arrayManipulation(int n, List> queries) { long[] arr = new long[n]; long sum = 0; long max = 0;

        for(int i=0; i < queries.Count; i++)
        {
            int a = queries[i][0] - 1;
            int b = queries[i][1] - 1;
            int k = queries[i][2];
    
            for(int col=a;col<=b;col++)
            {
                arr[col] += k;
                max = Math.Max(max,arr[col]);
            }
        }
    
        return max;
    }
    
  • + 0 comments

    public static long arrayManipulation(int n, List> queries) { long max=0; List> arr = new List>();

        List<long> ft = Enumerable.Repeat((long)0, n).ToList();        
    
        arr.Add(ft);
    
        for(int row=0;row<queries.Count;row++)
        {
            arr.Add(ft);
    
            for(int col=0;col<n;col++)
            {   
                var a=queries[row][0]-1;
                var b=queries[row][1]-1;
                var k=queries[row][2];
    
                if(col>=a && col<=b)
                {
                    long calc=arr[row][col]+k;
                    arr[row+1][col]=calc;
                    max=calc>max?calc:max;
                }
                else{
                    arr[row+1][col]=arr[row][col];
                }
            }
    
            Console.WriteLine(string.Join(" ",arr[row+1]));
        }
    
        return max;
    }
    

    }

  • + 0 comments

    Here's a solution in C#

    public static long arrayManipulation(int n, List<List<int>> queries)
        {       
            long[] arr = new long[n];
            long sum = 0;
            long max = 0;
            
            for(int i=0; i < queries.Count; i++){
                int a = queries[i][0] - 1;
                int b = queries[i][1] - 1;
                int k = queries[i][2];
                
                arr[a] += k;
                
                if(b+1 < n)
                    arr[b+1] -= k;
                    
            }
            
            for (int j = 0; j < arr.Length; j++){
                sum += arr[j];
                max = Math.Max(max,sum);
            }
            
            return max;
        }
    
  • + 0 comments

    Here's a solution in c# , but it doesn't pass the Test Case 7-12 due to more executing time . It shows error "Time Limit Exceed"

    public static long arrayManipulation(int n, List<List<int>> queries)
        {       
            long[] arr = new long[n+1];
            
            for(int i=0; i < queries.Count; i++){
                int a = queries[i][0];
                int b = queries[i][1];
                int k = queries[i][2];
                
                for(int j = a; j <= b; j++){
                    arr[j] += k;
                }
            }
            
            return arr.Max();
        }
    
  • + 0 comments

    My approach in JavaScript :-)

    function arrayManipulation(n, queries) {
        let diff = new Array(n + 1).fill(0);
    
        for (let i = 0; i < queries.length; i++) {
            // destructure the array
            let [a, b, k] = queries[i];
            
            // do the prefix sum
            diff[a - 1] += k;
            diff[b] -= k;    
        }
        
        let sum = 0;
        let max = -Infinity;
            
        for (let j = 0; j < n; j++) {
            sum += diff[j];
            
            if (sum > max) {
                max = sum;
            }
        }
        return max;
    }