Skip to content

Commit f1ce47a

Browse files
authored
Added tasks 2064-2076
1 parent 73eda43 commit f1ce47a

File tree

30 files changed

+1379
-0
lines changed

30 files changed

+1379
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g2001_2100.s2064_minimized_maximum_of_products_distributed_to_any_store
2+
3+
// #Medium #Array #Binary_Search #2023_06_26_Time_609_ms_(100.00%)_Space_63.6_MB_(100.00%)
4+
5+
class Solution {
6+
fun minimizedMaximum(n: Int, q: IntArray): Int {
7+
var min = 1
8+
var max = maxi(q)
9+
var ans = 0
10+
while (min <= max) {
11+
val mid = min + (max - min) / 2
12+
if (condition(q, mid, n)) {
13+
ans = mid
14+
max = mid - 1
15+
} else {
16+
min = mid + 1
17+
}
18+
}
19+
return ans
20+
}
21+
22+
private fun condition(arr: IntArray, mid: Int, n: Int): Boolean {
23+
var ans = 0
24+
for (num in arr) {
25+
ans += num / mid
26+
if (num % mid != 0) {
27+
ans++
28+
}
29+
}
30+
return ans <= n
31+
}
32+
33+
private fun maxi(arr: IntArray): Int {
34+
var ans = 0
35+
for (n in arr) {
36+
ans = ans.coerceAtLeast(n)
37+
}
38+
return ans
39+
}
40+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2064\. Minimized Maximum of Products Distributed to Any Store
2+
3+
Medium
4+
5+
You are given an integer `n` indicating there are `n` specialty retail stores. There are `m` product types of varying amounts, which are given as a **0-indexed** integer array `quantities`, where `quantities[i]` represents the number of products of the <code>i<sup>th</sup></code> product type.
6+
7+
You need to distribute **all products** to the retail stores following these rules:
8+
9+
* A store can only be given **at most one product type** but can be given **any** amount of it.
10+
* After distribution, each store will have been given some number of products (possibly `0`). Let `x` represent the maximum number of products given to any store. You want `x` to be as small as possible, i.e., you want to **minimize** the **maximum** number of products that are given to any store.
11+
12+
Return _the minimum possible_ `x`.
13+
14+
**Example 1:**
15+
16+
**Input:** n = 6, quantities = [11,6]
17+
18+
**Output:** 3
19+
20+
**Explanation:** One optimal way is:
21+
22+
- The 11 products of type 0 are distributed to the first four stores in these amounts: 2, 3, 3, 3
23+
24+
- The 6 products of type 1 are distributed to the other two stores in these amounts: 3, 3
25+
26+
The maximum number of products given to any store is max(2, 3, 3, 3, 3, 3) = 3.
27+
28+
**Example 2:**
29+
30+
**Input:** n = 7, quantities = [15,10,10]
31+
32+
**Output:** 5
33+
34+
**Explanation:** One optimal way is:
35+
36+
- The 15 products of type 0 are distributed to the first three stores in these amounts: 5, 5, 5
37+
38+
- The 10 products of type 1 are distributed to the next two stores in these amounts: 5, 5
39+
40+
- The 10 products of type 2 are distributed to the last two stores in these amounts: 5, 5
41+
42+
The maximum number of products given to any store is max(5, 5, 5, 5, 5, 5, 5) = 5.
43+
44+
**Example 3:**
45+
46+
**Input:** n = 1, quantities = [100000]
47+
48+
**Output:** 100000
49+
50+
**Explanation:** The only optimal way is:
51+
52+
- The 100000 products of type 0 are distributed to the only store.
53+
54+
The maximum number of products given to any store is max(100000) = 100000.
55+
56+
**Constraints:**
57+
58+
* `m == quantities.length`
59+
* <code>1 <= m <= n <= 10<sup>5</sup></code>
60+
* <code>1 <= quantities[i] <= 10<sup>5</sup></code>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package g2001_2100.s2065_maximum_path_quality_of_a_graph
2+
3+
// #Hard #Array #Graph #Backtracking #2023_06_26_Time_429_ms_(100.00%)_Space_46.7_MB_(100.00%)
4+
5+
class Solution {
6+
private var maxQuality = 0
7+
8+
internal class Node(var i: Int, var time: Int)
9+
10+
fun maximalPathQuality(values: IntArray, edges: Array<IntArray>, maxTime: Int): Int {
11+
val graph: MutableList<MutableList<Node>> = ArrayList()
12+
for (i in values.indices) {
13+
graph.add(ArrayList())
14+
}
15+
for (edge in edges) {
16+
val u = edge[0]
17+
val v = edge[1]
18+
val time = edge[2]
19+
val node1 = Node(u, time)
20+
val node2 = Node(v, time)
21+
graph[u].add(node2)
22+
graph[v].add(node1)
23+
}
24+
maxQuality = 0
25+
dfs(graph, 0, 0, maxTime, values[0], values)
26+
return maxQuality
27+
}
28+
29+
private fun dfs(
30+
graph: List<MutableList<Node>>,
31+
start: Int,
32+
curTime: Int,
33+
maxTime: Int,
34+
curValue: Int,
35+
values: IntArray
36+
) {
37+
if (curTime > maxTime) {
38+
return
39+
}
40+
if (curTime == maxTime && start != 0) {
41+
return
42+
}
43+
if (start == 0) {
44+
maxQuality = maxQuality.coerceAtLeast(curValue)
45+
}
46+
val tmp = values[start]
47+
if (tmp != 0) {
48+
values[start] = 0
49+
}
50+
for (node in graph[start]) {
51+
val v = node.i
52+
val time = node.time
53+
val value = values[v]
54+
if (value != 0) {
55+
values[v] = 0
56+
}
57+
dfs(graph, v, curTime + time, maxTime, curValue + value, values)
58+
if (value != 0) {
59+
values[v] = value
60+
}
61+
}
62+
if (tmp != 0) {
63+
values[start] = tmp
64+
}
65+
}
66+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2065\. Maximum Path Quality of a Graph
2+
3+
Hard
4+
5+
There is an **undirected** graph with `n` nodes numbered from `0` to `n - 1` (**inclusive**). You are given a **0-indexed** integer array `values` where `values[i]` is the **value** of the <code>i<sup>th</sup></code> node. You are also given a **0-indexed** 2D integer array `edges`, where each <code>edges[j] = [u<sub>j</sub>, v<sub>j</sub>, time<sub>j</sub>]</code> indicates that there is an undirected edge between the nodes <code>u<sub>j</sub></code> and <code>v<sub>j</sub></code>, and it takes <code>time<sub>j</sub></code> seconds to travel between the two nodes. Finally, you are given an integer `maxTime`.
6+
7+
A **valid** **path** in the graph is any path that starts at node `0`, ends at node `0`, and takes **at most** `maxTime` seconds to complete. You may visit the same node multiple times. The **quality** of a valid path is the **sum** of the values of the **unique nodes** visited in the path (each node's value is added **at most once** to the sum).
8+
9+
Return _the **maximum** quality of a valid path_.
10+
11+
**Note:** There are **at most four** edges connected to each node.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2021/10/19/ex1drawio.png)
16+
17+
**Input:** values = [0,32,10,43], edges = [[0,1,10],[1,2,15],[0,3,10]], maxTime = 49
18+
19+
**Output:** 75
20+
21+
**Explanation:**
22+
23+
One possible path is 0 -> 1 -> 0 -> 3 -> 0.
24+
25+
The total time taken is 10 + 10 + 10 + 10 = 40 <= 49.
26+
27+
The nodes visited are 0, 1, and 3, giving a maximal path quality of 0 + 32 + 43 = 75.
28+
29+
**Example 2:**
30+
31+
![](https://assets.leetcode.com/uploads/2021/10/19/ex2drawio.png)
32+
33+
**Input:** values = [5,10,15,20], edges = [[0,1,10],[1,2,10],[0,3,10]], maxTime = 30
34+
35+
**Output:** 25
36+
37+
**Explanation:**
38+
39+
One possible path is 0 -> 3 -> 0.
40+
41+
The total time taken is 10 + 10 = 20 <= 30.
42+
43+
The nodes visited are 0 and 3, giving a maximal path quality of 5 + 20 = 25.
44+
45+
**Example 3:**
46+
47+
![](https://assets.leetcode.com/uploads/2021/10/19/ex31drawio.png)
48+
49+
**Input:** values = [1,2,3,4], edges = [[0,1,10],[1,2,11],[2,3,12],[1,3,13]], maxTime = 50
50+
51+
**Output:** 7
52+
53+
**Explanation:**
54+
55+
One possible path is 0 -> 1 -> 3 -> 1 -> 0.
56+
57+
The total time taken is 10 + 13 + 13 + 10 = 46 <= 50.
58+
59+
The nodes visited are 0, 1, and 3, giving a maximal path quality of 1 + 2 + 4 = 7.
60+
61+
**Constraints:**
62+
63+
* `n == values.length`
64+
* `1 <= n <= 1000`
65+
* <code>0 <= values[i] <= 10<sup>8</sup></code>
66+
* `0 <= edges.length <= 2000`
67+
* `edges[j].length == 3`
68+
* <code>0 <= u<sub>j</sub> < v<sub>j</sub> <= n - 1</code>
69+
* <code>10 <= time<sub>j</sub>, maxTime <= 100</code>
70+
* All the pairs <code>[u<sub>j</sub>, v<sub>j</sub>]</code> are **unique**.
71+
* There are **at most four** edges connected to each node.
72+
* The graph may not be connected.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g2001_2100.s2068_check_whether_two_strings_are_almost_equivalent
2+
3+
// #Easy #String #Hash_Table #Counting #2023_06_26_Time_131_ms_(100.00%)_Space_34.1_MB_(100.00%)
4+
5+
import kotlin.math.abs
6+
7+
class Solution {
8+
fun checkAlmostEquivalent(word1: String, word2: String): Boolean {
9+
val freq = IntArray(26)
10+
for (i in word1.indices) {
11+
++freq[word1[i].code - 'a'.code]
12+
--freq[word2[i].code - 'a'.code]
13+
}
14+
for (i in freq) {
15+
if (abs(i) > 3) {
16+
return false
17+
}
18+
}
19+
return true
20+
}
21+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2068\. Check Whether Two Strings are Almost Equivalent
2+
3+
Easy
4+
5+
Two strings `word1` and `word2` are considered **almost equivalent** if the differences between the frequencies of each letter from `'a'` to `'z'` between `word1` and `word2` is **at most** `3`.
6+
7+
Given two strings `word1` and `word2`, each of length `n`, return `true` _if_ `word1` _and_ `word2` _are **almost equivalent**, or_ `false` _otherwise_.
8+
9+
The **frequency** of a letter `x` is the number of times it occurs in the string.
10+
11+
**Example 1:**
12+
13+
**Input:** word1 = "aaaa", word2 = "bccb"
14+
15+
**Output:** false
16+
17+
**Explanation:** There are 4 'a's in "aaaa" but 0 'a's in "bccb".
18+
19+
The difference is 4, which is more than the allowed 3.
20+
21+
**Example 2:**
22+
23+
**Input:** word1 = "abcdeef", word2 = "abaaacc"
24+
25+
**Output:** true
26+
27+
**Explanation:** The differences between the frequencies of each letter in word1 and word2 are at most 3:
28+
29+
- 'a' appears 1 time in word1 and 4 times in word2. The difference is 3.
30+
31+
- 'b' appears 1 time in word1 and 1 time in word2. The difference is 0.
32+
33+
- 'c' appears 1 time in word1 and 2 times in word2. The difference is 1.
34+
35+
- 'd' appears 1 time in word1 and 0 times in word2. The difference is 1.
36+
37+
- 'e' appears 2 times in word1 and 0 times in word2. The difference is 2.
38+
39+
- 'f' appears 1 time in word1 and 0 times in word2. The difference is 1.
40+
41+
**Example 3:**
42+
43+
**Input:** word1 = "cccddabba", word2 = "babababab"
44+
45+
**Output:** true
46+
47+
**Explanation:** The differences between the frequencies of each letter in word1 and word2 are at most 3:
48+
49+
- 'a' appears 2 times in word1 and 4 times in word2. The difference is 2.
50+
51+
- 'b' appears 2 times in word1 and 5 times in word2. The difference is 3.
52+
53+
- 'c' appears 3 times in word1 and 0 times in word2. The difference is 3.
54+
55+
- 'd' appears 2 times in word1 and 0 times in word2. The difference is 2.
56+
57+
**Constraints:**
58+
59+
* `n == word1.length == word2.length`
60+
* `1 <= n <= 100`
61+
* `word1` and `word2` consist only of lowercase English letters.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package g2001_2100.s2069_walking_robot_simulation_ii
2+
3+
// #Medium #Design #Simulation #2023_06_26_Time_636_ms_(100.00%)_Space_66.2_MB_(100.00%)
4+
5+
class Robot(width: Int, height: Int) {
6+
private var p: Int
7+
private val w: Int
8+
private val h: Int
9+
10+
init {
11+
w = width - 1
12+
h = height - 1
13+
p = 0
14+
}
15+
16+
fun step(num: Int) {
17+
p += num
18+
}
19+
20+
fun getPos(): IntArray {
21+
var remain = p % (2 * (w + h))
22+
if (remain <= w) {
23+
return intArrayOf(remain, 0)
24+
}
25+
remain -= w
26+
if (remain <= h) {
27+
return intArrayOf(w, remain)
28+
}
29+
remain -= h
30+
if (remain <= w) {
31+
return intArrayOf(w - remain, h)
32+
}
33+
remain -= w
34+
return intArrayOf(0, h - remain)
35+
}
36+
37+
fun getDir(): String {
38+
val pos = getPos()
39+
return if (p == 0 || pos[1] == 0 && pos[0] > 0) {
40+
"East"
41+
} else if (pos[0] == w && pos[1] > 0) {
42+
"North"
43+
} else if (pos[1] == h && pos[0] < w) {
44+
"West"
45+
} else {
46+
"South"
47+
}
48+
}
49+
}
50+
51+
/*
52+
* Your Robot object will be instantiated and called as such:
53+
* var obj = Robot(width, height)
54+
* obj.step(num)
55+
* var param_2 = obj.getPos()
56+
* var param_3 = obj.getDir()
57+
*/

0 commit comments

Comments
 (0)