Skip to content

Commit

Permalink
202nd Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Sep 7, 2024
1 parent 3e7aa6e commit 1cc889f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,15 @@ Ace Coding Interview with 75 Qs
| 714. Best Time to Buy and Sell Stock with Transaction Fee | Solution | Medium |
| 72. Edit Distance | Solution | Medium |

| Bit Manipulation | | |
| --------------------------------------------- | --------------- | ------ |
| 338. Counting Bits | [Solution][338] | Easy |
| 136. Single Number | [Solution][136] | Easy |
| 1318. Minimum Flips to Make a OR b Equal to c | Solution | Medium |
| Bit Manipulation | | |
| --------------------------------------------- | ---------------- | ------ |
| 338. Counting Bits | [Solution][338] | Easy |
| 136. Single Number | [Solution][136] | Easy |
| 1318. Minimum Flips to Make a OR b Equal to c | [Solution][1318] | Medium |

[338]: ./src/page-4/338.%20Counting%20Bits/countBits.ts
[136]: ./src/page-2/136.%20Single%20Number/singleNumber.ts
[1318]: ./src/page-13/1318.%20Minimum%20Flips%20to%20Make%20a%20OR%20b%20Equal%20to%20c/minFlips.ts

| Trie | | |
| --------------------------------- | -------- | ------ |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { minFlips } from './minFlips';

describe('1318. Minimum Flips to Make a OR b Equal to c', () => {
test('minFlips', () => {
expect(minFlips(2, 6, 5)).toBe(3);
expect(minFlips(4, 2, 7)).toBe(1);
expect(minFlips(1, 2, 3)).toBe(0);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
type MinFlips = (a: number, b: number, c: number) => number;

/**
* Accepted
*/
export const minFlips: MinFlips = (a, b, c) => {
let flips = 0;

for (let i = 0; i < 32; i++) {
// 32 bits to cover 32-bit integers
const bitA = (a >> i) & 1;
const bitB = (b >> i) & 1;
const bitC = (c >> i) & 1;

if (bitC === 1) {
// If bitC is 1, at least one of bitA or bitB must be 1
if (bitA === 0 && bitB === 0) {
flips += 1;
}
} else {
// If bitC is 0, both bitA and bitB must be 0
if (bitA === 1) flips += 1;
if (bitB === 1) flips += 1;
}
}

return flips;
};

0 comments on commit 1cc889f

Please sign in to comment.