Skip to content

Commit b63f203

Browse files
authored
feat: add solutions to lc problem: No.1318 (doocs#2462)
No.1318.Minimum Flips to Make a OR b Equal to c
1 parent 9a66cae commit b63f203

File tree

7 files changed

+69
-56
lines changed

7 files changed

+69
-56
lines changed

solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/README.md

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,27 @@
5252

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

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

5757
<!-- tabs:start -->
5858

5959
```python
6060
class Solution:
6161
def minFlips(self, a: int, b: int, c: int) -> int:
6262
ans = 0
63-
for i in range(30):
63+
for i in range(32):
6464
x, y, z = a >> i & 1, b >> i & 1, c >> i & 1
65-
if x | y != z:
66-
ans += 2 if x == 1 and y == 1 else 1
65+
ans += x + y if z == 0 else int(x == 0 and y == 0)
6766
return ans
6867
```
6968

7069
```java
7170
class Solution {
7271
public int minFlips(int a, int b, int c) {
7372
int ans = 0;
74-
for (int i = 0; i < 30; ++i) {
73+
for (int i = 0; i < 32; ++i) {
7574
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
76-
if ((x | y) != z) {
77-
ans += x == 1 && y == 1 ? 2 : 1;
78-
}
75+
ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
7976
}
8077
return ans;
8178
}
@@ -87,11 +84,9 @@ class Solution {
8784
public:
8885
int minFlips(int a, int b, int c) {
8986
int ans = 0;
90-
for (int i = 0; i < 30; ++i) {
87+
for (int i = 0; i < 32; ++i) {
9188
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
92-
if ((x | y) != z) {
93-
ans += x == 1 && y == 1 ? 2 : 1;
94-
}
89+
ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
9590
}
9691
return ans;
9792
}
@@ -100,20 +95,29 @@ public:
10095
10196
```go
10297
func minFlips(a int, b int, c int) (ans int) {
103-
for i := 0; i < 30; i++ {
98+
for i := 0; i < 32; i++ {
10499
x, y, z := a>>i&1, b>>i&1, c>>i&1
105-
if (x | y) != z {
106-
if x == 1 && y == 1 {
107-
ans += 2
108-
} else {
109-
ans++
110-
}
100+
if z == 0 {
101+
ans += x + y
102+
} else if x == 0 && y == 0 {
103+
ans++
111104
}
112105
}
113106
return
114107
}
115108
```
116109

110+
```ts
111+
function minFlips(a: number, b: number, c: number): number {
112+
let ans = 0;
113+
for (let i = 0; i < 32; ++i) {
114+
const [x, y, z] = [(a >> i) & 1, (b >> i) & 1, (c >> i) & 1];
115+
ans += z === 0 ? x + y : x + y === 0 ? 1 : 0;
116+
}
117+
return ans;
118+
}
119+
```
120+
117121
<!-- tabs:end -->
118122

119123
<!-- end -->

solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/README_EN.md

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,31 @@ Flip operation&nbsp;consists of change&nbsp;<strong>any</strong>&nbsp;single bit
6060

6161
## Solutions
6262

63-
### Solution 1
63+
### Solution 1: Bit Manipulation
64+
65+
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.
66+
67+
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)$.
6468

6569
<!-- tabs:start -->
6670

6771
```python
6872
class Solution:
6973
def minFlips(self, a: int, b: int, c: int) -> int:
7074
ans = 0
71-
for i in range(30):
75+
for i in range(32):
7276
x, y, z = a >> i & 1, b >> i & 1, c >> i & 1
73-
if x | y != z:
74-
ans += 2 if x == 1 and y == 1 else 1
77+
ans += x + y if z == 0 else int(x == 0 and y == 0)
7578
return ans
7679
```
7780

7881
```java
7982
class Solution {
8083
public int minFlips(int a, int b, int c) {
8184
int ans = 0;
82-
for (int i = 0; i < 30; ++i) {
85+
for (int i = 0; i < 32; ++i) {
8386
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
84-
if ((x | y) != z) {
85-
ans += x == 1 && y == 1 ? 2 : 1;
86-
}
87+
ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
8788
}
8889
return ans;
8990
}
@@ -95,11 +96,9 @@ class Solution {
9596
public:
9697
int minFlips(int a, int b, int c) {
9798
int ans = 0;
98-
for (int i = 0; i < 30; ++i) {
99+
for (int i = 0; i < 32; ++i) {
99100
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
100-
if ((x | y) != z) {
101-
ans += x == 1 && y == 1 ? 2 : 1;
102-
}
101+
ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
103102
}
104103
return ans;
105104
}
@@ -108,20 +107,29 @@ public:
108107
109108
```go
110109
func minFlips(a int, b int, c int) (ans int) {
111-
for i := 0; i < 30; i++ {
110+
for i := 0; i < 32; i++ {
112111
x, y, z := a>>i&1, b>>i&1, c>>i&1
113-
if (x | y) != z {
114-
if x == 1 && y == 1 {
115-
ans += 2
116-
} else {
117-
ans++
118-
}
112+
if z == 0 {
113+
ans += x + y
114+
} else if x == 0 && y == 0 {
115+
ans++
119116
}
120117
}
121118
return
122119
}
123120
```
124121

122+
```ts
123+
function minFlips(a: number, b: number, c: number): number {
124+
let ans = 0;
125+
for (let i = 0; i < 32; ++i) {
126+
const [x, y, z] = [(a >> i) & 1, (b >> i) & 1, (c >> i) & 1];
127+
ans += z === 0 ? x + y : x + y === 0 ? 1 : 0;
128+
}
129+
return ans;
130+
}
131+
```
132+
125133
<!-- tabs:end -->
126134

127135
<!-- end -->

solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ class Solution {
22
public:
33
int minFlips(int a, int b, int c) {
44
int ans = 0;
5-
for (int i = 0; i < 30; ++i) {
5+
for (int i = 0; i < 32; ++i) {
66
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
7-
if ((x | y) != z) {
8-
ans += x == 1 && y == 1 ? 2 : 1;
9-
}
7+
ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
108
}
119
return ans;
1210
}

solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
func minFlips(a int, b int, c int) (ans int) {
2-
for i := 0; i < 30; i++ {
2+
for i := 0; i < 32; i++ {
33
x, y, z := a>>i&1, b>>i&1, c>>i&1
4-
if (x | y) != z {
5-
if x == 1 && y == 1 {
6-
ans += 2
7-
} else {
8-
ans++
9-
}
4+
if z == 0 {
5+
ans += x + y
6+
} else if x == 0 && y == 0 {
7+
ans++
108
}
119
}
1210
return

solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
class Solution {
22
public int minFlips(int a, int b, int c) {
33
int ans = 0;
4-
for (int i = 0; i < 30; ++i) {
4+
for (int i = 0; i < 32; ++i) {
55
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
6-
if ((x | y) != z) {
7-
ans += x == 1 && y == 1 ? 2 : 1;
8-
}
6+
ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
97
}
108
return ans;
119
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class Solution:
22
def minFlips(self, a: int, b: int, c: int) -> int:
33
ans = 0
4-
for i in range(30):
4+
for i in range(32):
55
x, y, z = a >> i & 1, b >> i & 1, c >> i & 1
6-
if x | y != z:
7-
ans += 2 if x == 1 and y == 1 else 1
6+
ans += x + y if z == 0 else int(x == 0 and y == 0)
87
return ans
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function minFlips(a: number, b: number, c: number): number {
2+
let ans = 0;
3+
for (let i = 0; i < 32; ++i) {
4+
const [x, y, z] = [(a >> i) & 1, (b >> i) & 1, (c >> i) & 1];
5+
ans += z === 0 ? x + y : x + y === 0 ? 1 : 0;
6+
}
7+
return ans;
8+
}

0 commit comments

Comments
 (0)