class Solution:
def specialArray(self, nums: List[int]) -> int:
for n in range(len(nums) + 1):
if sum(1 for v in nums if v >= n) == n:
return n
return -1


1. 奇数层的数字为偶数，而且从左到右严格递减；
2. 偶数层的数字为奇数，而且从左到右严格递增。

BFS找出每一层来判断是否满足条件。

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
def isEvenOddTree(self, root: TreeNode) -> bool:
level = 0
cur = [root]
while cur:
next = []
nums = []
for node in cur:
if node is None:
continue
nums.append(node.val)
next.append(node.left)
next.append(node.right)
if level % 2 == 1:
if any(a % 2 == 1 for a in nums) or any(a <= b for a, b in zip(nums, nums[1:])):
return False
else:
if any(a % 2 == 0 for a in nums) or any(a >= b for a, b in zip(nums, nums[1:])):
return False
cur = next
level += 1
return True


class Solution:
def visiblePoints(self, points: List[List[int]], angle: int, location: List[int]) -> int:
x, y = location
base = 0
degrees = []
for a, b in points:
if a == x and b == y:
base += 1
continue
degrees.append(math.degrees(math.atan2(b - y, a - x)))
degrees.append(degrees[-1] + 360)
degrees.sort()
ans = i = j = 0
while i < len(degrees):
while i < len(degrees) and degrees[i] - degrees[j] <= angle:
i += 1
ans = max(ans, i - j)
j += 1
return ans + base


1. 改变最低位；
2. 如果第 (i + 1) 位是 1，第 (i + 2) 到最后的最低位都是 0，那么改变第 i 位。

（1）如果 y = c，那么

f(yx...x, c0...0) = f(x...x, 0...0)。

（2）如果 y != c，那么有两种情况 (y, c) = (1, 0) 或者 (0, 1)。

（2.1）(y, c) = (1, 0)。这种情况在前面已经讨论过，

f(1x...x, 00...0) = 2l + f(x...x, 10...0)

（2.2）(y, c) = (0, 1)。这种情况下，0x...x -> 10...0 的最优方案是

0x...x -> 010...0 -> 10...0

f(0x...x, 10...0) = 2l + f(x...x, 10...0)

f(yx...x, c0...0) = 2l + f(x...x, 10...0)

class Solution:
def minimumOneBitOperations(self, n: int) -> int:
bit = []
while n:
bit.append(n & 1)
n //= 2
ans = c = 0
for i in range(len(bit) - 1, -1, -1):
if c != bit[i]:
ans += 2 ** i
c = 1
else:
c = 0
return ans