diff --git a/0338.Counting-Bits/memo.md b/0338.Counting-Bits/memo.md new file mode 100644 index 0000000..0c2da58 --- /dev/null +++ b/0338.Counting-Bits/memo.md @@ -0,0 +1,23 @@ +# 338. Counting Bits + +## step1 +4m ぐらい。hamming weightの問題の配列前計算の方法をまねた。計算量 O(n)。動的計画法である。 + +動的計画法を使わないと計算量は O(nlog n)になる。 + +しかし、実行速度自体は組み込み関数を使えばこの解法の方が速い。 + +Follow up もその旨が書かれている。 + +> It is very easy to come up with a solution with a runtime of O(n log n). Can you do it in linear time O(n) and possibly in a single pass? + +## 他の人のコード +https://github.com/rihib/leetcode/pull/44 + +動的計画法だが考え方が異なる。自分のものは下一桁とそれ以外に分けていたが、これは上一桁とそれ以外に分けている。 + +> Rightmost set bitをunsetする方法 +- x & (x - 1) +- x - (x & -x) + +https://github.com/rihib/leetcode/blob/main/go/counting_bits.go diff --git a/0338.Counting-Bits/step1_dp.py b/0338.Counting-Bits/step1_dp.py new file mode 100644 index 0000000..9c89e15 --- /dev/null +++ b/0338.Counting-Bits/step1_dp.py @@ -0,0 +1,12 @@ +class Solution: + def countBits(self, n: int) -> list[int]: + if n == 0: + return [0] + + result = [0] * (n + 1) + + for i in range(n + 1): + result[i] = result[i >> 1] + (i & 1) + + return result + diff --git a/0338.Counting-Bits/step1_naive.py b/0338.Counting-Bits/step1_naive.py new file mode 100644 index 0000000..4aab8de --- /dev/null +++ b/0338.Counting-Bits/step1_naive.py @@ -0,0 +1,9 @@ +class Solution: + def countBits(self, n: int) -> list[int]: + result = [0] * (n + 1) + + for i in range(n + 1): + result[i] = i.bit_count() + + return result + diff --git a/0338.Counting-Bits/step2_dpv2.py b/0338.Counting-Bits/step2_dpv2.py new file mode 100644 index 0000000..2a45ec4 --- /dev/null +++ b/0338.Counting-Bits/step2_dpv2.py @@ -0,0 +1,17 @@ +class Solution: + def countBits(self, n: int) -> list[int]: + if n == 0: + return [0] + + result = [0] * (n + 1) + power_of_two = 1 + + for i in range(1, n + 1): + if i == power_of_two << 1: + result[i] = 1 + power_of_two = i + else: + result[i] = result[i - power_of_two] + 1 + + return result + diff --git a/0338.Counting-Bits/step2_naive.py b/0338.Counting-Bits/step2_naive.py new file mode 100644 index 0000000..72f039f --- /dev/null +++ b/0338.Counting-Bits/step2_naive.py @@ -0,0 +1,4 @@ +class Solution: + def countBits(self, n: int) -> list[int]: + return [i.bit_count() for i in range(n + 1)] + diff --git a/0338.Counting-Bits/step2_unset.py b/0338.Counting-Bits/step2_unset.py new file mode 100644 index 0000000..bb472d5 --- /dev/null +++ b/0338.Counting-Bits/step2_unset.py @@ -0,0 +1,12 @@ +class Solution: + def countBits(self, n: int) -> list[int]: + if n == 0: + return [0] + + result = [0] * (n + 1) + + for i in range(1, n + 1): + result[i] = result[i & (i - 1)] + 1 + + return result + diff --git a/0338.Counting-Bits/step2_unsetv2.py b/0338.Counting-Bits/step2_unsetv2.py new file mode 100644 index 0000000..5d6ef3a --- /dev/null +++ b/0338.Counting-Bits/step2_unsetv2.py @@ -0,0 +1,12 @@ +class Solution: + def countBits(self, n: int) -> list[int]: + if n == 0: + return [0] + + result = [0] * (n + 1) + + for i in range(1, n + 1): + result[i] = result[i - (i & -i)] + 1 + + return result +