Advertisement
Falexom

Untitled

Nov 15th, 2023
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. class Solution:
  2.     def countNegatives(self, grid):
  3.         result = 0
  4.         #   temp = []
  5.  
  6.         for row in grid:
  7.             if len(row) > 1:
  8.                 row = sorted(row)
  9.                 left = 0
  10.                 right = len(row)-1
  11.             else:
  12.                 if row[0] < 0:
  13.                     result += 1
  14.                 continue
  15.             iterations = 0
  16.             #   print(result)
  17.             while left < right:
  18.                 if row[left] == row[right] and iterations == 0:
  19.                     if row[right] < 0:
  20.                         result += len(row)
  21.                     break
  22.                 if row[left] < 0 and row[right] < 0 and iterations == 0:
  23.                     result += len(row)
  24.                     break
  25.                 iterations = 1
  26.                 #   print(left, right)
  27.                 if row[right] >= 0 and row[left] >= 0:
  28.                     temp_row_left = left
  29.                     temp_row_right = right
  30.                     left += 1
  31.                     right -= 1
  32.                     if left == temp_row_right and right == temp_row_left:
  33.                         break
  34.                 if row[left] < 0:
  35.                     #   temp.append((row, row[left]))
  36.                     result += 1
  37.                     left += 1
  38.                 if row[right] < 0:
  39.                     #   temp.append((row, row[right]))
  40.                     right -= 1
  41.                     result += 1
  42.  
  43.         return result
  44.  
  45.  
  46. s = Solution()
  47. print(s.countNegatives([[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]))
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement