Last active
February 17, 2021 17:31
-
-
Save Roshankumar350/119352989cbe7f8513bcea456b9768fc to your computer and use it in GitHub Desktop.
Cost of balloons : HackerEarth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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