leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 93bc551ae7eaa6121ba66930992060158f16520c
parent 2075aa952f7ef9a00d49dabfc634d604380d5737
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed,  3 Apr 2024 10:00:20 +0200

1 Random Problem

Diffstat:
AProblems/0477.cpp | 15+++++++++++++++
MREADME.md | 1+
2 files changed, 16 insertions(+), 0 deletions(-)

diff --git a/Problems/0477.cpp b/Problems/0477.cpp @@ -0,0 +1,15 @@ +class Solution { + public: + int totalHammingDistance(const vector<int> &nums) const { + const int n = size(nums); + int res = 0; + + for (unsigned mask = 1; mask <= 1 << 30; mask <<= 1) { + int count = + accumulate(begin(nums), end(nums), 0, [&mask](int a, int b) { return a + !!(b & mask); }); + res += count * (n - count); + } + + return res; + } +}; diff --git a/README.md b/README.md @@ -345,6 +345,7 @@ for solving problems. | 0460 | Hard | [LFU Cache](Problems/0460.cpp) | | 0462 | Medium | [Minimum Moves to Equal Array Elements II](Problems/0462.cpp) | | 0472 | Hard | [Concatenated Words](Problems/0472.cpp) | +| 0477 | Medium | [Total Hamming Distance](Problems/0477.cpp) | | 0485 | Easy | [Max Consecutive Ones](Problems/0485.cpp) | | 0486 | Medium | [Reachable Nodes With Restrictions](Problems/0486.cpp) | | 0491 | Medium | [Non-decreasing Subsequences](Problems/0491.cpp) |