@@ -0,0 +1,1597 @@
+import unittest%0Afrom typing import List%0A%0Aimport utils%0A%0A%0A# O(n) time. O(1) space. Brent's cycle detection algorithm.%0Aclass Solution:%0A    def circularArrayLoop(self, nums: List%5Bint%5D) -%3E bool:%0A        n = len(nums)%0A%0A        for start, move in enumerate(nums):%0A            if move == 0:%0A                continue%0A%0A            t = h = start%0A            forward = move %3E 0%0A            loop_length = 0%0A            max_loop_length = 1%0A%0A            def advance(curr):%0A                move = nums%5Bcurr%5D%0A                if move == 0 or (move %3E 0) != forward:%0A                    return False, curr%0A                nxt = (curr + move) %25 n%0A                return nxt != curr, nxt%0A%0A            while True:%0A                ok, h = advance(h)%0A                if not ok:%0A                    break%0A%0A                if t == h:%0A                    return True%0A%0A                loop_length += 1%0A                if loop_length %3E= max_loop_length:%0A                    loop_length = 0%0A                    max_loop_length %3C%3C= 1%0A                    t = h%0A%0A            t = start%0A%0A            while True:%0A                ok, nxt = advance(t)%0A                if not ok:%0A                    break%0A%0A                nums%5Bt%5D = 0%0A                t = nxt%0A%0A        return False%0A%0A%0Aclass Test(unittest.TestCase):%0A    def test(self):%0A        cases = utils.load_test_json(__file__).test_cases%0A%0A        for case in cases:%0A            args = str(case.args)%0A            actual = Solution().circularArrayLoop(**case.args.__dict__)%0A            self.assertEqual(case.expected, actual, msg=args)%0A%0A%0Aif __name__ == '__main__':%0A    unittest.main()%0A

457. Circular Array Loop