Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1318
Browse files Browse the repository at this point in the history
No.1318.Minimum Flips to Make a OR b Equal to c
  • Loading branch information
yanglbme committed Mar 19, 2024
1 parent 9a66cae commit fd6d38a
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 56 deletions.
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;
}

0 comments on commit fd6d38a

Please sign in to comment.