Skip to content

Added tasks 3270-3277 #688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package g3201_3300.s3270_find_the_key_of_the_numbers

// #Easy #Math #2024_09_04_Time_122_ms_(97.83%)_Space_34_MB_(65.22%)

import kotlin.math.min

class Solution {
fun generateKey(num1: Int, num2: Int, num3: Int): Int {
val s1 = (
min(
num1 / 1000 % 10,
min(
num2 / 1000 % 10,
num3 / 1000 % 10
)
) * 1000
)
val s2 = (
min(
num1 / 100 % 10,
min(
num2 / 100 % 10,
num3 / 100 % 10
)
) * 100
)
val s3 =
(
min(
num1 / 10 % 10,
min(
num2 / 10 % 10,
num3 / 10 % 10
)
) * 10
)
val s4 = min(
num1 % 10,
min(num2 % 10, num3 % 10)
)
return s1 + s2 + s3 + s4
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
3270\. Find the Key of the Numbers

Easy

You are given three **positive** integers `num1`, `num2`, and `num3`.

The `key` of `num1`, `num2`, and `num3` is defined as a four-digit number such that:

* Initially, if any number has **less than** four digits, it is padded with **leading zeros**.
* 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`.

Return the `key` of the three numbers **without** leading zeros (_if any_).

**Example 1:**

**Input:** num1 = 1, num2 = 10, num3 = 1000

**Output:** 0

**Explanation:**

On padding, `num1` becomes `"0001"`, `num2` becomes `"0010"`, and `num3` remains `"1000"`.

* The <code>1<sup>st</sup></code> digit of the `key` is `min(0, 0, 1)`.
* The <code>2<sup>nd</sup></code> digit of the `key` is `min(0, 0, 0)`.
* The <code>3<sup>rd</sup></code> digit of the `key` is `min(0, 1, 0)`.
* The <code>4<sup>th</sup></code> digit of the `key` is `min(1, 0, 0)`.

Hence, the `key` is `"0000"`, i.e. 0.

**Example 2:**

**Input:** num1 = 987, num2 = 879, num3 = 798

**Output:** 777

**Example 3:**

**Input:** num1 = 1, num2 = 2, num3 = 3

**Output:** 1

**Constraints:**

* `1 <= num1, num2, num3 <= 9999`
20 changes: 20 additions & 0 deletions src/main/kotlin/g3201_3300/s3271_hash_divided_string/Solution.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g3201_3300.s3271_hash_divided_string

// #Medium #String #Simulation #2024_09_04_Time_178_ms_(100.00%)_Space_36.9_MB_(97.50%)

class Solution {
fun stringHash(s: String, k: Int): String {
val result = StringBuilder()
var i = 0
var sum = 0
while (i < s.length) {
sum += s[i].code - 'a'.code
if ((i + 1) % k == 0) {
result.append(('a'.code + sum % 26).toChar())
sum = 0
}
i++
}
return result.toString()
}
}
46 changes: 46 additions & 0 deletions src/main/kotlin/g3201_3300/s3271_hash_divided_string/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
3271\. Hash Divided String

Medium

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`.

First, divide `s` into `n / k` **substrings**, each with a length of `k`. Then, initialize `result` as an **empty** string.

For each **substring** in order from the beginning:

* The **hash value** of a character is the index of that character in the **English alphabet** (e.g., `'a' → 0`, `'b' → 1`, ..., `'z' → 25`).
* Calculate the _sum_ of all the **hash values** of the characters in the substring.
* Find the remainder of this sum when divided by 26, which is called `hashedChar`.
* Identify the character in the English lowercase alphabet that corresponds to `hashedChar`.
* Append that character to the end of `result`.

Return `result`.

**Example 1:**

**Input:** s = "abcd", k = 2

**Output:** "bf"

**Explanation:**

First substring: `"ab"`, `0 + 1 = 1`, `1 % 26 = 1`, `result[0] = 'b'`.

Second substring: `"cd"`, `2 + 3 = 5`, `5 % 26 = 5`, `result[1] = 'f'`.

**Example 2:**

**Input:** s = "mxz", k = 3

**Output:** "i"

**Explanation:**

The only substring: `"mxz"`, `12 + 23 + 25 = 60`, `60 % 26 = 8`, `result[0] = 'i'`.

**Constraints:**

* `1 <= k <= 100`
* `k <= s.length <= 1000`
* `s.length` is divisible by `k`.
* `s` consists only of lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package g3201_3300.s3272_find_the_count_of_good_integers

// #Hard #Hash_Table #Math #Enumeration #Combinatorics
// #2024_09_04_Time_452_ms_(80.00%)_Space_53.5_MB_(60.00%)

import kotlin.math.max

class Solution {
private val palindromes: MutableList<String> = ArrayList()

private fun factorial(n: Int): Long {
var res: Long = 1
for (i in 2..n) {
res *= i.toLong()
}
return res
}

private fun countDigits(s: String): MutableMap<Char, Int> {
val freq: MutableMap<Char, Int> = HashMap()
for (c in s.toCharArray()) {
freq[c] = freq.getOrDefault(c, 0) + 1
}
return freq
}

private fun calculatePermutations(freq: Map<Char, Int>, length: Int): Long {
var totalPermutations = factorial(length)
for (count in freq.values) {
totalPermutations /= factorial(count)
}
return totalPermutations
}

private fun calculateValidPermutations(s: String): Long {
val freq = countDigits(s)
val n = s.length
var totalPermutations = calculatePermutations(freq, n)
if (freq.getOrDefault('0', 0) > 0) {
freq['0'] = freq['0']!! - 1
val invalidPermutations = calculatePermutations(freq, n - 1)
totalPermutations -= invalidPermutations
}
return totalPermutations
}

private fun generatePalindromes(
f: Int,
r: Int,
k: Int,
lb: Int,
sum: Int,
ans: StringBuilder,
rem: IntArray
) {
if (f > r) {
if (sum == 0) {
palindromes.add(ans.toString())
}
return
}
for (i in lb..9) {
ans.setCharAt(f, ('0'.code + i).toChar())
ans.setCharAt(r, ('0'.code + i).toChar())
var chk = sum
chk = (chk + rem[f] * i) % k
if (f != r) {
chk = (chk + rem[r] * i) % k
}
generatePalindromes(f + 1, r - 1, k, 0, chk, ans, rem)
}
}

private fun allKPalindromes(n: Int, k: Int): List<String> {
val ans = StringBuilder(n)
ans.append("0".repeat(max(0.0, n.toDouble()).toInt()))
val rem = IntArray(n)
rem[0] = 1
for (i in 1 until n) {
rem[i] = (rem[i - 1] * 10) % k
}
palindromes.clear()
generatePalindromes(0, n - 1, k, 1, 0, ans, rem)
return palindromes
}

fun countGoodIntegers(n: Int, k: Int): Long {
val ans = allKPalindromes(n, k)
val st: MutableSet<String> = HashSet()
for (str in ans) {
val arr = str.toCharArray()
arr.sort()
st.add(String(arr))
}
val v: List<String> = ArrayList(st)
var chk: Long = 0
for (str in v) {
val cc = calculateValidPermutations(str)
chk += cc
}
return chk
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
3272\. Find the Count of Good Integers

Hard

You are given two **positive** integers `n` and `k`.

An integer `x` is called **k-palindromic** if:

* `x` is a palindrome.
* `x` is divisible by `k`.

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.

Return the count of **good** integers containing `n` digits.

**Note** that _any_ integer must **not** have leading zeros, **neither** before **nor** after rearrangement. For example, 1010 _cannot_ be rearranged to form 101.

**Example 1:**

**Input:** n = 3, k = 5

**Output:** 27

**Explanation:**

_Some_ of the good integers are:

* 551 because it can be rearranged to form 515.
* 525 because it is already k-palindromic.

**Example 2:**

**Input:** n = 1, k = 4

**Output:** 2

**Explanation:**

The two good integers are 4 and 8.

**Example 3:**

**Input:** n = 5, k = 6

**Output:** 2468

**Constraints:**

* `1 <= n <= 10`
* `1 <= k <= 9`
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package g3201_3300.s3273_minimum_amount_of_damage_dealt_to_bob

// #Hard #Array #Sorting #Greedy #2024_09_04_Time_793_ms_(90.00%)_Space_67.1_MB_(55.00%)

class Solution {
fun minDamage(pw: Int, damage: IntArray, health: IntArray): Long {
var res: Long = 0
var sum: Long = 0
for (e in damage) {
sum += e.toLong()
}
val pairs = arrayOfNulls<Pair>(damage.size)
for (e in damage.indices) {
pairs[e] = Pair(damage[e], (health[e] + pw - 1) / pw)
}
pairs.sort()
for (pr in pairs) {
res += pr!!.`val` * sum
sum -= pr.key.toLong()
}
return res
}

internal class Pair(var key: Int, var `val`: Int) : Comparable<Pair> {
override fun compareTo(p: Pair): Int {
return `val` * p.key - key * p.`val`
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
3273\. Minimum Amount of Damage Dealt to Bob

Hard

You are given an integer `power` and two integer arrays `damage` and `health`, both having length `n`.

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`).

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.

Determine the **minimum** total amount of damage points that will be dealt to Bob before **all** `n` enemies are _dead_.

**Example 1:**

**Input:** power = 4, damage = [1,2,3,4], health = [4,5,6,8]

**Output:** 39

**Explanation:**

* 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.
* 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.
* 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.
* 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.

**Example 2:**

**Input:** power = 1, damage = [1,1,1,1], health = [1,2,3,4]

**Output:** 20

**Explanation:**

* 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.
* 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.
* 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.
* 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.

**Example 3:**

**Input:** power = 8, damage = [40], health = [59]

**Output:** 320

**Constraints:**

* <code>1 <= power <= 10<sup>4</sup></code>
* <code>1 <= n == damage.length == health.length <= 10<sup>5</sup></code>
* <code>1 <= damage[i], health[i] <= 10<sup>4</sup></code>
Loading