Skip to content

Commit 1776443

Browse files
committed
Added tasks 3516-3519
1 parent 0fad90c commit 1776443

File tree

12 files changed

+512
-0
lines changed

12 files changed

+512
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g3501_3600.s3516_find_closest_person;
2+
3+
// #Easy #Math #2025_04_14_Time_0_ms_(100.00%)_Space_41.20_MB_(_%)
4+
5+
public class Solution {
6+
public int findClosest(int x, int y, int z) {
7+
int d1 = Math.abs(z - x);
8+
int d2 = Math.abs(z - y);
9+
if (d1 == d2) {
10+
return 0;
11+
} else if (d1 < d2) {
12+
return 1;
13+
} else {
14+
return 2;
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
3516\. Find Closest Person
2+
3+
Easy
4+
5+
You are given three integers `x`, `y`, and `z`, representing the positions of three people on a number line:
6+
7+
* `x` is the position of Person 1.
8+
* `y` is the position of Person 2.
9+
* `z` is the position of Person 3, who does **not** move.
10+
11+
Both Person 1 and Person 2 move toward Person 3 at the **same** speed.
12+
13+
Determine which person reaches Person 3 **first**:
14+
15+
* Return 1 if Person 1 arrives first.
16+
* Return 2 if Person 2 arrives first.
17+
* Return 0 if both arrive at the **same** time.
18+
19+
Return the result accordingly.
20+
21+
**Example 1:**
22+
23+
**Input:** x = 2, y = 7, z = 4
24+
25+
**Output:** 1
26+
27+
**Explanation:**
28+
29+
* Person 1 is at position 2 and can reach Person 3 (at position 4) in 2 steps.
30+
* Person 2 is at position 7 and can reach Person 3 in 3 steps.
31+
32+
Since Person 1 reaches Person 3 first, the output is 1.
33+
34+
**Example 2:**
35+
36+
**Input:** x = 2, y = 5, z = 6
37+
38+
**Output:** 2
39+
40+
**Explanation:**
41+
42+
* Person 1 is at position 2 and can reach Person 3 (at position 6) in 4 steps.
43+
* Person 2 is at position 5 and can reach Person 3 in 1 step.
44+
45+
Since Person 2 reaches Person 3 first, the output is 2.
46+
47+
**Example 3:**
48+
49+
**Input:** x = 1, y = 5, z = 3
50+
51+
**Output:** 0
52+
53+
**Explanation:**
54+
55+
* Person 1 is at position 1 and can reach Person 3 (at position 3) in 2 steps.
56+
* Person 2 is at position 5 and can reach Person 3 in 2 steps.
57+
58+
Since both Person 1 and Person 2 reach Person 3 at the same time, the output is 0.
59+
60+
**Constraints:**
61+
62+
* `1 <= x, y, z <= 100`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3501_3600.s3517_smallest_palindromic_rearrangement_i;
2+
3+
// #Medium #String #Sorting #Counting_Sort #2025_04_14_Time_33_ms_(100.00%)_Space_46.07_MB_(100.00%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public String smallestPalindrome(String s) {
9+
int n = s.length();
10+
int m = n / 2;
11+
if (n == 1 || n == 2) {
12+
return s;
13+
}
14+
char[] fArr = s.substring(0, m).toCharArray();
15+
Arrays.sort(fArr);
16+
String f = new String(fArr);
17+
StringBuilder rev = new StringBuilder(f).reverse();
18+
if (n % 2 == 1) {
19+
f += s.charAt(m);
20+
}
21+
return f + rev;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
3517\. Smallest Palindromic Rearrangement I
2+
3+
Medium
4+
5+
You are given a **palindromic** string `s`.
6+
7+
Return the **lexicographically smallest** palindromic permutation of `s`.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "z"
12+
13+
**Output:** "z"
14+
15+
**Explanation:**
16+
17+
A string of only one character is already the lexicographically smallest palindrome.
18+
19+
**Example 2:**
20+
21+
**Input:** s = "babab"
22+
23+
**Output:** "abbba"
24+
25+
**Explanation:**
26+
27+
Rearranging `"babab"``"abbba"` gives the smallest lexicographic palindrome.
28+
29+
**Example 3:**
30+
31+
**Input:** s = "daccad"
32+
33+
**Output:** "acddca"
34+
35+
**Explanation:**
36+
37+
Rearranging `"daccad"``"acddca"` gives the smallest lexicographic palindrome.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= s.length <= 10<sup>5</sup></code>
42+
* `s` consists of lowercase English letters.
43+
* `s` is guaranteed to be palindromic.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package g3501_3600.s3518_smallest_palindromic_rearrangement_ii;
2+
3+
// #Hard #String #Hash_Table #Math #Counting #Combinatorics
4+
// #2025_04_14_Time_34_ms_(100.00%)_Space_45.64_MB_(100.00%)
5+
6+
public class Solution {
7+
private static final long MAX_K = 1000001;
8+
9+
public String smallestPalindrome(String inputStr, int k) {
10+
int[] frequency = new int[26];
11+
for (int i = 0; i < inputStr.length(); i++) {
12+
char ch = inputStr.charAt(i);
13+
frequency[ch - 'a']++;
14+
}
15+
char mid = 0;
16+
for (int i = 0; i < 26; i++) {
17+
if (frequency[i] % 2 == 1) {
18+
mid = (char) ('a' + i);
19+
frequency[i]--;
20+
break;
21+
}
22+
}
23+
int[] halfFreq = new int[26];
24+
int halfLength = 0;
25+
for (int i = 0; i < 26; i++) {
26+
halfFreq[i] = frequency[i] / 2;
27+
halfLength += halfFreq[i];
28+
}
29+
long totalPerms = multinomial(halfFreq);
30+
if (k > totalPerms) {
31+
return "";
32+
}
33+
StringBuilder firstHalfBuilder = new StringBuilder();
34+
for (int i = 0; i < halfLength; i++) {
35+
for (int c = 0; c < 26; c++) {
36+
if (halfFreq[c] > 0) {
37+
halfFreq[c]--;
38+
long perms = multinomial(halfFreq);
39+
if (perms >= k) {
40+
firstHalfBuilder.append((char) ('a' + c));
41+
break;
42+
} else {
43+
k -= (int) perms;
44+
halfFreq[c]++;
45+
}
46+
}
47+
}
48+
}
49+
String firstHalf = firstHalfBuilder.toString();
50+
String revHalf = new StringBuilder(firstHalf).reverse().toString();
51+
String result;
52+
if (mid == 0) {
53+
result = firstHalf + revHalf;
54+
} else {
55+
result = firstHalf + mid + revHalf;
56+
}
57+
return result;
58+
}
59+
60+
private long multinomial(int[] counts) {
61+
int tot = 0;
62+
for (int cnt : counts) {
63+
tot += cnt;
64+
}
65+
long res = 1;
66+
for (int i = 0; i < 26; i++) {
67+
int cnt = counts[i];
68+
res = res * binom(tot, cnt);
69+
if (res >= MAX_K) {
70+
return MAX_K;
71+
}
72+
tot -= cnt;
73+
}
74+
return res;
75+
}
76+
77+
private long binom(int n, int k) {
78+
if (k > n) {
79+
return 0;
80+
}
81+
if (k > n - k) {
82+
k = n - k;
83+
}
84+
long result = 1;
85+
for (int i = 1; i <= k; i++) {
86+
result = result * (n - i + 1) / i;
87+
if (result >= MAX_K) {
88+
return MAX_K;
89+
}
90+
}
91+
return result;
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
3518\. Smallest Palindromic Rearrangement II
2+
3+
Hard
4+
5+
You are given a **palindromic** string `s` and an integer `k`.
6+
7+
Return the **k-th** **lexicographically smallest** palindromic permutation of `s`. If there are fewer than `k` distinct palindromic permutations, return an empty string.
8+
9+
**Note:** Different rearrangements that yield the same palindromic string are considered identical and are counted once.
10+
11+
**Example 1:**
12+
13+
**Input:** s = "abba", k = 2
14+
15+
**Output:** "baab"
16+
17+
**Explanation:**
18+
19+
* The two distinct palindromic rearrangements of `"abba"` are `"abba"` and `"baab"`.
20+
* Lexicographically, `"abba"` comes before `"baab"`. Since `k = 2`, the output is `"baab"`.
21+
22+
**Example 2:**
23+
24+
**Input:** s = "aa", k = 2
25+
26+
**Output:** ""
27+
28+
**Explanation:**
29+
30+
* There is only one palindromic rearrangement: `"aa"`.
31+
* The output is an empty string since `k = 2` exceeds the number of possible rearrangements.
32+
33+
**Example 3:**
34+
35+
**Input:** s = "bacab", k = 1
36+
37+
**Output:** "abcba"
38+
39+
**Explanation:**
40+
41+
* The two distinct palindromic rearrangements of `"bacab"` are `"abcba"` and `"bacab"`.
42+
* Lexicographically, `"abcba"` comes before `"bacab"`. Since `k = 1`, the output is `"abcba"`.
43+
44+
**Constraints:**
45+
46+
* <code>1 <= s.length <= 10<sup>4</sup></code>
47+
* `s` consists of lowercase English letters.
48+
* `s` is guaranteed to be palindromic.
49+
* <code>1 <= k <= 10<sup>6</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package g3501_3600.s3519_count_numbers_with_non_decreasing_digits;
2+
3+
// #Hard #String #Dynamic_Programming #Math #2025_04_14_Time_19_ms_(100.00%)_Space_45.43_MB_(50.00%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class Solution {
9+
public int countNumbers(String l, String r, int b) {
10+
long ans1 = find(r.toCharArray(), b);
11+
char[] start = subTractOne(l.toCharArray());
12+
long ans2 = find(start, b);
13+
return (int) ((ans1 - ans2) % 1000000007L);
14+
}
15+
16+
private long find(char[] arr, int b) {
17+
int[] nums = convertNumToBase(arr, b);
18+
Long[][][] dp = new Long[nums.length][2][11];
19+
return solve(0, nums, 1, b, 0, dp) - 1;
20+
}
21+
22+
private long solve(int i, int[] arr, int tight, int base, int last, Long[][][] dp) {
23+
if (i == arr.length) {
24+
return 1L;
25+
}
26+
if (dp[i][tight][last] != null) {
27+
return dp[i][tight][last];
28+
}
29+
int till = base - 1;
30+
if (tight == 1) {
31+
till = arr[i];
32+
}
33+
long ans = 0;
34+
for (int j = 0; j <= till; j++) {
35+
if (j >= last) {
36+
ans = (ans + solve(i + 1, arr, tight & (j == arr[i] ? 1 : 0), base, j, dp));
37+
}
38+
}
39+
return dp[i][tight][last] = ans;
40+
}
41+
42+
private char[] subTractOne(char[] arr) {
43+
int n = arr.length;
44+
int i = n - 1;
45+
while (i >= 0 && arr[i] == '0') {
46+
arr[i--] = '9';
47+
}
48+
int x = arr[i] - '0' - 1;
49+
arr[i] = (char) (x + '0');
50+
int j = 0;
51+
int idx = 0;
52+
while (j < n && arr[j] == '0') {
53+
j++;
54+
}
55+
char[] res = new char[n - j];
56+
for (int k = j; k < n; k++) {
57+
res[idx++] = arr[k];
58+
}
59+
return res;
60+
}
61+
62+
private int[] convertNumToBase(char[] arr, int base) {
63+
int n = arr.length;
64+
int[] num = new int[n];
65+
int i = 0;
66+
while (i < n) {
67+
num[i] = arr[i++] - '0';
68+
}
69+
List<Integer> temp = new ArrayList<>();
70+
int len = n;
71+
while (len > 0) {
72+
int rem = 0;
73+
int[] next = new int[len];
74+
int newLen = 0;
75+
int j = 0;
76+
while (j < len) {
77+
long cur = rem * 10L + num[j];
78+
int q = (int) (cur / base);
79+
rem = (int) (cur % base);
80+
if (newLen > 0 || q != 0) {
81+
next[newLen] = q;
82+
newLen++;
83+
}
84+
j++;
85+
}
86+
temp.add(rem);
87+
num = next;
88+
len = newLen;
89+
}
90+
int[] res = new int[temp.size()];
91+
int k = 0;
92+
int size = temp.size();
93+
while (k < size) {
94+
res[k] = temp.get(size - 1 - k);
95+
k++;
96+
}
97+
return res;
98+
}
99+
}

0 commit comments

Comments
 (0)