C++20 sections · 1024 units
Open in Course

Dragons - Implementation

Complete solution


plaintext
function defeat_dragons(s, dragons)
 // dragons is vector of pairs (strength, bonus)
 sort(dragons) // sort by first element (strength)
 current_strength := s
 for each dragon in dragons
 if current_strength > dragon.strength then
 current_strength := current_strength + dragon.bonus
 else
 return "NO"
 return "YES"

The time complexity is O(nlogn)O(n \log n) for sorting, where nn is the number of dragons.