algorithms

Algorithm implementations
git clone git://git.laack.co/algorithms.git
Log | Files | Refs | README

count-good-cyclic-rotations.py (693B)


      1 class Solution:
      2     def countGoodRotations(self, nums: list[int]) -> int:
      3         sc = 0
      4         bound = len(nums) // 2
      5         nl = len(nums)
      6         
      7         delta = 0
      8         
      9         for idx in range(0,len(nums)):
     10             new_pos = ((idx) + nl) % nl
     11             if new_pos < bound:
     12                 delta += nums[idx]
     13             else:
     14                 delta -= nums[idx]
     15 
     16         if delta > 0:
     17             sc += 1
     18 
     19         for pivot in range(1,len(nums)):
     20             join_right = nums[pivot-1]
     21             join_left = nums[(((pivot - 1) + bound)) % nl]
     22             
     23             delta += (join_left - join_right) * 2
     24             if delta > 0:
     25                 sc += 1
     26 
     27         return sc