Skip to content

Commit 229b901

Browse files
authored
Added tasks 3270-3277
1 parent deddeb8 commit 229b901

File tree

24 files changed

+908
-0
lines changed

24 files changed

+908
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g3201_3300.s3270_find_the_key_of_the_numbers
2+
3+
// #Easy #Math #2024_09_04_Time_122_ms_(97.83%)_Space_34_MB_(65.22%)
4+
5+
import kotlin.math.min
6+
7+
class Solution {
8+
fun generateKey(num1: Int, num2: Int, num3: Int): Int {
9+
val s1 = (
10+
min(
11+
num1 / 1000 % 10,
12+
min(
13+
num2 / 1000 % 10,
14+
num3 / 1000 % 10
15+
)
16+
) * 1000
17+
)
18+
val s2 = (
19+
min(
20+
num1 / 100 % 10,
21+
min(
22+
num2 / 100 % 10,
23+
num3 / 100 % 10
24+
)
25+
) * 100
26+
)
27+
val s3 =
28+
(
29+
min(
30+
num1 / 10 % 10,
31+
min(
32+
num2 / 10 % 10,
33+
num3 / 10 % 10
34+
)
35+
) * 10
36+
)
37+
val s4 = min(
38+
num1 % 10,
39+
min(num2 % 10, num3 % 10)
40+
)
41+
return s1 + s2 + s3 + s4
42+
}
43+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
3270\. Find the Key of the Numbers
2+
3+
Easy
4+
5+
You are given three **positive** integers `num1`, `num2`, and `num3`.
6+
7+
The `key` of `num1`, `num2`, and `num3` is defined as a four-digit number such that:
8+
9+
* Initially, if any number has **less than** four digits, it is padded with **leading zeros**.
10+
* The <code>i<sup>th</sup></code> digit (`1 <= i <= 4`) of the `key` is generated by taking the **smallest** digit among the <code>i<sup>th</sup></code> digits of `num1`, `num2`, and `num3`.
11+
12+
Return the `key` of the three numbers **without** leading zeros (_if any_).
13+
14+
**Example 1:**
15+
16+
**Input:** num1 = 1, num2 = 10, num3 = 1000
17+
18+
**Output:** 0
19+
20+
**Explanation:**
21+
22+
On padding, `num1` becomes `"0001"`, `num2` becomes `"0010"`, and `num3` remains `"1000"`.
23+
24+
* The <code>1<sup>st</sup></code> digit of the `key` is `min(0, 0, 1)`.
25+
* The <code>2<sup>nd</sup></code> digit of the `key` is `min(0, 0, 0)`.
26+
* The <code>3<sup>rd</sup></code> digit of the `key` is `min(0, 1, 0)`.
27+
* The <code>4<sup>th</sup></code> digit of the `key` is `min(1, 0, 0)`.
28+
29+
Hence, the `key` is `"0000"`, i.e. 0.
30+
31+
**Example 2:**
32+
33+
**Input:** num1 = 987, num2 = 879, num3 = 798
34+
35+
**Output:** 777
36+
37+
**Example 3:**
38+
39+
**Input:** num1 = 1, num2 = 2, num3 = 3
40+
41+
**Output:** 1
42+
43+
**Constraints:**
44+
45+
* `1 <= num1, num2, num3 <= 9999`
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g3201_3300.s3271_hash_divided_string
2+
3+
// #Medium #String #Simulation #2024_09_04_Time_178_ms_(100.00%)_Space_36.9_MB_(97.50%)
4+
5+
class Solution {
6+
fun stringHash(s: String, k: Int): String {
7+
val result = StringBuilder()
8+
var i = 0
9+
var sum = 0
10+
while (i < s.length) {
11+
sum += s[i].code - 'a'.code
12+
if ((i + 1) % k == 0) {
13+
result.append(('a'.code + sum % 26).toChar())
14+
sum = 0
15+
}
16+
i++
17+
}
18+
return result.toString()
19+
}
20+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
3271\. Hash Divided String
2+
3+
Medium
4+
5+
You are given a string `s` of length `n` and an integer `k`, where `n` is a **multiple** of `k`. Your task is to hash the string `s` into a new string called `result`, which has a length of `n / k`.
6+
7+
First, divide `s` into `n / k` **substrings**, each with a length of `k`. Then, initialize `result` as an **empty** string.
8+
9+
For each **substring** in order from the beginning:
10+
11+
* The **hash value** of a character is the index of that character in the **English alphabet** (e.g., `'a' → 0`, `'b' → 1`, ..., `'z' → 25`).
12+
* Calculate the _sum_ of all the **hash values** of the characters in the substring.
13+
* Find the remainder of this sum when divided by 26, which is called `hashedChar`.
14+
* Identify the character in the English lowercase alphabet that corresponds to `hashedChar`.
15+
* Append that character to the end of `result`.
16+
17+
Return `result`.
18+
19+
**Example 1:**
20+
21+
**Input:** s = "abcd", k = 2
22+
23+
**Output:** "bf"
24+
25+
**Explanation:**
26+
27+
First substring: `"ab"`, `0 + 1 = 1`, `1 % 26 = 1`, `result[0] = 'b'`.
28+
29+
Second substring: `"cd"`, `2 + 3 = 5`, `5 % 26 = 5`, `result[1] = 'f'`.
30+
31+
**Example 2:**
32+
33+
**Input:** s = "mxz", k = 3
34+
35+
**Output:** "i"
36+
37+
**Explanation:**
38+
39+
The only substring: `"mxz"`, `12 + 23 + 25 = 60`, `60 % 26 = 8`, `result[0] = 'i'`.
40+
41+
**Constraints:**
42+
43+
* `1 <= k <= 100`
44+
* `k <= s.length <= 1000`
45+
* `s.length` is divisible by `k`.
46+
* `s` consists only of lowercase English letters.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package g3201_3300.s3272_find_the_count_of_good_integers
2+
3+
// #Hard #Hash_Table #Math #Enumeration #Combinatorics
4+
// #2024_09_04_Time_452_ms_(80.00%)_Space_53.5_MB_(60.00%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
private val palindromes: MutableList<String> = ArrayList()
10+
11+
private fun factorial(n: Int): Long {
12+
var res: Long = 1
13+
for (i in 2..n) {
14+
res *= i.toLong()
15+
}
16+
return res
17+
}
18+
19+
private fun countDigits(s: String): MutableMap<Char, Int> {
20+
val freq: MutableMap<Char, Int> = HashMap()
21+
for (c in s.toCharArray()) {
22+
freq[c] = freq.getOrDefault(c, 0) + 1
23+
}
24+
return freq
25+
}
26+
27+
private fun calculatePermutations(freq: Map<Char, Int>, length: Int): Long {
28+
var totalPermutations = factorial(length)
29+
for (count in freq.values) {
30+
totalPermutations /= factorial(count)
31+
}
32+
return totalPermutations
33+
}
34+
35+
private fun calculateValidPermutations(s: String): Long {
36+
val freq = countDigits(s)
37+
val n = s.length
38+
var totalPermutations = calculatePermutations(freq, n)
39+
if (freq.getOrDefault('0', 0) > 0) {
40+
freq['0'] = freq['0']!! - 1
41+
val invalidPermutations = calculatePermutations(freq, n - 1)
42+
totalPermutations -= invalidPermutations
43+
}
44+
return totalPermutations
45+
}
46+
47+
private fun generatePalindromes(
48+
f: Int,
49+
r: Int,
50+
k: Int,
51+
lb: Int,
52+
sum: Int,
53+
ans: StringBuilder,
54+
rem: IntArray
55+
) {
56+
if (f > r) {
57+
if (sum == 0) {
58+
palindromes.add(ans.toString())
59+
}
60+
return
61+
}
62+
for (i in lb..9) {
63+
ans.setCharAt(f, ('0'.code + i).toChar())
64+
ans.setCharAt(r, ('0'.code + i).toChar())
65+
var chk = sum
66+
chk = (chk + rem[f] * i) % k
67+
if (f != r) {
68+
chk = (chk + rem[r] * i) % k
69+
}
70+
generatePalindromes(f + 1, r - 1, k, 0, chk, ans, rem)
71+
}
72+
}
73+
74+
private fun allKPalindromes(n: Int, k: Int): List<String> {
75+
val ans = StringBuilder(n)
76+
ans.append("0".repeat(max(0.0, n.toDouble()).toInt()))
77+
val rem = IntArray(n)
78+
rem[0] = 1
79+
for (i in 1 until n) {
80+
rem[i] = (rem[i - 1] * 10) % k
81+
}
82+
palindromes.clear()
83+
generatePalindromes(0, n - 1, k, 1, 0, ans, rem)
84+
return palindromes
85+
}
86+
87+
fun countGoodIntegers(n: Int, k: Int): Long {
88+
val ans = allKPalindromes(n, k)
89+
val st: MutableSet<String> = HashSet()
90+
for (str in ans) {
91+
val arr = str.toCharArray()
92+
arr.sort()
93+
st.add(String(arr))
94+
}
95+
val v: List<String> = ArrayList(st)
96+
var chk: Long = 0
97+
for (str in v) {
98+
val cc = calculateValidPermutations(str)
99+
chk += cc
100+
}
101+
return chk
102+
}
103+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
3272\. Find the Count of Good Integers
2+
3+
Hard
4+
5+
You are given two **positive** integers `n` and `k`.
6+
7+
An integer `x` is called **k-palindromic** if:
8+
9+
* `x` is a palindrome.
10+
* `x` is divisible by `k`.
11+
12+
An integer is called **good** if its digits can be _rearranged_ to form a **k-palindromic** integer. For example, for `k = 2`, 2020 can be rearranged to form the _k-palindromic_ integer 2002, whereas 1010 cannot be rearranged to form a _k-palindromic_ integer.
13+
14+
Return the count of **good** integers containing `n` digits.
15+
16+
**Note** that _any_ integer must **not** have leading zeros, **neither** before **nor** after rearrangement. For example, 1010 _cannot_ be rearranged to form 101.
17+
18+
**Example 1:**
19+
20+
**Input:** n = 3, k = 5
21+
22+
**Output:** 27
23+
24+
**Explanation:**
25+
26+
_Some_ of the good integers are:
27+
28+
* 551 because it can be rearranged to form 515.
29+
* 525 because it is already k-palindromic.
30+
31+
**Example 2:**
32+
33+
**Input:** n = 1, k = 4
34+
35+
**Output:** 2
36+
37+
**Explanation:**
38+
39+
The two good integers are 4 and 8.
40+
41+
**Example 3:**
42+
43+
**Input:** n = 5, k = 6
44+
45+
**Output:** 2468
46+
47+
**Constraints:**
48+
49+
* `1 <= n <= 10`
50+
* `1 <= k <= 9`
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g3201_3300.s3273_minimum_amount_of_damage_dealt_to_bob
2+
3+
// #Hard #Array #Sorting #Greedy #2024_09_04_Time_793_ms_(90.00%)_Space_67.1_MB_(55.00%)
4+
5+
class Solution {
6+
fun minDamage(pw: Int, damage: IntArray, health: IntArray): Long {
7+
var res: Long = 0
8+
var sum: Long = 0
9+
for (e in damage) {
10+
sum += e.toLong()
11+
}
12+
val pairs = arrayOfNulls<Pair>(damage.size)
13+
for (e in damage.indices) {
14+
pairs[e] = Pair(damage[e], (health[e] + pw - 1) / pw)
15+
}
16+
pairs.sort()
17+
for (pr in pairs) {
18+
res += pr!!.`val` * sum
19+
sum -= pr.key.toLong()
20+
}
21+
return res
22+
}
23+
24+
internal class Pair(var key: Int, var `val`: Int) : Comparable<Pair> {
25+
override fun compareTo(p: Pair): Int {
26+
return `val` * p.key - key * p.`val`
27+
}
28+
}
29+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
3273\. Minimum Amount of Damage Dealt to Bob
2+
3+
Hard
4+
5+
You are given an integer `power` and two integer arrays `damage` and `health`, both having length `n`.
6+
7+
Bob has `n` enemies, where enemy `i` will deal Bob `damage[i]` **points** of damage per second while they are _alive_ (i.e. `health[i] > 0`).
8+
9+
Every second, **after** the enemies deal damage to Bob, he chooses **one** of the enemies that is still _alive_ and deals `power` points of damage to them.
10+
11+
Determine the **minimum** total amount of damage points that will be dealt to Bob before **all** `n` enemies are _dead_.
12+
13+
**Example 1:**
14+
15+
**Input:** power = 4, damage = [1,2,3,4], health = [4,5,6,8]
16+
17+
**Output:** 39
18+
19+
**Explanation:**
20+
21+
* Attack enemy 3 in the first two seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is `10 + 10 = 20` points.
22+
* Attack enemy 2 in the next two seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is `6 + 6 = 12` points.
23+
* Attack enemy 0 in the next second, after which enemy 0 will go down, the number of damage points dealt to Bob is `3` points.
24+
* Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is `2 + 2 = 4` points.
25+
26+
**Example 2:**
27+
28+
**Input:** power = 1, damage = [1,1,1,1], health = [1,2,3,4]
29+
30+
**Output:** 20
31+
32+
**Explanation:**
33+
34+
* Attack enemy 0 in the first second, after which enemy 0 will go down, the number of damage points dealt to Bob is `4` points.
35+
* Attack enemy 1 in the next two seconds, after which enemy 1 will go down, the number of damage points dealt to Bob is `3 + 3 = 6` points.
36+
* Attack enemy 2 in the next three seconds, after which enemy 2 will go down, the number of damage points dealt to Bob is `2 + 2 + 2 = 6` points.
37+
* Attack enemy 3 in the next four seconds, after which enemy 3 will go down, the number of damage points dealt to Bob is `1 + 1 + 1 + 1 = 4` points.
38+
39+
**Example 3:**
40+
41+
**Input:** power = 8, damage = [40], health = [59]
42+
43+
**Output:** 320
44+
45+
**Constraints:**
46+
47+
* <code>1 <= power <= 10<sup>4</sup></code>
48+
* <code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code>
49+
* <code>1 <= damage[i], health[i] <= 10<sup>4</sup></code>

0 commit comments

Comments
 (0)