|
44 | 44 |
|
45 | 45 | ## Solutions
|
46 | 46 |
|
47 |
| -### Solution 1 |
| 47 | +### Solution 1: Greedy + Bit Manipulation |
| 48 | + |
| 49 | +According to the problem description, to make the last element of the array as small as possible and the bitwise AND result of the elements in the array is $x$, the first element of the array must be $x$. |
| 50 | + |
| 51 | +Assume the binary representation of $x$ is $\underline{1}00\underline{1}00$, then the array sequence is $\underline{1}00\underline{1}00$, $\underline{1}00\underline{1}01$, $\underline{1}00\underline{1}10$, $\underline{1}00\underline{1}11$... |
| 52 | + |
| 53 | +If we ignore the underlined part, then the array sequence is $0000$, $0001$, $0010$, $0011$..., the first item is $0$, then the $n$-th item is $n-1$. |
| 54 | + |
| 55 | +Therefore, the answer is to fill each bit of the binary of $n-1$ into the $0$ bit of the binary of $x$ based on $x$. |
| 56 | + |
| 57 | +The time complexity is $O(\log x)$, and the space complexity is $O(1)$. |
48 | 58 |
|
49 | 59 | <!-- tabs:start -->
|
50 | 60 |
|
51 | 61 | ```python
|
52 |
| - |
| 62 | +class Solution: |
| 63 | + def minEnd(self, n: int, x: int) -> int: |
| 64 | + n -= 1 |
| 65 | + ans = x |
| 66 | + for i in range(31): |
| 67 | + if x >> i & 1 ^ 1: |
| 68 | + ans |= (n & 1) << i |
| 69 | + n >>= 1 |
| 70 | + ans |= n << 31 |
| 71 | + return ans |
53 | 72 | ```
|
54 | 73 |
|
55 | 74 | ```java
|
56 |
| - |
| 75 | +class Solution { |
| 76 | + public long minEnd(int n, int x) { |
| 77 | + --n; |
| 78 | + long ans = x; |
| 79 | + for (int i = 0; i < 31; ++i) { |
| 80 | + if ((x >> i & 1) == 0) { |
| 81 | + ans |= (n & 1) << i; |
| 82 | + n >>= 1; |
| 83 | + } |
| 84 | + } |
| 85 | + ans |= (long) n << 31; |
| 86 | + return ans; |
| 87 | + } |
| 88 | +} |
57 | 89 | ```
|
58 | 90 |
|
59 | 91 | ```cpp
|
60 |
| - |
| 92 | +class Solution { |
| 93 | +public: |
| 94 | + long long minEnd(int n, int x) { |
| 95 | + --n; |
| 96 | + long long ans = x; |
| 97 | + for (int i = 0; i < 31; ++i) { |
| 98 | + if (x >> i & 1 ^ 1) { |
| 99 | + ans |= (n & 1) << i; |
| 100 | + n >>= 1; |
| 101 | + } |
| 102 | + } |
| 103 | + ans |= (1LL * n) << 31; |
| 104 | + return ans; |
| 105 | + } |
| 106 | +}; |
61 | 107 | ```
|
62 | 108 |
|
63 | 109 | ```go
|
| 110 | +func minEnd(n int, x int) (ans int64) { |
| 111 | + n-- |
| 112 | + ans = int64(x) |
| 113 | + for i := 0; i < 31; i++ { |
| 114 | + if x>>i&1 == 0 { |
| 115 | + ans |= int64((n & 1) << i) |
| 116 | + n >>= 1 |
| 117 | + } |
| 118 | + } |
| 119 | + ans |= int64(n) << 31 |
| 120 | + return |
| 121 | +} |
| 122 | +``` |
64 | 123 |
|
| 124 | +```ts |
| 125 | +function minEnd(n: number, x: number): number { |
| 126 | + --n; |
| 127 | + let ans: bigint = BigInt(x); |
| 128 | + for (let i = 0; i < 31; ++i) { |
| 129 | + if (((x >> i) & 1) ^ 1) { |
| 130 | + ans |= BigInt(n & 1) << BigInt(i); |
| 131 | + n >>= 1; |
| 132 | + } |
| 133 | + } |
| 134 | + ans |= BigInt(n) << BigInt(31); |
| 135 | + return Number(ans); |
| 136 | +} |
65 | 137 | ```
|
66 | 138 |
|
67 | 139 | <!-- tabs:end -->
|
|
0 commit comments