3133. Minimum Array End
C++
class Solution { public: long long minEnd(int n, int x) { // Set x's 0s with (n - 1)'s LSb-to-MSb bits, preserving x's 1s. This // operation increase x for (n - 1) iterations while preserving x's 1s. const int kMaxBit = log2(n) + log2(x) + 2; const long k = n - 1; long ans = x; int kBinaryIndex = 0; for (int i = 0; i < kMaxBit; ++i) { if ((ans >> i & 1) == 0) { // Set x's 0 with k's bit if the running bit of k is 1. if (k >> kBinaryIndex & 1) ans |= 1L << i; ++kBinaryIndex; } } return ans; } };
JAVA
import math class Solution: def minEnd(self, n: int, x: int) -> int: kMaxBit = math.log2(n) + math.log2(x) + 2 k = n - 1 ans = x kBinaryIndex = 0 for i in range(int(kMaxBit)): if (ans >> i & 1) == 0: if (k >> kBinaryIndex & 1): ans |= 1 << i kBinaryIndex += 1 return ans- PYTHON
public class Solution { public long minEnd(int n, int x) { int kMaxBit = (int)(Math.log(n) / Math.log(2) + Math.log(x) / Math.log(2) + 2); long k = n - 1; long ans = x; int kBinaryIndex = 0; for (int i = 0; i < kMaxBit; ++i) { if ((ans >> i & 1) == 0) { if ((k >> kBinaryIndex & 1) == 1) { ans |= 1L << i; } kBinaryIndex++; } } return ans; } }
Comments
Post a Comment