Skip to content

Instantly share code, notes, and snippets.

@Roshankumar350
Last active February 17, 2021 17:31
Show Gist options
  • Save Roshankumar350/119352989cbe7f8513bcea456b9768fc to your computer and use it in GitHub Desktop.
Save Roshankumar350/119352989cbe7f8513bcea456b9768fc to your computer and use it in GitHub Desktop.
Cost of balloons : HackerEarth
// Write your code here
func solve() {
// know the cost of balloons
let costOfBallons = getArray(readLine()!)
// know the number of participants
let numberOfParticipant = Int(readLine()!)!
// some holder to capture who has solved the problem
var firstParticipantSolved = 0
var secondParticipantSolved = 0
// loop over number of articipant
for _ in 1...numberOfParticipant {
// status of problem solved by each participant
let status = getArray(readLine()!)
let firstStatus = status[0]
let secondStatus = status[1]
switch (firstStatus, secondStatus) {
case (1,1) :
firstParticipantSolved += 1
secondParticipantSolved += 1
case (1,0):
firstParticipantSolved += 1
case (0,1):
secondParticipantSolved += 1
default:
firstParticipantSolved += 0
secondParticipantSolved += 0
}
}
let costOne = (firstParticipantSolved * costOfBallons[0]) + (secondParticipantSolved * costOfBallons[1])
let costTwo = (secondParticipantSolved * costOfBallons[0]) + (firstParticipantSolved * costOfBallons[1])
print(min(costOne,costTwo))
}
// get the array of values from string
func getArray(_ str: String) -> [Int] {
let numbers = str.split(separator: " ").map{ Int($0) }.compactMap{ $0 }
return numbers
}
// Read the testCases
let testCases = Int(readLine()!)!
// Loop over testCases
for _ in 1...testCases {
solve()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment