Skip to content

Commit 6a13435

Browse files
committed
Sync LeetCode submission Runtime - 2348 ms (61.76%), Memory - 17.8 MB (54.45%)
1 parent c25eeb6 commit 6a13435

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<p>A <strong>k-mirror number</strong> is a <strong>positive</strong> integer <strong>without leading zeros</strong> that reads the same both forward and backward in base-10 <strong>as well as</strong> in base-k.</p>
2+
3+
<ul>
4+
<li>For example, <code>9</code> is a 2-mirror number. The representation of <code>9</code> in base-10 and base-2 are <code>9</code> and <code>1001</code> respectively, which read the same both forward and backward.</li>
5+
<li>On the contrary, <code>4</code> is not a 2-mirror number. The representation of <code>4</code> in base-2 is <code>100</code>, which does not read the same both forward and backward.</li>
6+
</ul>
7+
8+
<p>Given the base <code>k</code> and the number <code>n</code>, return <em>the <strong>sum</strong> of the</em> <code>n</code> <em><strong>smallest</strong> k-mirror numbers</em>.</p>
9+
10+
<p>&nbsp;</p>
11+
<p><strong class="example">Example 1:</strong></p>
12+
13+
<pre>
14+
<strong>Input:</strong> k = 2, n = 5
15+
<strong>Output:</strong> 25
16+
<strong>Explanation:
17+
</strong>The 5 smallest 2-mirror numbers and their representations in base-2 are listed as follows:
18+
base-10 base-2
19+
1 1
20+
3 11
21+
5 101
22+
7 111
23+
9 1001
24+
Their sum = 1 + 3 + 5 + 7 + 9 = 25.
25+
</pre>
26+
27+
<p><strong class="example">Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> k = 3, n = 7
31+
<strong>Output:</strong> 499
32+
<strong>Explanation:
33+
</strong>The 7 smallest 3-mirror numbers are and their representations in base-3 are listed as follows:
34+
base-10 base-3
35+
1 1
36+
2 2
37+
4 11
38+
8 22
39+
121 11111
40+
151 12121
41+
212 21212
42+
Their sum = 1 + 2 + 4 + 8 + 121 + 151 + 212 = 499.
43+
</pre>
44+
45+
<p><strong class="example">Example 3:</strong></p>
46+
47+
<pre>
48+
<strong>Input:</strong> k = 7, n = 17
49+
<strong>Output:</strong> 20379000
50+
<strong>Explanation:</strong> The 17 smallest 7-mirror numbers are:
51+
1, 2, 3, 4, 5, 6, 8, 121, 171, 242, 292, 16561, 65656, 2137312, 4602064, 6597956, 6958596
52+
</pre>
53+
54+
<p>&nbsp;</p>
55+
<p><strong>Constraints:</strong></p>
56+
57+
<ul>
58+
<li><code>2 &lt;= k &lt;= 9</code></li>
59+
<li><code>1 &lt;= n &lt;= 30</code></li>
60+
</ul>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Time: O(log n)
2+
# Space: O(n)
3+
4+
class Solution:
5+
def createPalindrome(self, num: int, odd: bool) -> int:
6+
x = num
7+
if odd:
8+
x //= 10
9+
while x > 0:
10+
num = num * 10 + x % 10
11+
x //= 10
12+
return num
13+
14+
def isPalindrome(self, num: int, base: int) -> bool:
15+
digits = []
16+
while num > 0:
17+
digits.append(num % base)
18+
num //= base
19+
return digits == digits[::-1]
20+
21+
def kMirror(self, k: int, n: int) -> int:
22+
total = 0
23+
length = 1
24+
while n > 0:
25+
for i in range(length, length * 10):
26+
if n <= 0:
27+
break
28+
p = self.createPalindrome(i, True)
29+
if self.isPalindrome(p, k):
30+
total += p
31+
n -= 1
32+
for i in range(length, length * 10):
33+
if n <= 0:
34+
break
35+
p = self.createPalindrome(i, False)
36+
if self.isPalindrome(p, k):
37+
total += p
38+
n -= 1
39+
length *= 10
40+
return total

0 commit comments

Comments
 (0)