MAX_N = int(1e5) factors = [[] for _ inrange(MAX_N + 1)] for i inrange(1, MAX_N + 1): for j inrange(i, MAX_N + 1, i): factors[j].append(i)
classSolution: defsortableIntegers(self, nums: list[int]) -> int: n = len(nums)
defcheck(st: int, k: int) -> bool: cnt = 0 for i inrange(st + 1, st + k): if nums[i - 1] > nums[i]: cnt += 1 if cnt >= 2: returnFalse return cnt == 0or (cnt == 1and nums[st] >= nums[st + k - 1])
ans = 0 for k in factors[n]: pre_mx = -inf for st inrange(0, n, k): ifnot check(st, k): break ifmin(nums[st : st + k]) < pre_mx: break pre_mx = max(nums[st : st + k]) else: ans += k return ans
寫在最後
Cover Image Credit
The cover image was created by @D.DCCK. All rights belong to the original artist.
If you are the copyright holder and believe this usage is inappropriate, please contact me by email or leave a comment. I will remove the image promptly.