We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Practice
  • Certification
  • Compete
  • Career Fair
  • Hiring developers?
  1. Practice
  2. Data Structures
  3. Arrays
  4. Array Manipulation
  5. Discussions

Array Manipulation

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • tomade 4 years ago+ 0 comments

    Yes, that makes more sense and saves on memory and running time. Using a defaultdict in Python:

    from collections import defaultdict
    
    sums = defaultdict(int)
    _, n_ranges = (int(x) for x in input().split())
    
    for _ in range(n_ranges):
        start, stop, value = (int(x) for x in input().split())
        sums[start] += value
        sums[stop+1] -= value
    
    max_val = running_val = 0
    for _, val in sorted(sums.items()):
        running_val += val
        max_val = max(max_val, running_val)
    
    print(max_val)
    
    1|
    ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature