Skip to content

feat: add solutions to lc problem: No.1318 #2462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,27 @@

我们可以枚举 $a$, $b$, $c$ 的二进制表示的每一位,分别记为 $x$, $y$, $z$。如果 $x$ 和 $y$ 的按位或运算结果与 $z$ 不同,此时我们判断 $x$ 和 $y$ 是否都是 $1$,如果是,则需要翻转两次,否则只需要翻转一次。我们将所有需要翻转的次数累加即可。

时间复杂度 $O(\log M)$,空间复杂度 $O(1)$。其中 $M$ 是题目中数字的最大值
时间复杂度 $O(\log M)$,其中 $M$ 是题目中数字的最大值。空间复杂度 $O(1)$。

<!-- tabs:start -->

```python
class Solution:
def minFlips(self, a: int, b: int, c: int) -> int:
ans = 0
for i in range(30):
for i in range(32):
x, y, z = a >> i & 1, b >> i & 1, c >> i & 1
if x | y != z:
ans += 2 if x == 1 and y == 1 else 1
ans += x + y if z == 0 else int(x == 0 and y == 0)
return ans
```

```java
class Solution {
public int minFlips(int a, int b, int c) {
int ans = 0;
for (int i = 0; i < 30; ++i) {
for (int i = 0; i < 32; ++i) {
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
if ((x | y) != z) {
ans += x == 1 && y == 1 ? 2 : 1;
}
ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
}
return ans;
}
Expand All @@ -87,11 +84,9 @@ class Solution {
public:
int minFlips(int a, int b, int c) {
int ans = 0;
for (int i = 0; i < 30; ++i) {
for (int i = 0; i < 32; ++i) {
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
if ((x | y) != z) {
ans += x == 1 && y == 1 ? 2 : 1;
}
ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
}
return ans;
}
Expand All @@ -100,20 +95,29 @@ public:

```go
func minFlips(a int, b int, c int) (ans int) {
for i := 0; i < 30; i++ {
for i := 0; i < 32; i++ {
x, y, z := a>>i&1, b>>i&1, c>>i&1
if (x | y) != z {
if x == 1 && y == 1 {
ans += 2
} else {
ans++
}
if z == 0 {
ans += x + y
} else if x == 0 && y == 0 {
ans++
}
}
return
}
```

```ts
function minFlips(a: number, b: number, c: number): number {
let ans = 0;
for (let i = 0; i < 32; ++i) {
const [x, y, z] = [(a >> i) & 1, (b >> i) & 1, (c >> i) & 1];
ans += z === 0 ? x + y : x + y === 0 ? 1 : 0;
}
return ans;
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,31 @@ Flip operation&nbsp;consists of change&nbsp;<strong>any</strong>&nbsp;single bit

## Solutions

### Solution 1
### Solution 1: Bit Manipulation

We can enumerate each bit of the binary representation of $a$, $b$, and $c$, denoted as $x$, $y$, and $z$ respectively. If the bitwise OR operation result of $x$ and $y$ is different from $z$, we then check if both $x$ and $y$ are $1$. If so, we need to flip twice, otherwise, we only need to flip once. We accumulate all the required flip times.

The time complexity is $O(\log M)$, where $M$ is the maximum value of the numbers in the problem. The space complexity is $O(1)$.

<!-- tabs:start -->

```python
class Solution:
def minFlips(self, a: int, b: int, c: int) -> int:
ans = 0
for i in range(30):
for i in range(32):
x, y, z = a >> i & 1, b >> i & 1, c >> i & 1
if x | y != z:
ans += 2 if x == 1 and y == 1 else 1
ans += x + y if z == 0 else int(x == 0 and y == 0)
return ans
```

```java
class Solution {
public int minFlips(int a, int b, int c) {
int ans = 0;
for (int i = 0; i < 30; ++i) {
for (int i = 0; i < 32; ++i) {
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
if ((x | y) != z) {
ans += x == 1 && y == 1 ? 2 : 1;
}
ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
}
return ans;
}
Expand All @@ -95,11 +96,9 @@ class Solution {
public:
int minFlips(int a, int b, int c) {
int ans = 0;
for (int i = 0; i < 30; ++i) {
for (int i = 0; i < 32; ++i) {
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
if ((x | y) != z) {
ans += x == 1 && y == 1 ? 2 : 1;
}
ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
}
return ans;
}
Expand All @@ -108,20 +107,29 @@ public:

```go
func minFlips(a int, b int, c int) (ans int) {
for i := 0; i < 30; i++ {
for i := 0; i < 32; i++ {
x, y, z := a>>i&1, b>>i&1, c>>i&1
if (x | y) != z {
if x == 1 && y == 1 {
ans += 2
} else {
ans++
}
if z == 0 {
ans += x + y
} else if x == 0 && y == 0 {
ans++
}
}
return
}
```

```ts
function minFlips(a: number, b: number, c: number): number {
let ans = 0;
for (let i = 0; i < 32; ++i) {
const [x, y, z] = [(a >> i) & 1, (b >> i) & 1, (c >> i) & 1];
ans += z === 0 ? x + y : x + y === 0 ? 1 : 0;
}
return ans;
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ class Solution {
public:
int minFlips(int a, int b, int c) {
int ans = 0;
for (int i = 0; i < 30; ++i) {
for (int i = 0; i < 32; ++i) {
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
if ((x | y) != z) {
ans += x == 1 && y == 1 ? 2 : 1;
}
ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
}
return ans;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
func minFlips(a int, b int, c int) (ans int) {
for i := 0; i < 30; i++ {
for i := 0; i < 32; i++ {
x, y, z := a>>i&1, b>>i&1, c>>i&1
if (x | y) != z {
if x == 1 && y == 1 {
ans += 2
} else {
ans++
}
if z == 0 {
ans += x + y
} else if x == 0 && y == 0 {
ans++
}
}
return
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
class Solution {
public int minFlips(int a, int b, int c) {
int ans = 0;
for (int i = 0; i < 30; ++i) {
for (int i = 0; i < 32; ++i) {
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
if ((x | y) != z) {
ans += x == 1 && y == 1 ? 2 : 1;
}
ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
}
return ans;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
class Solution:
def minFlips(self, a: int, b: int, c: int) -> int:
ans = 0
for i in range(30):
for i in range(32):
x, y, z = a >> i & 1, b >> i & 1, c >> i & 1
if x | y != z:
ans += 2 if x == 1 and y == 1 else 1
ans += x + y if z == 0 else int(x == 0 and y == 0)
return ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function minFlips(a: number, b: number, c: number): number {
let ans = 0;
for (let i = 0; i < 32; ++i) {
const [x, y, z] = [(a >> i) & 1, (b >> i) & 1, (c >> i) & 1];
ans += z === 0 ? x + y : x + y === 0 ? 1 : 0;
}
return ans;
}