Problem Statement:https://www.hackerrank.com/challenges/crush/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=arrays

Sample Code : Solution 1, Getting TLE

n,m=map(int,input().split())
a=[0]*n
for _ in range(m):
    f,l,val=[int(n) for n in input().split()]
    for i in range(f-1,l):
        a[i]+=val
print(max(a))  

Sample Code: Solution 2, Efficient one

n,m=map(int,input().split())
a=[0]*(n+1)
m_sum=temp=0
for _ in range(m):
    f,l,val=[int(n) for n in input().split()]
    a[f-1]+=val
    if l<=n:
        a[l]-=val
for i in a:
    temp+=i
    if m_sum<temp:
        m_sum=temp
print(m_sum)  

Explanation:

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top