※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 94.156.205.96 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1731119686.A.9BF.html
3133. Minimum Array End
## 思路
AND之後是x, 所以每個值都有x的bit
陣列第i個值 = 原本的x 加上 在剩下的0中塞入i-1的bit值
Input: n = 3, x = 5
arr = [5 (0101), 7 (0111), 13 (1101)]
x = 0101 (5)
1 0 (2)
-------
1101 (13)
## Code
```python
class Solution:
def minEnd(self, n: int, x: int) -> int:
res = x
n -= 1
for i in range(64):
if x & (1 << i):
continue
res |= (n & 1) << i
n >>= 1
if n == 0:
break
return res
```
--