Array Manipulation

Sort by

recency

|

28 Discussions

|

  • + 0 comments

    C++ solution:

    long arrayManipulation(int n, vector<vector<int>> queries) {
        vector<long> arr(n+2);
        
        for (const auto& q : queries) {
            int a=q[0], b=q[1], k=q[2];
            arr[a] += k;
            arr[b+1] -= k;
        }
        
        partial_sum(begin(arr), end(arr), begin(arr));
        return *max_element(begin(arr), end(arr));
    }
    
  • + 0 comments

    Can someone rewrite the problem definition, it's terribly written...

  • + 0 comments

    How was I every supposed to come up with that? ;-;

  • + 0 comments

    Java 8 - This solution does require a sort and then a full pass through the queries, but the memory overhead is less and still meet so the time requirments.

        public static long arrayManipulation(int n, List<List<Integer>> queries) {
            long result = Integer.MIN_VALUE;
            
            queries.sort((q1,q2)->Integer.compare(q1.get(0), q2.get(0)));
            
            PriorityQueue<List<Integer>> expireQueue = new PriorityQueue<>(
                (q1,q2)->Integer.compare(q1.get(1), q2.get(1))
            ) ;
            long runningTotal = 0;
            
            for (List<Integer> query: queries) {
                int idx = query.get(0);            
                expireQueue.offer(query);
                
                runningTotal += query.get(2);
                
                while(expireQueue.peek().get(1)<idx) 
                    runningTotal -= expireQueue.poll().get(2) ;
                
                if (runningTotal>result)
                    result = runningTotal;
            }
            return result ;
        }
    
  • + 0 comments

    here is hackerrank array manipulation problem solution in Python Java c++ c and javascript