Skip to content

Commit 0afa4e5

Browse files
committed
Added tasks 3516-3519
1 parent 1fa2f8e commit 0afa4e5

File tree

12 files changed

+523
-0
lines changed

12 files changed

+523
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g3501_3600.s3516_find_closest_person
2+
3+
// #Easy #Math #2025_04_14_Time_1_ms_(100.00%)_Space_41.06_MB_(100.00%)
4+
5+
import kotlin.math.abs
6+
7+
class Solution {
8+
fun findClosest(x: Int, y: Int, z: Int): Int {
9+
val d1 = abs(z - x)
10+
val d2 = abs(z - y)
11+
if (d1 == d2) {
12+
return 0
13+
} else if (d1 < d2) {
14+
return 1
15+
} else {
16+
return 2
17+
}
18+
}
19+
}
Lines changed: 62 additions & 0 deletions
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`
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g3501_3600.s3517_smallest_palindromic_rearrangement_i
2+
3+
// #Medium #String #Sorting #Counting_Sort #2025_04_14_Time_49_ms_(100.00%)_Space_52.03_MB_(100.00%)
4+
5+
class Solution {
6+
fun smallestPalindrome(s: String): String {
7+
val n = s.length
8+
val m = n / 2
9+
if (n == 1 || n == 2) {
10+
return s
11+
}
12+
val fArr = s.substring(0, m).toCharArray()
13+
fArr.sort()
14+
var f = String(fArr)
15+
val rev = StringBuilder(f).reverse()
16+
if (n % 2 == 1) {
17+
f += s[m]
18+
}
19+
return f + rev
20+
}
21+
}
Lines changed: 43 additions & 0 deletions
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.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package g3501_3600.s3518_smallest_palindromic_rearrangement_ii
2+
3+
// #Hard #String #Hash_Table #Math #Counting #Combinatorics
4+
// #2025_04_14_Time_27_ms_(100.00%)_Space_48.58_MB_(100.00%)
5+
6+
class Solution {
7+
fun smallestPalindrome(inputStr: String, k: Int): String {
8+
var k = k
9+
val frequency = IntArray(26)
10+
for (i in 0..<inputStr.length) {
11+
val ch = inputStr.get(i)
12+
frequency[ch.code - 'a'.code]++
13+
}
14+
var mid = 0.toChar()
15+
for (i in 0..25) {
16+
if (frequency[i] % 2 == 1) {
17+
mid = ('a'.code + i).toChar()
18+
frequency[i]--
19+
break
20+
}
21+
}
22+
val halfFreq = IntArray(26)
23+
var halfLength = 0
24+
for (i in 0..25) {
25+
halfFreq[i] = frequency[i] / 2
26+
halfLength += halfFreq[i]
27+
}
28+
val totalPerms = multinomial(halfFreq)
29+
if (k > totalPerms) {
30+
return ""
31+
}
32+
val firstHalfBuilder = StringBuilder()
33+
for (i in 0..<halfLength) {
34+
for (c in 0..25) {
35+
if (halfFreq[c] > 0) {
36+
halfFreq[c]--
37+
val perms = multinomial(halfFreq)
38+
if (perms >= k) {
39+
firstHalfBuilder.append(('a'.code + c).toChar())
40+
break
41+
} else {
42+
k -= perms.toInt()
43+
halfFreq[c]++
44+
}
45+
}
46+
}
47+
}
48+
val firstHalf = firstHalfBuilder.toString()
49+
val revHalf = StringBuilder(firstHalf).reverse().toString()
50+
val result: String
51+
if (mid.code == 0) {
52+
result = firstHalf + revHalf
53+
} else {
54+
result = firstHalf + mid + revHalf
55+
}
56+
return result
57+
}
58+
59+
private fun multinomial(counts: IntArray): Long {
60+
var tot = 0
61+
for (cnt in counts) {
62+
tot += cnt
63+
}
64+
var res: Long = 1
65+
for (i in 0..25) {
66+
val cnt = counts[i]
67+
res = res * binom(tot, cnt)
68+
if (res >= MAX_K) {
69+
return MAX_K
70+
}
71+
tot -= cnt
72+
}
73+
return res
74+
}
75+
76+
private fun binom(n: Int, k: Int): Long {
77+
var k = k
78+
if (k > n) {
79+
return 0
80+
}
81+
if (k > n - k) {
82+
k = n - k
83+
}
84+
var result: Long = 1
85+
for (i in 1..k) {
86+
result = result * (n - i + 1) / i
87+
if (result >= MAX_K) {
88+
return MAX_K
89+
}
90+
}
91+
return result
92+
}
93+
94+
companion object {
95+
private const val MAX_K: Long = 1000001
96+
}
97+
}
Lines changed: 49 additions & 0 deletions
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>
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package g3501_3600.s3519_count_numbers_with_non_decreasing_digits
2+
3+
// #Hard #String #Dynamic_Programming #Math
4+
// #2025_04_14_Time_31_ms_(100.00%)_Space_46.39_MB_(100.00%)
5+
6+
class Solution {
7+
fun countNumbers(l: String, r: String, b: Int): Int {
8+
val ans1 = find(r.toCharArray(), b)
9+
val start = subTractOne(l.toCharArray())
10+
val ans2 = find(start, b)
11+
return ((ans1 - ans2) % 1000000007L).toInt()
12+
}
13+
14+
private fun find(arr: CharArray, b: Int): Long {
15+
val nums = convertNumToBase(arr, b)
16+
val dp = Array<Array<Array<Long?>>>(nums.size) { Array<Array<Long?>>(2) { arrayOfNulls<Long>(11) } }
17+
return solve(0, nums, 1, b, 0, dp) - 1
18+
}
19+
20+
private fun solve(i: Int, arr: IntArray, tight: Int, base: Int, last: Int, dp: Array<Array<Array<Long?>>>): Long {
21+
if (i == arr.size) {
22+
return 1L
23+
}
24+
if (dp[i][tight][last] != null) {
25+
return dp[i][tight][last]!!
26+
}
27+
var till = base - 1
28+
if (tight == 1) {
29+
till = arr[i]
30+
}
31+
var ans: Long = 0
32+
for (j in 0..till) {
33+
if (j >= last) {
34+
ans = (ans + solve(i + 1, arr, tight and (if (j == arr[i]) 1 else 0), base, j, dp))
35+
}
36+
}
37+
dp[i][tight][last] = ans
38+
return ans
39+
}
40+
41+
private fun subTractOne(arr: CharArray): CharArray {
42+
val n = arr.size
43+
var i = n - 1
44+
while (i >= 0 && arr[i] == '0') {
45+
arr[i--] = '9'
46+
}
47+
val x = arr[i].code - '0'.code - 1
48+
arr[i] = (x + '0'.code).toChar()
49+
var j = 0
50+
var idx = 0
51+
while (j < n && arr[j] == '0') {
52+
j++
53+
}
54+
val res = CharArray(n - j)
55+
for (k in j..<n) {
56+
res[idx++] = arr[k]
57+
}
58+
return res
59+
}
60+
61+
private fun convertNumToBase(arr: CharArray, base: Int): IntArray {
62+
val n = arr.size
63+
var num = IntArray(n)
64+
var i = 0
65+
while (i < n) {
66+
num[i] = arr[i++].code - '0'.code
67+
}
68+
val temp: MutableList<Int?> = ArrayList<Int?>()
69+
var len = n
70+
while (len > 0) {
71+
var rem = 0
72+
val next = IntArray(len)
73+
var newLen = 0
74+
var j = 0
75+
while (j < len) {
76+
val cur = rem * 10L + num[j]
77+
val q = (cur / base).toInt()
78+
rem = (cur % base).toInt()
79+
if (newLen > 0 || q != 0) {
80+
next[newLen] = q
81+
newLen++
82+
}
83+
j++
84+
}
85+
temp.add(rem)
86+
num = next
87+
len = newLen
88+
}
89+
val res = IntArray(temp.size)
90+
var k = 0
91+
val size = temp.size
92+
while (k < size) {
93+
res[k] = temp[size - 1 - k]!!
94+
k++
95+
}
96+
return res
97+
}
98+
}

0 commit comments

Comments
 (0)