Skip to content

Commit a16de33

Browse files
committed
up problem add binary
1 parent 1263698 commit a16de33

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

src/leetcode/array/majority-element.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,24 @@ export function majorityElement(nums: number[]): number {
55
return Math.floor(nums[Math.floor(nums.length / 2)])
66
};
77

8+
export function majorityElementV2(nums: number[]): number {
9+
if (nums.length == 0) return -1
10+
11+
const dict: Map<number, number> = new Map<number, number>()
12+
for (let i = 0; i < nums.length; i++) {
13+
if (dict.has(nums[i])) {
14+
const occurs = dict.get(nums[i]) as number + 1
15+
if (occurs > nums.length / 2) {
16+
return nums[i]
17+
}
18+
dict.set(nums[i], occurs)
19+
20+
} else {
21+
dict.set(nums[i], 1)
22+
}
23+
}
24+
return nums[0] // Timecomplexity O(n), SpaceComplexity O(number)
25+
26+
};
827

9-
console.log("major element: ", majorityElement([3, 3, 3, 4, 2]))
28+
console.log("major element: ", majorityElementV2([3, 3, 3, 4, 2]))
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { addBinary } from './add-binary';
2+
import { describe, test, expect } from 'bun:test';
3+
4+
describe('addBinary', () => {
5+
test('should return 0 for adding two zeros', () => {
6+
expect(addBinary('0', '0')).toBe('0');
7+
});
8+
9+
test('should return the same binary for adding zero', () => {
10+
expect(addBinary('1', '0')).toBe('1');
11+
expect(addBinary('0', '1')).toBe('1');
12+
});
13+
14+
test('should return 10 for adding 1 and 1', () => {
15+
expect(addBinary('1', '1')).toBe('10');
16+
});
17+
18+
test('should correctly add two binary numbers of equal length', () => {
19+
expect(addBinary('1010', '1011')).toBe('10101');
20+
expect(addBinary('1101', '1011')).toBe('11000');
21+
});
22+
23+
test('should correctly add binary numbers of different lengths', () => {
24+
expect(addBinary('111', '1')).toBe('1000');
25+
expect(addBinary('111111', '1')).toBe('1000000');
26+
expect(addBinary('101010', '1100')).toBe('100110');
27+
});
28+
29+
test('should handle empty strings', () => {
30+
expect(addBinary('', '101')).toBe('101');
31+
expect(addBinary('101', '')).toBe('101');
32+
expect(addBinary('', '')).toBe('0');
33+
});
34+
});

src/leetcode/string/add-binary.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
export function addBinary(a: string, b: string): string {
2+
if (a.length == 0) return b
3+
if (b.length == 0) return a
4+
if (a.length && b.length == 0) return '0'
5+
let sum = ''
6+
let carry = 0
7+
// 1010
8+
// 101
9+
while (a.length > 0 || b.length > 0 || carry != 0) {
10+
const currentSum = Number(a[a.length - 1] ?? 0) + Number(b[b.length - 1] ?? 0)
11+
if (currentSum == 2) {
12+
if (carry > 0) {
13+
sum = `${1}${sum}`
14+
} else {
15+
sum = `${0}${sum}`
16+
carry += 1
17+
}
18+
} else {
19+
if (carry > 0) {
20+
if (currentSum + carry == 2) {
21+
sum = `${0}${sum}`
22+
} else {
23+
sum = `${1}${sum}`
24+
carry--
25+
}
26+
} else {
27+
sum = `${currentSum}${sum}`
28+
}
29+
}
30+
a = a.slice(0, -1)
31+
b = b.slice(0, -1)
32+
}
33+
return sum
34+
};
35+
36+
// console.log(addBinary('1010', '1011'));
37+
// console.log(addBinary('1010', '101'));
38+
// console.log(addBinary('11', '1'));
39+
// console.log(addBinary('1101', '1011')); // 11000
40+
// Time complexity: O(Max(n, m)), Có cách nào làm tốt hơn hay không ?

0 commit comments

Comments
 (0)