Boss Fight in Python

PythonServer Side ProgrammingProgramming

Suppose we have a binary list called fighters and another list of binary lists called bosses. In fighters list the 1 is representing a fighter. Similarly, in bosses list 1 representing a boss. That fighters can beat a boss’s row if it contains more fighters than bosses. We have to return a new bosses matrix with defeated boss rows removed.

So, if the input is like fighters = [0,1,1]

011
000
001
011
111

then the output will be

011
111

To solve this, we will follow these steps −

  • fighter_cnt := sum of all elements of fighters

  • result := a new list

  • for each row in bosses, do

    • if fighter_cnt <= sum of each element in row, then

      • insert row at the end of result

  • return result

Let us see the following implementation to get better understanding −

Example

 Live Demo

class Solution:
   def solve(self, fighters, bosses):
      fighter_cnt = sum(fighters)
      result = []
      for row in bosses:
         if fighter_cnt <= sum(row):
            result.append(row)
      return result
ob = Solution()
fighters = [0, 1, 1]
bosses = [[0, 0, 0], [0, 0, 1], [0, 1, 1], [1, 1, 1]]
print(ob.solve(fighters, bosses))

Input

[0, 1, 1], [[0, 0, 0], [0, 0, 1], [0, 1, 1], [1, 1, 1]]

Output

[[0, 1, 1], [1, 1, 1]]
raja
Published on 02-Sep-2020 16:44:30
Advertisements