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.
Someone's probably mentioned, but to avoid using O(n) space, you can use a sparse data structure like a map.
importqualifiedData.Map.StrictasMimportData.List(foldl')main=dogetLine-- skip the first line, we won't use N or Minput<-mapreadWords.lines<$>getContents-- lazily read operationsletops=foldl'insertOperationM.emptyinputprint$findMaxopsreadWords=mapread.wordsinsertOperationm[a,b,k]=(M.insertWith(+)ak).(M.insertWith(+)(b+1)(-k))$mfindMax::M.MapIntInt->Int-- Result is >= 0findMaxm=snd$M.foldl'update(0,0)mwhereupdate=\(cur,peak)x->letnext=cur+xin(next,maxpeaknext)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Array Manipulation
You are viewing a single comment's thread. Return to all comments →
Someone's probably mentioned, but to avoid using O(n) space, you can use a sparse data structure like a map.