Skip to content

Commit ed0e722

Browse files
authored
Added task 2081.
1 parent 21147c5 commit ed0e722

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package g2001_2100.s2081_sum_of_k_mirror_numbers;
2+
3+
// #Hard #Math #Enumeration #2022_05_29_Time_759_ms_(81.69%)_Space_116.6_MB_(61.97%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
public long kMirror(int k, int n) {
10+
List<Long> result = new ArrayList<>();
11+
int len = 1;
12+
while (result.size() < n) {
13+
backtrack(result, new char[len++], k, n, 0);
14+
}
15+
long sum = 0;
16+
for (Long num : result) {
17+
sum += num;
18+
}
19+
return sum;
20+
}
21+
22+
private void backtrack(List<Long> result, char[] arr, int k, int n, int index) {
23+
if (result.size() == n) {
24+
return;
25+
}
26+
if (index >= (arr.length + 1) / 2) {
27+
// Number in base-10
28+
Long number = Long.parseLong(String.valueOf(arr), k);
29+
30+
if (isPalindrome(number)) {
31+
result.add(number);
32+
}
33+
return;
34+
}
35+
// Generate base-k palindrome number in arr.length without leading zeros
36+
for (char i = 0; i < k; i++) {
37+
if (index == 0 && i == 0) {
38+
// Leading zeros
39+
continue;
40+
}
41+
char c = (char) (i + '0');
42+
arr[index] = c;
43+
arr[arr.length - 1 - index] = c;
44+
backtrack(result, arr, k, n, index + 1);
45+
}
46+
}
47+
48+
private boolean isPalindrome(Long number) {
49+
String strNum = String.valueOf(number);
50+
int left = 0;
51+
int right = strNum.length() - 1;
52+
while (left < right) {
53+
if (strNum.charAt(left) == strNum.charAt(right)) {
54+
left++;
55+
right--;
56+
} else {
57+
return false;
58+
}
59+
}
60+
return true;
61+
}
62+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2081\. Sum of k-Mirror Numbers
2+
3+
Hard
4+
5+
A **k-mirror number** is a **positive** integer **without leading zeros** that reads the same both forward and backward in base-10 **as well as** in base-k.
6+
7+
* For example, `9` is a 2-mirror number. The representation of `9` in base-10 and base-2 are `9` and `1001` respectively, which read the same both forward and backward.
8+
* On the contrary, `4` is not a 2-mirror number. The representation of `4` in base-2 is `100`, which does not read the same both forward and backward.
9+
10+
Given the base `k` and the number `n`, return _the **sum** of the_ `n` _**smallest** k-mirror numbers_.
11+
12+
**Example 1:**
13+
14+
**Input:** k = 2, n = 5
15+
16+
**Output:** 25
17+
18+
**Explanation:**
19+
20+
The 5 smallest 2-mirror numbers and their representations in base-2 are listed as follows:
21+
22+
base-10 base-2
23+
1 1
24+
3 11
25+
5 101
26+
7 111
27+
9 1001
28+
Their sum = 1 + 3 + 5 + 7 + 9 = 25.
29+
30+
**Example 2:**
31+
32+
**Input:** k = 3, n = 7
33+
34+
**Output:** 499
35+
36+
**Explanation:** The 7 smallest 3-mirror numbers are and their representations in base-3 are listed as follows:
37+
38+
base-10 base-3
39+
1 1
40+
2 2
41+
4 11
42+
8 22
43+
121 11111
44+
151 12121
45+
212 21212
46+
Their sum = 1 + 2 + 4 + 8 + 121 + 151 + 212 = 499.
47+
48+
**Example 3:**
49+
50+
**Input:** k = 7, n = 17
51+
52+
**Output:** 20379000
53+
54+
**Explanation:** The 17 smallest 7-mirror numbers are:
55+
56+
1, 2, 3, 4, 5, 6, 8, 121, 171, 242, 292, 16561, 65656, 2137312, 4602064, 6597956, 6958596
57+
58+
**Constraints:**
59+
60+
* `2 <= k <= 9`
61+
* `1 <= n <= 30`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2001_2100.s2081_sum_of_k_mirror_numbers;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void kMirror() {
11+
assertThat(new Solution().kMirror(2, 5), equalTo(25L));
12+
}
13+
14+
@Test
15+
void kMirror2() {
16+
assertThat(new Solution().kMirror(3, 7), equalTo(499L));
17+
}
18+
19+
@Test
20+
void kMirror3() {
21+
assertThat(new Solution().kMirror(7, 17), equalTo(20379000L));
22+
}
23+
}

0 commit comments

Comments
 (0)