algorithms

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

commit 9b6e2d120e2ac998acad4c90f755ebd8a6e6fb46
parent cd956f6d2ffd2b8679c841d2bf4876442eceda64
Author: Andrew Laack <andrew@laack.co>
Date:   Fri, 11 Sep 2026 16:39:09 -0500

Two problems from contest

Diffstat:
Acount-good-cyclic-rotations/count-good-cyclic-rotations.py | 27+++++++++++++++++++++++++++
Acount-rotations-with-exactly-k-equal-adjacent-pairs/count-rotations-with-exactly-k-equal-adjacent-pairs.py | 23+++++++++++++++++++++++
2 files changed, 50 insertions(+), 0 deletions(-)

diff --git a/count-good-cyclic-rotations/count-good-cyclic-rotations.py b/count-good-cyclic-rotations/count-good-cyclic-rotations.py @@ -0,0 +1,27 @@ +class Solution: + def countGoodRotations(self, nums: list[int]) -> int: + sc = 0 + bound = len(nums) // 2 + nl = len(nums) + + delta = 0 + + for idx in range(0,len(nums)): + new_pos = ((idx) + nl) % nl + if new_pos < bound: + delta += nums[idx] + else: + delta -= nums[idx] + + if delta > 0: + sc += 1 + + for pivot in range(1,len(nums)): + join_right = nums[pivot-1] + join_left = nums[(((pivot - 1) + bound)) % nl] + + delta += (join_left - join_right) * 2 + if delta > 0: + sc += 1 + + return sc diff --git a/count-rotations-with-exactly-k-equal-adjacent-pairs/count-rotations-with-exactly-k-equal-adjacent-pairs.py b/count-rotations-with-exactly-k-equal-adjacent-pairs/count-rotations-with-exactly-k-equal-adjacent-pairs.py @@ -0,0 +1,23 @@ +class Solution: + + def prefix_append(self,num,s): + pre = s[0:num] + s = s[num:len(s)] + pre + return s + def score(self,s): + sc = 0 + for i in range(0,len(s) - 1): + if s[i] == s[i+1]: + sc += 1 + return sc + + def countRotations(self, s: str, k: int) -> int: + # string s length n, int k + # choose prefix of s and move it to the end of string + # score = number of i where i == i+1 + score = 0 + for n in range(0,len(s)): + re = self.prefix_append(n,s) + if self.score(re) == k: + score += 1 + return score