From 7e6129d5f54431b5d07b324218c4c225531dbfab Mon Sep 17 00:00:00 2001 From: Chemandante <valentin@struchalin.com> Date: Tue, 4 Oct 2022 13:16:21 +0300 Subject: [PATCH] Added Python solution for "1680. Concatenation of Consecutive Binary Numbers" --- 16/80/m1680.py | 23 +++++++++++++++++++++++ docs/index.md | 1 + 2 files changed, 24 insertions(+) create mode 100644 16/80/m1680.py diff --git a/16/80/m1680.py b/16/80/m1680.py new file mode 100644 index 0000000..838b42e --- /dev/null +++ b/16/80/m1680.py @@ -0,0 +1,23 @@ +# Solved: +# (M) Concatenation of Consecutive Binary Numbers +# https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/ + +class Solution: + def concatenatedBinary(self, n: int) -> int: + res = 1 + shift = 1 + modulo = 10**9 + 7 + + for i in range(2, n + 1): + # If `i` is exact power of 2 increase `shift` by 1 + if i & (i - 1) == 0: + shift += 1 + res = ((res << shift) + i) % modulo + + return res + + +x = Solution() +print(x.concatenatedBinary(1)) # 1 +print(x.concatenatedBinary(3)) # 27 +print(x.concatenatedBinary(12)) # 505379714 diff --git a/docs/index.md b/docs/index.md index c66abec..61f4fa7 100644 --- a/docs/index.md +++ b/docs/index.md @@ -98,6 +98,7 @@ Here is the list of solved problems. Language images are clickable and lead to t | 1539 | ![E](img/E.png) [Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number/) | | <a href="https://github.com/chemandante/leetcode/blob/master/15/39/m1539.swift" target="_blank"><img src="img/sw.png"></a> | | 1608 | ![E](img/E.png) [Special Array With X Elements Greater Than or Equal X](https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/) | | <a href="https://github.com/chemandante/leetcode/blob/master/16/08/m1608.swift" target="_blank"><img src="img/sw.png"></a> | | 1647 | ![M](img/M.png) [Minimum Deletions to Make Character Frequencies Unique](https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/) | | <a href="https://github.com/chemandante/leetcode/blob/master/16/47/m1647.cpp" target="_blank"><img src="img/cpp.png"></a> | +| 1680 | ![M](img/M.png) [Concatenation of Consecutive Binary Numbers](https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/) | | <a href="https://github.com/chemandante/leetcode/blob/master/16/80/m1680.py" target="_blank"><img src="img/py.png"></a> | | 1689 | ![M](img/M.png) [Partitioning Into Minimum Number Of Deci-Binary Numbers](https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/) | | <a href="https://github.com/chemandante/leetcode/blob/master/16/89/m1689.cpp" target="_blank"><img src="img/cpp.png"></a> | | 1710 | ![E](img/E.png) [Maximum Units on a Truck](https://leetcode.com/problems/maximum-units-on-a-truck/) | | <a href="https://github.com/chemandante/leetcode/blob/master/17/10/m1710.cpp" target="_blank"><img src="img/cpp.png"></a> | | 1855 | ![M](img/M.png) [Maximum Distance Between a Pair of Values](https://leetcode.com/problems/maximum-distance-between-a-pair-of-values/) | | <a href="https://github.com/chemandante/leetcode/blob/master/18/55/m1855.py" target="_blank"><img src="img/py.png"></a> |