Skip to content

Instantly share code, notes, and snippets.

@Roshankumar350
Created January 17, 2021 15:58
Show Gist options
  • Save Roshankumar350/10408a6ca9798d9c5c7ebda8d0e19817 to your computer and use it in GitHub Desktop.
Save Roshankumar350/10408a6ca9798d9c5c7ebda8d0e19817 to your computer and use it in GitHub Desktop.
// Problem: https://www.pramp.com/challenge/2WBx3Axln1t7JQ2jQq96
func findBusiestPeriod(data: [[Int]]) -> Int {
var timeStamp = 0
var busiest = -1
var currentBusiest = 0
let length = data.count
for (index, eachArray) in data.enumerated() {
let state = eachArray[2]
let visitors = eachArray[1]
let currentTimeStamp = eachArray[0]
if state == 1 {
currentBusiest += visitors
} else {
currentBusiest -= visitors
}
if (index < length - 1) && currentTimeStamp == data[index + 1][0] {
continue
}
if currentBusiest > busiest {
busiest = currentBusiest
timeStamp = currentTimeStamp
}
}
return timeStamp
}
/*
input: data = [ [1487799425, 14, 1],
[1487799425, 4, 0],
[1487799425, 2, 0],
[1487800378, 10, 1],
[1487801478, 18, 0],
[1487801478, 18, 1],
[1487901013, 1, 0],
[1487901211, 7, 1],
[1487901211, 7, 0] ]
output: 1487800378 # since the increase in the number of people
# in the mall is the highest at that point
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment