Skip to content

Commit 9bfdaed

Browse files
committed
Added tasks 3461-3464
1 parent c2c8c01 commit 9bfdaed

File tree

5 files changed

+421
-1
lines changed
  • src/main/kotlin/g3401_3500
    • s3461_check_if_digits_are_equal_in_string_after_operations_i
    • s3462_maximum_sum_with_at_most_k_elements
    • s3463_check_if_digits_are_equal_in_string_after_operations_ii
    • s3464_maximize_the_distance_between_points_on_a_square

5 files changed

+421
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,10 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3464 |[Maximize the Distance Between Points on a Square](src/main/kotlin/g3401_3500/s3464_maximize_the_distance_between_points_on_a_square)| Hard | Array, Greedy, Binary_Search | 18 | 98.51
2092+
| 3463 |[Check If Digits Are Equal in String After Operations II](src/main/kotlin/g3401_3500/s3463_check_if_digits_are_equal_in_string_after_operations_ii)| Hard | String, Math, Number_Theory, Combinatorics | 38 | 100.00
2093+
| 3462 |[Maximum Sum With at Most K Elements](src/main/kotlin/g3401_3500/s3462_maximum_sum_with_at_most_k_elements)| Medium | Array, Sorting, Greedy, Matrix, Heap_(Priority_Queue) | 139 | 100.00
2094+
| 3461 |[Check If Digits Are Equal in String After Operations I](src/main/kotlin/g3401_3500/s3461_check_if_digits_are_equal_in_string_after_operations_i)| Easy | String, Math, Simulation, Number_Theory, Combinatorics | 3 | 100.00
20912095
| 3459 |[Length of Longest V-Shaped Diagonal Segment](src/main/kotlin/g3401_3500/s3459_length_of_longest_v_shaped_diagonal_segment)| Hard | Array, Dynamic_Programming, Matrix, Memoization | 287 | 100.00
20922096
| 3458 |[Select K Disjoint Special Substrings](src/main/kotlin/g3401_3500/s3458_select_k_disjoint_special_substrings)| Medium | String, Hash_Table, Dynamic_Programming, Sorting, Greedy | 24 | 100.00
20932097
| 3457 |[Eat Pizzas!](src/main/kotlin/g3401_3500/s3457_eat_pizzas)| Medium | Array, Sorting, Greedy | 101 | 90.91
@@ -2109,7 +2113,7 @@
21092113
| 3440 |[Reschedule Meetings for Maximum Free Time II](src/main/kotlin/g3401_3500/s3440_reschedule_meetings_for_maximum_free_time_ii)| Medium | Array, Greedy, Enumeration | 8 | 100.00
21102114
| 3439 |[Reschedule Meetings for Maximum Free Time I](src/main/kotlin/g3401_3500/s3439_reschedule_meetings_for_maximum_free_time_i)| Medium | Array, Greedy, Sliding_Window | 5 | 80.00
21112115
| 3438 |[Find Valid Pair of Adjacent Digits in String](src/main/kotlin/g3401_3500/s3438_find_valid_pair_of_adjacent_digits_in_string)| Easy | String, Hash_Table, Counting | 2 | 93.18
2112-
| 3436 |[Find Valid Emails](src/main/kotlin/g3401_3500/s3436_find_valid_emails)| Easy || 451 | 70.84
2116+
| 3436 |[Find Valid Emails](src/main/kotlin/g3401_3500/s3436_find_valid_emails)| Easy | Database | 451 | 70.84
21132117
| 3435 |[Frequencies of Shortest Supersequences](src/main/kotlin/g3401_3500/s3435_frequencies_of_shortest_supersequences)| Hard | Array, String, Bit_Manipulation, Graph, Enumeration, Topological_Sort | 35 | 100.00
21142118
| 3434 |[Maximum Frequency After Subarray Operation](src/main/kotlin/g3401_3500/s3434_maximum_frequency_after_subarray_operation)| Medium | Array, Hash_Table, Dynamic_Programming, Greedy, Prefix_Sum | 51 | 100.00
21152119
| 3433 |[Count Mentions Per User](src/main/kotlin/g3401_3500/s3433_count_mentions_per_user)| Medium | Array, Math, Sorting, Simulation | 52 | 100.00
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3461\. Check If Digits Are Equal in String After Operations I
5+
6+
Easy
7+
8+
You are given a string `s` consisting of digits. Perform the following operation repeatedly until the string has **exactly** two digits:
9+
10+
* For each pair of consecutive digits in `s`, starting from the first digit, calculate a new digit as the sum of the two digits **modulo** 10.
11+
* Replace `s` with the sequence of newly calculated digits, _maintaining the order_ in which they are computed.
12+
13+
Return `true` if the final two digits in `s` are the **same**; otherwise, return `false`.
14+
15+
**Example 1:**
16+
17+
**Input:** s = "3902"
18+
19+
**Output:** true
20+
21+
**Explanation:**
22+
23+
* Initially, `s = "3902"`
24+
* First operation:
25+
* `(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2`
26+
* `(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9`
27+
* `(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2`
28+
* `s` becomes `"292"`
29+
* Second operation:
30+
* `(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1`
31+
* `(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1`
32+
* `s` becomes `"11"`
33+
* Since the digits in `"11"` are the same, the output is `true`.
34+
35+
**Example 2:**
36+
37+
**Input:** s = "34789"
38+
39+
**Output:** false
40+
41+
**Explanation:**
42+
43+
* Initially, `s = "34789"`.
44+
* After the first operation, `s = "7157"`.
45+
* After the second operation, `s = "862"`.
46+
* After the third operation, `s = "48"`.
47+
* Since `'4' != '8'`, the output is `false`.
48+
49+
**Constraints:**
50+
51+
* `3 <= s.length <= 100`
52+
* `s` consists of only digits.
53+
54+
## Solution
55+
56+
```kotlin
57+
class Solution {
58+
fun hasSameDigits(s: String): Boolean {
59+
val ch = s.toCharArray()
60+
var k = ch.size - 1
61+
while (k != 1) {
62+
for (i in 0..<k) {
63+
val a = ch[i].code - 48
64+
val b = ch[i + 1].code - 48
65+
val d = (a + b) % 10
66+
val c = (d + '0'.code).toChar()
67+
ch[i] = c
68+
}
69+
k--
70+
}
71+
return ch[0] == ch[1]
72+
}
73+
}
74+
```
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3462\. Maximum Sum With at Most K Elements
5+
6+
Medium
7+
8+
You are given a 2D integer matrix `grid` of size `n x m`, an integer array `limits` of length `n`, and an integer `k`. The task is to find the **maximum sum** of **at most** `k` elements from the matrix `grid` such that:
9+
10+
* The number of elements taken from the <code>i<sup>th</sup></code> row of `grid` does not exceed `limits[i]`.
11+
12+
13+
Return the **maximum sum**.
14+
15+
**Example 1:**
16+
17+
**Input:** grid = \[\[1,2],[3,4]], limits = [1,2], k = 2
18+
19+
**Output:** 7
20+
21+
**Explanation:**
22+
23+
* From the second row, we can take at most 2 elements. The elements taken are 4 and 3.
24+
* The maximum possible sum of at most 2 selected elements is `4 + 3 = 7`.
25+
26+
**Example 2:**
27+
28+
**Input:** grid = \[\[5,3,7],[8,2,6]], limits = [2,2], k = 3
29+
30+
**Output:** 21
31+
32+
**Explanation:**
33+
34+
* From the first row, we can take at most 2 elements. The element taken is 7.
35+
* From the second row, we can take at most 2 elements. The elements taken are 8 and 6.
36+
* The maximum possible sum of at most 3 selected elements is `7 + 8 + 6 = 21`.
37+
38+
**Constraints:**
39+
40+
* `n == grid.length == limits.length`
41+
* `m == grid[i].length`
42+
* `1 <= n, m <= 500`
43+
* <code>0 <= grid[i][j] <= 10<sup>5</sup></code>
44+
* `0 <= limits[i] <= m`
45+
* `0 <= k <= min(n * m, sum(limits))`
46+
47+
## Solution
48+
49+
```kotlin
50+
class Solution {
51+
fun maxSum(grid: Array<IntArray>, limits: IntArray, k: Int): Long {
52+
var l = 0
53+
for (i in limits.indices) {
54+
l += limits[i]
55+
}
56+
val dp = IntArray(l)
57+
var a = 0
58+
for (i in grid.indices) {
59+
val lim = limits[i]
60+
grid[i].sort()
61+
for (j in grid[i].size - lim..<grid[i].size) {
62+
dp[a] = grid[i][j]
63+
a++
64+
}
65+
}
66+
dp.sort()
67+
var sum = 0L
68+
for (i in l - 1 downTo l - k) {
69+
sum += dp[i].toLong()
70+
}
71+
return sum
72+
}
73+
}
74+
```
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3463\. Check If Digits Are Equal in String After Operations II
5+
6+
Hard
7+
8+
You are given a string `s` consisting of digits. Perform the following operation repeatedly until the string has **exactly** two digits:
9+
10+
* For each pair of consecutive digits in `s`, starting from the first digit, calculate a new digit as the sum of the two digits **modulo** 10.
11+
* Replace `s` with the sequence of newly calculated digits, _maintaining the order_ in which they are computed.
12+
13+
Return `true` if the final two digits in `s` are the **same**; otherwise, return `false`.
14+
15+
**Example 1:**
16+
17+
**Input:** s = "3902"
18+
19+
**Output:** true
20+
21+
**Explanation:**
22+
23+
* Initially, `s = "3902"`
24+
* First operation:
25+
* `(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2`
26+
* `(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9`
27+
* `(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2`
28+
* `s` becomes `"292"`
29+
* Second operation:
30+
* `(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1`
31+
* `(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1`
32+
* `s` becomes `"11"`
33+
* Since the digits in `"11"` are the same, the output is `true`.
34+
35+
**Example 2:**
36+
37+
**Input:** s = "34789"
38+
39+
**Output:** false
40+
41+
**Explanation:**
42+
43+
* Initially, `s = "34789"`.
44+
* After the first operation, `s = "7157"`.
45+
* After the second operation, `s = "862"`.
46+
* After the third operation, `s = "48"`.
47+
* Since `'4' != '8'`, the output is `false`.
48+
49+
**Constraints:**
50+
51+
* <code>3 <= s.length <= 10<sup>5</sup></code>
52+
* `s` consists of only digits.
53+
54+
## Solution
55+
56+
```kotlin
57+
class Solution {
58+
private fun powMod10(a: Int, n: Int): Int {
59+
var a = a
60+
var n = n
61+
var x = 1
62+
while (n >= 1) {
63+
if (n % 2 == 1) {
64+
x = (x * a) % 10
65+
}
66+
a = (a * a) % 10
67+
n /= 2
68+
}
69+
return x
70+
}
71+
72+
private fun f(n: Int): IntArray {
73+
val ns = IntArray(n + 1)
74+
val n2 = IntArray(n + 1)
75+
val n5 = IntArray(n + 1)
76+
ns[0] = 1
77+
for (i in 1..n) {
78+
var m = i
79+
n2[i] = n2[i - 1]
80+
n5[i] = n5[i - 1]
81+
while (m % 2 == 0) {
82+
m /= 2
83+
n2[i]++
84+
}
85+
while (m % 5 == 0) {
86+
m /= 5
87+
n5[i]++
88+
}
89+
ns[i] = (ns[i - 1] * m) % 10
90+
}
91+
val inv = IntArray(10)
92+
for (i in 1..9) {
93+
for (j in 0..9) {
94+
if (i * j % 10 == 1) {
95+
inv[i] = j
96+
}
97+
}
98+
}
99+
val xs = IntArray(n + 1)
100+
for (k in 0..n) {
101+
var a = 0
102+
val s2 = n2[n] - n2[n - k] - n2[k]
103+
val s5 = n5[n] - n5[n - k] - n5[k]
104+
if (s2 == 0 || s5 == 0) {
105+
a = (ns[n] * inv[ns[n - k]] * inv[ns[k]] * powMod10(2, s2) * powMod10(5, s5)) % 10
106+
}
107+
xs[k] = a
108+
}
109+
return xs
110+
}
111+
112+
fun hasSameDigits(s: String): Boolean {
113+
val n = s.length
114+
val xs = f(n - 2)
115+
val arr = IntArray(n)
116+
for (i in 0..<n) {
117+
arr[i] = s[i].code - '0'.code
118+
}
119+
var num1 = 0
120+
var num2 = 0
121+
for (i in 0..<n - 1) {
122+
num1 = (num1 + xs[i] * arr[i]) % 10
123+
num2 = (num2 + xs[i] * arr[i + 1]) % 10
124+
}
125+
return num1 == num2
126+
}
127+
}
128+
```

0 commit comments

Comments
 (0)