Skip to content

Commit 0c6edf6

Browse files
committed
Added tasks 3270-3277
1 parent f2930eb commit 0c6edf6

File tree

24 files changed

+871
-0
lines changed

24 files changed

+871
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3201_3300.s3270_find_the_key_of_the_numbers;
2+
3+
// #Easy #2024_09_02_Time_0_ms_(100.00%)_Space_40.5_MB_(100.00%)
4+
5+
public class Solution {
6+
public int generateKey(int num1, int num2, int num3) {
7+
int s1 =
8+
Math.min(((num1 / 1000) % 10), Math.min(((num2 / 1000) % 10), ((num3 / 1000) % 10)))
9+
* 1000;
10+
int s2 =
11+
Math.min(((num1 / 100) % 10), Math.min(((num2 / 100) % 10), ((num3 / 100) % 10)))
12+
* 100;
13+
int s3 =
14+
Math.min(((num1 / 10) % 10), Math.min(((num2 / 10) % 10), ((num3 / 10) % 10))) * 10;
15+
int s4 = Math.min((num1 % 10), Math.min((num2 % 10), (num3 % 10)));
16+
return s1 + s2 + s3 + s4;
17+
}
18+
}
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g3201_3300.s3271_hash_divided_string;
2+
3+
// #Medium #2024_09_02_Time_2_ms_(100.00%)_Space_44.7_MB_(100.00%)
4+
5+
public class Solution {
6+
public String stringHash(String s, int k) {
7+
var result = new StringBuilder();
8+
for (int i = 0, sum = 0; i < s.length(); i++) {
9+
sum += s.charAt(i) - 'a';
10+
if ((i + 1) % k == 0) {
11+
result.append((char) ('a' + sum % 26));
12+
sum = 0;
13+
}
14+
}
15+
return result.toString();
16+
}
17+
}
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: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package g3201_3300.s3272_find_the_count_of_good_integers;
2+
3+
// #Hard #2024_09_02_Time_167_ms_(100.00%)_Space_54.5_MB_(100.00%)
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.HashMap;
8+
import java.util.HashSet;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.Set;
12+
13+
public class Solution {
14+
private List<String> palindromes = new ArrayList<>();
15+
16+
private long factorial(int n) {
17+
long res = 1;
18+
for (int i = 2; i <= n; i++) {
19+
res *= i;
20+
}
21+
return res;
22+
}
23+
24+
private Map<Character, Integer> countDigits(String s) {
25+
Map<Character, Integer> freq = new HashMap<>();
26+
for (char c : s.toCharArray()) {
27+
freq.put(c, freq.getOrDefault(c, 0) + 1);
28+
}
29+
return freq;
30+
}
31+
32+
private long calculatePermutations(Map<Character, Integer> freq, int length) {
33+
long totalPermutations = factorial(length);
34+
for (int count : freq.values()) {
35+
totalPermutations /= factorial(count);
36+
}
37+
return totalPermutations;
38+
}
39+
40+
private long calculateValidPermutations(String s) {
41+
Map<Character, Integer> freq = countDigits(s);
42+
int n = s.length();
43+
long totalPermutations = calculatePermutations(freq, n);
44+
if (freq.getOrDefault('0', 0) > 0) {
45+
freq.put('0', freq.get('0') - 1);
46+
long invalidPermutations = calculatePermutations(freq, n - 1);
47+
totalPermutations -= invalidPermutations;
48+
}
49+
50+
return totalPermutations;
51+
}
52+
53+
private void generatePalindromes(
54+
int f, int r, int k, int lb, int sum, StringBuilder ans, int[] rem) {
55+
if (f > r) {
56+
if (sum == 0) {
57+
palindromes.add(ans.toString());
58+
}
59+
return;
60+
}
61+
for (int i = lb; i <= 9; i++) {
62+
ans.setCharAt(f, (char) ('0' + i));
63+
ans.setCharAt(r, (char) ('0' + i));
64+
int chk = sum;
65+
chk = (chk + rem[f] * i) % k;
66+
if (f != r) {
67+
chk = (chk + rem[r] * i) % k;
68+
}
69+
generatePalindromes(f + 1, r - 1, k, 0, chk, ans, rem);
70+
}
71+
}
72+
73+
private List<String> allKPalindromes(int n, int k) {
74+
StringBuilder ans = new StringBuilder(n);
75+
for (int i = 0; i < n; i++) {
76+
ans.append('0');
77+
}
78+
int[] rem = new int[n];
79+
rem[0] = 1;
80+
for (int i = 1; i < n; i++) {
81+
rem[i] = (rem[i - 1] * 10) % k;
82+
}
83+
84+
palindromes.clear();
85+
generatePalindromes(0, n - 1, k, 1, 0, ans, rem);
86+
87+
return palindromes;
88+
}
89+
90+
public long countGoodIntegers(int n, int k) {
91+
List<String> ans = allKPalindromes(n, k);
92+
Set<String> st = new HashSet<>();
93+
for (String str : ans) {
94+
char[] arr = str.toCharArray();
95+
Arrays.sort(arr);
96+
st.add(new String(arr));
97+
}
98+
99+
List<String> v = new ArrayList<>(st);
100+
long chk = 0;
101+
for (String str : v) {
102+
long cc = calculateValidPermutations(str);
103+
chk += cc;
104+
}
105+
return chk;
106+
}
107+
}
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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package g3201_3300.s3273_minimum_amount_of_damage_dealt_to_bob;
2+
3+
// #Hard #2024_09_02_Time_221_ms_(100.00%)_Space_59.9_MB_(100.00%)
4+
5+
import java.util.ArrayList;
6+
import java.util.Collections;
7+
import java.util.List;
8+
9+
public class Solution {
10+
int total;
11+
int power;
12+
13+
private class Pair implements Comparable<Pair> {
14+
int health;
15+
int damage;
16+
17+
Pair(int health, int damage) {
18+
this.health = health;
19+
this.damage = damage;
20+
}
21+
22+
@Override
23+
public int compareTo(Pair p) {
24+
25+
int thisHits = (int) Math.ceil((double) this.health / power);
26+
int otherHits = (int) Math.ceil((double) p.health / power);
27+
28+
double thisTotalDamage = this.damage * 1.0 / thisHits;
29+
double otherTotalDamage = p.damage * 1.0 / otherHits;
30+
31+
return Double.compare(otherTotalDamage, thisTotalDamage);
32+
}
33+
}
34+
35+
public long minDamage(int power, int[] damage, int[] health) {
36+
this.power = power;
37+
total = 0;
38+
List<Pair> pairs = new ArrayList<>();
39+
for (int i = 0; i < damage.length; i++) {
40+
pairs.add(new Pair(health[i], damage[i]));
41+
}
42+
Collections.sort(pairs);
43+
long totalDamage = 0;
44+
int hitsRequired = 0;
45+
for (Pair pair : pairs) {
46+
hitsRequired += (int) Math.ceil((double) pair.health / power);
47+
totalDamage += (long) hitsRequired * pair.damage;
48+
}
49+
return totalDamage;
50+
}
51+
}
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)