From 4d1f118f555038a04d1b835c599cc49216262761 Mon Sep 17 00:00:00 2001 From: tom4649 Date: Sat, 18 Jul 2026 06:57:46 +0900 Subject: [PATCH 1/2] Add step1,2 --- 0338.Counting-Bits/memo.md | 23 +++++++++++++++++++++++ 0338.Counting-Bits/step1_dp.py | 12 ++++++++++++ 0338.Counting-Bits/step1_naive.py | 12 ++++++++++++ 0338.Counting-Bits/step2_dpv2.py | 17 +++++++++++++++++ 0338.Counting-Bits/step2_unset.py | 12 ++++++++++++ 0338.Counting-Bits/step2_unsetv2.py | 13 +++++++++++++ 6 files changed, 89 insertions(+) create mode 100644 0338.Counting-Bits/memo.md create mode 100644 0338.Counting-Bits/step1_dp.py create mode 100644 0338.Counting-Bits/step1_naive.py create mode 100644 0338.Counting-Bits/step2_dpv2.py create mode 100644 0338.Counting-Bits/step2_unset.py create mode 100644 0338.Counting-Bits/step2_unsetv2.py 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..8418339 --- /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..20a3773 --- /dev/null +++ b/0338.Counting-Bits/step1_naive.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] = 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..b19e41b --- /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_unset.py b/0338.Counting-Bits/step2_unset.py new file mode 100644 index 0000000..8fbbece --- /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) + power_of_two = 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..26cd340 --- /dev/null +++ b/0338.Counting-Bits/step2_unsetv2.py @@ -0,0 +1,13 @@ +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): + result[i] = result[i - (i & -i)] + 1 + + return result + From f9ca4120c91f89ec95c200276094fc78d9dedf87 Mon Sep 17 00:00:00 2001 From: tom4649 Date: Sat, 18 Jul 2026 12:03:29 +0900 Subject: [PATCH 2/2] Fix --- 0338.Counting-Bits/step1_dp.py | 2 +- 0338.Counting-Bits/step1_naive.py | 5 +---- 0338.Counting-Bits/step2_dpv2.py | 2 +- 0338.Counting-Bits/step2_naive.py | 4 ++++ 0338.Counting-Bits/step2_unset.py | 4 ++-- 0338.Counting-Bits/step2_unsetv2.py | 3 +-- 6 files changed, 10 insertions(+), 10 deletions(-) create mode 100644 0338.Counting-Bits/step2_naive.py diff --git a/0338.Counting-Bits/step1_dp.py b/0338.Counting-Bits/step1_dp.py index 8418339..9c89e15 100644 --- a/0338.Counting-Bits/step1_dp.py +++ b/0338.Counting-Bits/step1_dp.py @@ -1,5 +1,5 @@ class Solution: - def countBits(self, n: int) -> List[int]: + def countBits(self, n: int) -> list[int]: if n == 0: return [0] diff --git a/0338.Counting-Bits/step1_naive.py b/0338.Counting-Bits/step1_naive.py index 20a3773..4aab8de 100644 --- a/0338.Counting-Bits/step1_naive.py +++ b/0338.Counting-Bits/step1_naive.py @@ -1,8 +1,5 @@ class Solution: - def countBits(self, n: int) -> List[int]: - if n == 0: - return [0] - + def countBits(self, n: int) -> list[int]: result = [0] * (n + 1) for i in range(n + 1): diff --git a/0338.Counting-Bits/step2_dpv2.py b/0338.Counting-Bits/step2_dpv2.py index b19e41b..2a45ec4 100644 --- a/0338.Counting-Bits/step2_dpv2.py +++ b/0338.Counting-Bits/step2_dpv2.py @@ -1,5 +1,5 @@ class Solution: - def countBits(self, n: int) -> List[int]: + def countBits(self, n: int) -> list[int]: if n == 0: return [0] 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 index 8fbbece..bb472d5 100644 --- a/0338.Counting-Bits/step2_unset.py +++ b/0338.Counting-Bits/step2_unset.py @@ -1,12 +1,12 @@ class Solution: - def countBits(self, n: int) -> List[int]: + 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): 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 index 26cd340..5d6ef3a 100644 --- a/0338.Counting-Bits/step2_unsetv2.py +++ b/0338.Counting-Bits/step2_unsetv2.py @@ -1,10 +1,9 @@ class Solution: - def countBits(self, n: int) -> List[int]: + 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): result[i] = result[i - (i & -i)] + 1