Skip to content

Commit 1026745

Browse files
committed
Added tasks 3556-3563
1 parent 793ac0a commit 1026745

File tree

9 files changed

+894
-0
lines changed
  • src/main/kotlin/g3501_3600
    • s3556_sum_of_largest_prime_substrings
    • s3557_find_maximum_number_of_non_intersecting_substrings
    • s3558_number_of_ways_to_assign_edge_weights_i
    • s3559_number_of_ways_to_assign_edge_weights_ii
    • s3560_find_minimum_log_transportation_cost
    • s3561_resulting_string_after_adjacent_removals
    • s3562_maximum_profit_from_trading_stocks_with_discounts
    • s3563_lexicographically_smallest_string_after_adjacent_removals

9 files changed

+894
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,14 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3563 |[Lexicographically Smallest String After Adjacent Removals](src/main/kotlin/g3501_3600/s3563_lexicographically_smallest_string_after_adjacent_removals)| Hard | String, Dynamic_Programming | 186 | 100.00
2092+
| 3562 |[Maximum Profit from Trading Stocks with Discounts](src/main/kotlin/g3501_3600/s3562_maximum_profit_from_trading_stocks_with_discounts)| Hard | Array, Dynamic_Programming, Depth_First_Search, Tree | 40 | 100.00
2093+
| 3561 |[Resulting String After Adjacent Removals](src/main/kotlin/g3501_3600/s3561_resulting_string_after_adjacent_removals)| Medium | String, Stack, Simulation | 43 | 100.00
2094+
| 3560 |[Find Minimum Log Transportation Cost](src/main/kotlin/g3501_3600/s3560_find_minimum_log_transportation_cost)| Easy | Math | 0 | 100.00
2095+
| 3559 |[Number of Ways to Assign Edge Weights II](src/main/kotlin/g3501_3600/s3559_number_of_ways_to_assign_edge_weights_ii)| Hard | Array, Dynamic_Programming, Math, Depth_First_Search, Tree | 197 | 100.00
2096+
| 3558 |[Number of Ways to Assign Edge Weights I](src/main/kotlin/g3501_3600/s3558_number_of_ways_to_assign_edge_weights_i)| Medium | Math, Depth_First_Search, Tree | 21 | 100.00
2097+
| 3557 |[Find Maximum Number of Non Intersecting Substrings](src/main/kotlin/g3501_3600/s3557_find_maximum_number_of_non_intersecting_substrings)| Medium | String, Hash_Table, Dynamic_Programming, Greedy | 28 | 70.59
2098+
| 3556 |[Sum of Largest Prime Substrings](src/main/kotlin/g3501_3600/s3556_sum_of_largest_prime_substrings)| Medium | String, Hash_Table, Math, Sorting, Number_Theory | 25 | 100.00
20912099
| 3554 |[Find Category Recommendation Pairs](src/main/kotlin/g3501_3600/s3554_find_category_recommendation_pairs)| Hard | Database | 623 | 82.76
20922100
| 3553 |[Minimum Weighted Subgraph With the Required Paths II](src/main/kotlin/g3501_3600/s3553_minimum_weighted_subgraph_with_the_required_paths_ii)| Hard | Array, Depth_First_Search, Tree | 142 | 100.00
20932101
| 3552 |[Grid Teleportation Traversal](src/main/kotlin/g3501_3600/s3552_grid_teleportation_traversal)| Medium | Array, Hash_Table, Breadth_First_Search, Matrix | 147 | 100.00
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
## 3556\. Sum of Largest Prime Substrings
5+
6+
Medium
7+
8+
Given a string `s`, find the sum of the **3 largest unique prime numbers** that can be formed using any of its ****substring****.
9+
10+
Return the **sum** of the three largest unique prime numbers that can be formed. If fewer than three exist, return the sum of **all** available primes. If no prime numbers can be formed, return 0.
11+
12+
**Note:** Each prime number should be counted only **once**, even if it appears in **multiple** substrings. Additionally, when converting a substring to an integer, any leading zeros are ignored.
13+
14+
**Example 1:**
15+
16+
**Input:** s = "12234"
17+
18+
**Output:** 1469
19+
20+
**Explanation:**
21+
22+
* The unique prime numbers formed from the substrings of `"12234"` are 2, 3, 23, 223, and 1223.
23+
* The 3 largest primes are 1223, 223, and 23. Their sum is 1469.
24+
25+
**Example 2:**
26+
27+
**Input:** s = "111"
28+
29+
**Output:** 11
30+
31+
**Explanation:**
32+
33+
* The unique prime number formed from the substrings of `"111"` is 11.
34+
* Since there is only one prime number, the sum is 11.
35+
36+
**Constraints:**
37+
38+
* `1 <= s.length <= 10`
39+
* `s` consists of only digits.
40+
41+
## Solution
42+
43+
```kotlin
44+
class Solution {
45+
fun sumOfLargestPrimes(s: String): Long {
46+
val set: MutableSet<Long> = HashSet()
47+
val n = s.length
48+
var first: Long = -1
49+
var second: Long = -1
50+
var third: Long = -1
51+
for (i in 0..<n) {
52+
var num: Long = 0
53+
for (j in i..<n) {
54+
num = num * 10 + (s[j].code - '0'.code)
55+
if (i != j && s[i] == '0') {
56+
break
57+
}
58+
if (isPrime(num) && !set.contains(num)) {
59+
set.add(num)
60+
if (num > first) {
61+
third = second
62+
second = first
63+
first = num
64+
} else if (num > second) {
65+
third = second
66+
second = num
67+
} else if (num > third) {
68+
third = num
69+
}
70+
}
71+
}
72+
}
73+
var sum: Long = 0
74+
if (first != -1L) {
75+
sum += first
76+
}
77+
if (second != -1L) {
78+
sum += second
79+
}
80+
if (third != -1L) {
81+
sum += third
82+
}
83+
return sum
84+
}
85+
86+
fun isPrime(num: Long): Boolean {
87+
if (num <= 1) {
88+
return false
89+
}
90+
if (num == 2L || num == 3L) {
91+
return true
92+
}
93+
if (num % 2 == 0L || num % 3 == 0L) {
94+
return false
95+
}
96+
var i: Long = 5
97+
while (i * i <= num) {
98+
if (num % i == 0L || num % (i + 2) == 0L) {
99+
return false
100+
}
101+
i += 6
102+
}
103+
return true
104+
}
105+
}
106+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
## 3557\. Find Maximum Number of Non Intersecting Substrings
5+
6+
Medium
7+
8+
You are given a string `word`.
9+
10+
Return the **maximum** number of non-intersecting ****substring**** of word that are at **least** four characters long and start and end with the same letter.
11+
12+
**Example 1:**
13+
14+
**Input:** word = "abcdeafdef"
15+
16+
**Output:** 2
17+
18+
**Explanation:**
19+
20+
The two substrings are `"abcdea"` and `"fdef"`.
21+
22+
**Example 2:**
23+
24+
**Input:** word = "bcdaaaab"
25+
26+
**Output:** 1
27+
28+
**Explanation:**
29+
30+
The only substring is `"aaaa"`. Note that we cannot **also** choose `"bcdaaaab"` since it intersects with the other substring.
31+
32+
**Constraints:**
33+
34+
* <code>1 <= word.length <= 2 * 10<sup>5</sup></code>
35+
* `word` consists only of lowercase English letters.
36+
37+
## Solution
38+
39+
```kotlin
40+
class Solution {
41+
fun maxSubstrings(s: String): Int {
42+
val prev = IntArray(26)
43+
var r = 0
44+
prev.fill(-1)
45+
for (i in 0..<s.length) {
46+
val j = s[i].code - 'a'.code
47+
if (prev[j] != -1 && i - prev[j] + 1 >= 4) {
48+
++r
49+
prev.fill(-1)
50+
} else if (prev[j] == -1) {
51+
prev[j] = i
52+
}
53+
}
54+
return r
55+
}
56+
}
57+
```
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
## 3558\. Number of Ways to Assign Edge Weights I
5+
6+
Medium
7+
8+
There is an undirected tree with `n` nodes labeled from 1 to `n`, rooted at node 1. The tree is represented by a 2D integer array `edges` of length `n - 1`, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>.
9+
10+
Initially, all edges have a weight of 0. You must assign each edge a weight of either **1** or **2**.
11+
12+
The **cost** of a path between any two nodes `u` and `v` is the total weight of all edges in the path connecting them.
13+
14+
Select any one node `x` at the **maximum** depth. Return the number of ways to assign edge weights in the path from node 1 to `x` such that its total cost is **odd**.
15+
16+
Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
17+
18+
**Note:** Ignore all edges **not** in the path from node 1 to `x`.
19+
20+
**Example 1:**
21+
22+
![](https://assets.leetcode.com/uploads/2025/03/23/screenshot-2025-03-24-at-060006.png)
23+
24+
**Input:** edges = \[\[1,2]]
25+
26+
**Output:** 1
27+
28+
**Explanation:**
29+
30+
* The path from Node 1 to Node 2 consists of one edge (`1 → 2`).
31+
* Assigning weight 1 makes the cost odd, while 2 makes it even. Thus, the number of valid assignments is 1.
32+
33+
**Example 2:**
34+
35+
![](https://assets.leetcode.com/uploads/2025/03/23/screenshot-2025-03-24-at-055820.png)
36+
37+
**Input:** edges = \[\[1,2],[1,3],[3,4],[3,5]]
38+
39+
**Output:** 2
40+
41+
**Explanation:**
42+
43+
* The maximum depth is 2, with nodes 4 and 5 at the same depth. Either node can be selected for processing.
44+
* For example, the path from Node 1 to Node 4 consists of two edges (`1 → 3` and `3 → 4`).
45+
* Assigning weights (1,2) or (2,1) results in an odd cost. Thus, the number of valid assignments is 2.
46+
47+
**Constraints:**
48+
49+
* <code>2 <= n <= 10<sup>5</sup></code>
50+
* `edges.length == n - 1`
51+
* <code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>]</code>
52+
* <code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code>
53+
* `edges` represents a valid tree.
54+
55+
## Solution
56+
57+
```kotlin
58+
class Solution {
59+
fun assignEdgeWeights(edges: Array<IntArray>): Int {
60+
if (pow2[0] == 0L) {
61+
pow2[0] = 1
62+
for (i in 1..<pow2.size) {
63+
pow2[i] = (pow2[i - 1] shl 1) % mod
64+
}
65+
}
66+
val n = edges.size + 1
67+
val adj = IntArray(n + 1)
68+
val degrees = IntArray(n + 1)
69+
for (edge in edges) {
70+
val u = edge[0]
71+
val v = edge[1]
72+
adj[u] += v
73+
adj[v] += u
74+
degrees[u]++
75+
degrees[v]++
76+
}
77+
val que = IntArray(n)
78+
var write = 0
79+
var read = 0
80+
for (i in 2..n) {
81+
if (degrees[i] == 1) {
82+
que[write++] = i
83+
}
84+
}
85+
var distance = 0
86+
while (read < write) {
87+
distance++
88+
var size = write - read
89+
while (size-- > 0) {
90+
val v = que[read++]
91+
val u = adj[v]
92+
adj[u] -= v
93+
if (--degrees[u] == 1 && u != 1) {
94+
que[write++] = u
95+
}
96+
}
97+
}
98+
return pow2[distance - 1].toInt()
99+
}
100+
101+
companion object {
102+
private const val mod = 1e9.toInt() + 7
103+
private val pow2 = LongArray(100001)
104+
}
105+
}
106+
```

0 commit comments

Comments
 (0)