Skip to content

Commit f3f3551

Browse files
authored
Added tasks 2661-2666
1 parent 5a50362 commit f3f3551

File tree

14 files changed

+515
-0
lines changed

14 files changed

+515
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2601_2700.s2661_first_completely_painted_row_or_column
2+
3+
// #Medium #Array #Hash_Table #Matrix #2023_07_25_Time_901_ms_(100.00%)_Space_73.6_MB_(83.33%)
4+
5+
class Solution {
6+
fun firstCompleteIndex(arr: IntArray, mat: Array<IntArray>): Int {
7+
val map: HashMap<Int, Int> = HashMap()
8+
var ans = mat.size * mat[0].size
9+
for (i in arr.indices) {
10+
map.put(arr[i], i)
11+
}
12+
for (i in mat.indices) {
13+
var maxV = 0
14+
for (j in mat[0].indices) {
15+
maxV = maxV.coerceAtLeast(map[mat[i][j]]!!)
16+
}
17+
ans = ans.coerceAtMost(maxV)
18+
}
19+
for (i in mat[0].indices) {
20+
var maxV = 0
21+
for (j in mat.indices) {
22+
maxV = maxV.coerceAtLeast(map[mat[j][i]]!!)
23+
}
24+
ans = ans.coerceAtMost(maxV)
25+
}
26+
return ans
27+
}
28+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2661\. First Completely Painted Row or Column
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer array `arr`, and an `m x n` integer **matrix** `mat`. `arr` and `mat` both contain **all** the integers in the range `[1, m * n]`.
6+
7+
Go through each index `i` in `arr` starting from index `0` and paint the cell in `mat` containing the integer `arr[i]`.
8+
9+
Return _the smallest index_ `i` _at which either a row or a column will be completely painted in_ `mat`.
10+
11+
**Example 1:**
12+
13+
![](image explanation for example 1)![image explanation for example 1](https://assets.leetcode.com/uploads/2023/01/18/grid1.jpg)
14+
15+
**Input:** arr = [1,3,4,2], mat = [[1,4],[2,3]]
16+
17+
**Output:** 2
18+
19+
**Explanation:** The moves are shown in order, and both the first row and second column of the matrix become fully painted at arr[2].
20+
21+
**Example 2:**
22+
23+
![image explanation for example 2](https://assets.leetcode.com/uploads/2023/01/18/grid2.jpg)
24+
25+
**Input:** arr = [2,8,7,4,1,3,5,6,9], mat = [[3,2,5],[1,4,6],[8,7,9]]
26+
27+
**Output:** 3
28+
29+
**Explanation:** The second column becomes fully painted at arr[3].
30+
31+
**Constraints:**
32+
33+
* `m == mat.length`
34+
* `n = mat[i].length`
35+
* `arr.length == m * n`
36+
* <code>1 <= m, n <= 10<sup>5</sup></code>
37+
* <code>1 <= m * n <= 10<sup>5</sup></code>
38+
* `1 <= arr[i], mat[r][c] <= m * n`
39+
* All the integers of `arr` are **unique**.
40+
* All the integers of `mat` are **unique**.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package g2601_2700.s2662_minimum_cost_of_a_path_with_special_roads
2+
3+
// #Medium #Array #Heap_Priority_Queue #Graph #Shortest_Path
4+
// #2023_07_25_Time_690_ms_(100.00%)_Space_59.5_MB_(50.00%)
5+
6+
import java.util.PriorityQueue
7+
8+
class Solution {
9+
fun minimumCost(start: IntArray, target: IntArray, specialRoads: Array<IntArray>): Int {
10+
val pointList = mutableListOf<Point>()
11+
val costMap = HashMap<Pair<Point, Point>, Int>()
12+
val distMap = HashMap<Point, Int>()
13+
val sp = Point(start[0], start[1])
14+
distMap[sp] = 0
15+
for (road in specialRoads) {
16+
val p = Point(road[0], road[1])
17+
val q = Point(road[2], road[3])
18+
val cost = road[4]
19+
if (costMap.getOrDefault(Pair(p, q), Int.MAX_VALUE) > cost) {
20+
costMap[Pair(p, q)] = cost
21+
}
22+
pointList.add(p)
23+
pointList.add(q)
24+
distMap[p] = Int.MAX_VALUE
25+
distMap[q] = Int.MAX_VALUE
26+
}
27+
val tp = Point(target[0], target[1])
28+
pointList.add(tp)
29+
distMap[tp] = Int.MAX_VALUE
30+
val points = pointList.distinct()
31+
val pq = PriorityQueue<PointWithCost>()
32+
pq.offer(PointWithCost(sp, 0))
33+
while (pq.isNotEmpty()) {
34+
val curr = pq.poll()
35+
val cost = curr.cost
36+
val cp = curr.p
37+
if (cp == tp) return cost
38+
for (np in points) {
39+
if (cp == np) continue
40+
var nextCost = cost + dist(cp, np)
41+
if (costMap.containsKey(Pair(cp, np))) {
42+
nextCost = nextCost.coerceAtMost(cost + costMap[Pair(cp, np)]!!)
43+
}
44+
if (nextCost < distMap[np]!!) {
45+
distMap[np] = nextCost
46+
pq.offer(PointWithCost(np, nextCost))
47+
}
48+
}
49+
}
50+
return -1
51+
}
52+
53+
fun dist(sp: Point, tp: Point): Int {
54+
return kotlin.math.abs(sp.x - tp.x) + kotlin.math.abs(sp.y - tp.y)
55+
}
56+
}
57+
58+
data class Point(val x: Int, val y: Int)
59+
60+
data class PointWithCost(val p: Point, val cost: Int) : Comparable<PointWithCost> {
61+
override fun compareTo(other: PointWithCost) = compareValuesBy(this, other) { it.cost }
62+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2662\. Minimum Cost of a Path With Special Roads
2+
3+
Medium
4+
5+
You are given an array `start` where `start = [startX, startY]` represents your initial position `(startX, startY)` in a 2D space. You are also given the array `target` where `target = [targetX, targetY]` represents your target position `(targetX, targetY)`.
6+
7+
The cost of going from a position `(x1, y1)` to any other position in the space `(x2, y2)` is `|x2 - x1| + |y2 - y1|`.
8+
9+
There are also some special roads. You are given a 2D array `specialRoads` where <code>specialRoads[i] = [x1<sub>i</sub>, y1<sub>i</sub>, x2<sub>i</sub>, y2<sub>i</sub>, cost<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> special road can take you from <code>(x1<sub>i</sub>, y1<sub>i</sub>)</code> to <code>(x2<sub>i</sub>, y2<sub>i</sub>)</code> with a cost equal to <code>cost<sub>i</sub></code>. You can use each special road any number of times.
10+
11+
Return _the minimum cost required to go from_ `(startX, startY)` to `(targetX, targetY)`.
12+
13+
**Example 1:**
14+
15+
**Input:** start = [1,1], target = [4,5], specialRoads = [[1,2,3,3,2],[3,4,4,5,1]]
16+
17+
**Output:** 5
18+
19+
**Explanation:** The optimal path from (1,1) to (4,5) is the following:
20+
- (1,1) -> (1,2). This move has a cost of |1 - 1| + |2 - 1| = 1.
21+
- (1,2) -> (3,3). This move uses the first special edge, the cost is 2.
22+
- (3,3) -> (3,4). This move has a cost of |3 - 3| + |4 - 3| = 1.
23+
- (3,4) -> (4,5). This move uses the second special edge, the cost is 1.
24+
25+
So the total cost is 1 + 2 + 1 + 1 = 5.
26+
27+
It can be shown that we cannot achieve a smaller total cost than 5.
28+
29+
**Example 2:**
30+
31+
**Input:** start = [3,2], target = [5,7], specialRoads = [[3,2,3,4,4],[3,3,5,5,5],[3,4,5,6,6]]
32+
33+
**Output:** 7
34+
35+
**Explanation:** It is optimal to not use any special edges and go directly from the starting to the ending position with a cost |5 - 3| + |7 - 2| = 7.
36+
37+
**Constraints:**
38+
39+
* `start.length == target.length == 2`
40+
* <code>1 <= startX <= targetX <= 10<sup>5</sup></code>
41+
* <code>1 <= startY <= targetY <= 10<sup>5</sup></code>
42+
* `1 <= specialRoads.length <= 200`
43+
* `specialRoads[i].length == 5`
44+
* <code>startX <= x1<sub>i</sub>, x2<sub>i</sub> <= targetX</code>
45+
* <code>startY <= y1<sub>i</sub>, y2<sub>i</sub> <= targetY</code>
46+
* <code>1 <= cost<sub>i</sub> <= 10<sup>5</sup></code>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g2601_2700.s2663_lexicographically_smallest_beautiful_string
2+
3+
// #Hard #String #Greedy #2023_07_25_Time_324_ms_(100.00%)_Space_40.6_MB_(100.00%)
4+
5+
class Solution {
6+
fun smallestBeautifulString(s: String, k: Int): String {
7+
val n = s.length
8+
val charr = s.toCharArray()
9+
for (i in n - 1 downTo 0) {
10+
++charr[i]
11+
var canbuild = true
12+
if (charr[i] > 'a' + k - 1) continue
13+
while (!isValid(charr, i)) {
14+
++charr[i]
15+
if (charr[i] > 'a' + k - 1) {
16+
canbuild = false
17+
break
18+
}
19+
}
20+
if (!canbuild) continue
21+
for (j in i + 1 until n) {
22+
charr[j] = 'a'
23+
while (!isValid(charr, j)) {
24+
++charr[j]
25+
}
26+
}
27+
return StringBuilder().append(charr).toString()
28+
}
29+
return ""
30+
}
31+
32+
private fun isValid(s: CharArray, i: Int): Boolean {
33+
if (i - 1 >= 0 && s[i - 1] == s[i]) return false
34+
if (i - 2 >= 0 && s[i - 2] == s[i]) return false
35+
return true
36+
}
37+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2663\. Lexicographically Smallest Beautiful String
2+
3+
Hard
4+
5+
A string is **beautiful** if:
6+
7+
* It consists of the first `k` letters of the English lowercase alphabet.
8+
* It does not contain any substring of length `2` or more which is a palindrome.
9+
10+
You are given a beautiful string `s` of length `n` and a positive integer `k`.
11+
12+
Return _the lexicographically smallest string of length_ `n`_, which is larger than_ `s` _and is **beautiful**_. If there is no such string, return an empty string.
13+
14+
A string `a` is lexicographically larger than a string `b` (of the same length) if in the first position where `a` and `b` differ, `a` has a character strictly larger than the corresponding character in `b`.
15+
16+
* For example, `"abcd"` is lexicographically larger than `"abcc"` because the first position they differ is at the fourth character, and `d` is greater than `c`.
17+
18+
**Example 1:**
19+
20+
**Input:** s = "abcz", k = 26
21+
22+
**Output:** "abda"
23+
24+
**Explanation:**
25+
26+
The string "abda" is beautiful and lexicographically larger than the string "abcz".
27+
28+
It can be proven that there is no string that is lexicographically larger than the string "abcz", beautiful, and lexicographically smaller than the string "abda".
29+
30+
**Example 2:**
31+
32+
**Input:** s = "dc", k = 4
33+
34+
**Output:** ""
35+
36+
**Explanation:** It can be proven that there is no string that is lexicographically larger than the string "dc" and is beautiful.
37+
38+
**Constraints:**
39+
40+
* <code>1 <= n == s.length <= 10<sup>5</sup></code>
41+
* `4 <= k <= 26`
42+
* `s` is a beautiful string.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2665\. Counter II
2+
3+
Easy
4+
5+
Write a function `createCounter`. It should accept an initial integer `init`. It should return an object with three functions.
6+
7+
The three functions are:
8+
9+
* `increment()` increases the current value by 1 and then returns it.
10+
* `decrement()` reduces the current value by 1 and then returns it.
11+
* `reset()` sets the current value to `init` and then returns it.
12+
13+
**Example 1:**
14+
15+
**Input:** init = 5, calls = ["increment","reset","decrement"]
16+
17+
**Output:** [6,5,4]
18+
19+
**Explanation:**
20+
21+
const counter = createCounter(5);
22+
counter.increment(); // 6
23+
counter.reset(); // 5
24+
counter.decrement(); // 4
25+
26+
**Example 2:**
27+
28+
**Input:** init = 0, calls = ["increment","increment","decrement","reset","reset"]
29+
30+
**Output:** [1,2,1,0,0]
31+
32+
**Explanation:**
33+
34+
const counter = createCounter(0);
35+
counter.increment(); // 1
36+
counter.increment(); // 2
37+
counter.decrement(); // 1
38+
counter.reset(); // 0
39+
counter.reset(); // 0
40+
41+
**Constraints:**
42+
43+
* `-1000 <= init <= 1000`
44+
* `total calls not to exceed 1000`
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// #Easy #2023_07_25_Time_65_ms_(86.59%)_Space_44.8_MB_(93.23%)
2+
3+
type ReturnObj = {
4+
increment: () => number,
5+
decrement: () => number,
6+
reset: () => number,
7+
}
8+
9+
function createCounter(init: number): ReturnObj {
10+
let n = init
11+
return {
12+
increment: () => {
13+
n = n + 1
14+
return n
15+
},
16+
decrement: () => {
17+
n = n - 1
18+
return n
19+
},
20+
reset: () => {
21+
n = init
22+
return n
23+
}
24+
}
25+
}
26+
27+
/*
28+
* const counter = createCounter(5)
29+
* counter.increment(); // 6
30+
* counter.reset(); // 5
31+
* counter.decrement(); // 4
32+
*/
33+
34+
export { createCounter }
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2666\. Allow One Function Call
2+
3+
Easy
4+
5+
Given a function `fn`, return a new function that is identical to the original function except that it ensures `fn` is called at most once.
6+
7+
* The first time the returned function is called, it should return the same result as `fn`.
8+
* Every subsequent time it is called, it should return `undefined`.
9+
10+
**Example 1:**
11+
12+
**Input:** fn = (a,b,c) => (a + b + c), calls = [[1,2,3],[2,3,6]]
13+
14+
**Output:** [{"calls":1,"value":6}]
15+
16+
**Explanation:**
17+
18+
const onceFn = once(fn);
19+
onceFn(1, 2, 3); // 6
20+
onceFn(2, 3, 6); // undefined, fn was not called
21+
22+
**Example 2:**
23+
24+
**Input:** fn = (a,b,c) => (a \* b \* c), calls = [[5,7,4],[2,3,6],[4,6,8]]
25+
26+
**Output:** [{"calls":1,"value":140}]
27+
28+
**Explanation:**
29+
30+
const onceFn = once(fn);
31+
onceFn(5, 7, 4); // 140
32+
onceFn(2, 3, 6); // undefined, fn was not called
33+
onceFn(4, 6, 8); // undefined, fn was not called
34+
35+
**Constraints:**
36+
37+
* `1 <= calls.length <= 10`
38+
* `1 <= calls[i].length <= 100`
39+
* `2 <= JSON.stringify(calls).length <= 1000`

0 commit comments

Comments
 (0)