Skip to content

Commit ac602ca

Browse files
authored
Added tasks 2251-2300
1 parent 8f778c7 commit ac602ca

File tree

118 files changed

+4407
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+4407
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package g2201_2300.s2251_number_of_flowers_in_full_bloom
2+
3+
// #Hard #Array #Hash_Table #Sorting #Binary_Search #Prefix_Sum #Ordered_Set
4+
// #2023_06_28_Time_973_ms_(100.00%)_Space_88.6_MB_(100.00%)
5+
6+
import java.util.Arrays
7+
import java.util.PriorityQueue
8+
9+
class Solution {
10+
fun fullBloomFlowers(flowers: Array<IntArray>, persons: IntArray): IntArray {
11+
Arrays.sort(flowers, { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) })
12+
val ans = IntArray(persons.size)
13+
val pq = PriorityQueue({ a: Pair, b: Pair -> a.j.compareTo(b.j) })
14+
var j = 0
15+
val t = Array(persons.size) { IntArray(2) }
16+
for (i in persons.indices) {
17+
t[i][0] = persons[i]
18+
t[i][1] = i
19+
}
20+
Arrays.sort(t, { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) })
21+
for (ints in t) {
22+
while (pq.isNotEmpty()) {
23+
if (pq.peek().j < ints[0]) {
24+
pq.poll()
25+
} else {
26+
break
27+
}
28+
}
29+
while (j < flowers.size) {
30+
if (flowers[j][0] <= ints[0] && flowers[j][1] >= ints[0]) {
31+
pq.add(Pair(flowers[j][0], flowers[j][1]))
32+
j++
33+
continue
34+
}
35+
if (flowers[j][1] < ints[0]) {
36+
j++
37+
continue
38+
}
39+
break
40+
}
41+
ans[ints[1]] = pq.size
42+
}
43+
return ans
44+
}
45+
46+
private class Pair(var i: Int, var j: Int)
47+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2251\. Number of Flowers in Full Bloom
2+
3+
Hard
4+
5+
You are given a **0-indexed** 2D integer array `flowers`, where <code>flowers[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means the <code>i<sup>th</sup></code> flower will be in **full bloom** from <code>start<sub>i</sub></code> to <code>end<sub>i</sub></code> (**inclusive**). You are also given a **0-indexed** integer array `persons` of size `n`, where `persons[i]` is the time that the <code>i<sup>th</sup></code> person will arrive to see the flowers.
6+
7+
Return _an integer array_ `answer` _of size_ `n`_, where_ `answer[i]` _is the **number** of flowers that are in full bloom when the_ <code>i<sup>th</sup></code> _person arrives._
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2022/03/02/ex1new.jpg)
12+
13+
**Input:** flowers = [[1,6],[3,7],[9,12],[4,13]], persons = [2,3,7,11]
14+
15+
**Output:** [1,2,2,2]
16+
17+
**Explanation:** The figure above shows the times when the flowers are in full bloom and when the people arrive. For each person, we return the number of flowers in full bloom during their arrival.
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2022/03/02/ex2new.jpg)
22+
23+
**Input:** flowers = [[1,10],[3,3]], persons = [3,3,2]
24+
25+
**Output:** [2,2,1]
26+
27+
**Explanation:** The figure above shows the times when the flowers are in full bloom and when the people arrive. For each person, we return the number of flowers in full bloom during their arrival.
28+
29+
**Constraints:**
30+
31+
* <code>1 <= flowers.length <= 5 * 10<sup>4</sup></code>
32+
* `flowers[i].length == 2`
33+
* <code>1 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>9</sup></code>
34+
* <code>1 <= persons.length <= 5 * 10<sup>4</sup></code>
35+
* <code>1 <= persons[i] <= 10<sup>9</sup></code>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g2201_2300.s2255_count_prefixes_of_a_given_string
2+
3+
// #Easy #Array #String #2023_06_28_Time_162_ms_(100.00%)_Space_37.5_MB_(66.67%)
4+
5+
class Solution {
6+
fun countPrefixes(words: Array<String>, s: String): Int {
7+
var count = 0
8+
for (str in words) {
9+
if (s.indexOf(str) == 0) {
10+
++count
11+
}
12+
}
13+
return count
14+
}
15+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2255\. Count Prefixes of a Given String
2+
3+
Easy
4+
5+
You are given a string array `words` and a string `s`, where `words[i]` and `s` comprise only of **lowercase English letters**.
6+
7+
Return _the **number of strings** in_ `words` _that are a **prefix** of_ `s`.
8+
9+
A **prefix** of a string is a substring that occurs at the beginning of the string. A **substring** is a contiguous sequence of characters within a string.
10+
11+
**Example 1:**
12+
13+
**Input:** words = ["a","b","c","ab","bc","abc"], s = "abc"
14+
15+
**Output:** 3
16+
17+
**Explanation:**
18+
19+
The strings in words which are a prefix of s = "abc" are:
20+
21+
"a", "ab", and "abc".
22+
23+
Thus the number of strings in words which are a prefix of s is 3.
24+
25+
**Example 2:**
26+
27+
**Input:** words = ["a","a"], s = "aa"
28+
29+
**Output:** 2
30+
31+
**Explanation:**
32+
33+
Both of the strings are a prefix of s.
34+
35+
Note that the same string can occur multiple times in words, and it should be counted each time.
36+
37+
**Constraints:**
38+
39+
* `1 <= words.length <= 1000`
40+
* `1 <= words[i].length, s.length <= 10`
41+
* `words[i]` and `s` consist of lowercase English letters **only**.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g2201_2300.s2256_minimum_average_difference
2+
3+
// #Medium #Array #Prefix_Sum #2023_06_28_Time_527_ms_(100.00%)_Space_52.1_MB_(100.00%)
4+
5+
class Solution {
6+
fun minimumAverageDifference(nums: IntArray): Int {
7+
var numsSum: Long = 0
8+
for (num in nums) {
9+
numsSum += num.toLong()
10+
}
11+
var minAverageDiff = Long.MAX_VALUE
12+
var sumFromFront: Long = 0
13+
var index = 0
14+
for (i in nums.indices) {
15+
sumFromFront += nums[i].toLong()
16+
val numbersRight = if (i == nums.size - 1) 1 else nums.size - i - 1
17+
val averageDiff = Math.abs(sumFromFront / (i + 1) - (numsSum - sumFromFront) / numbersRight)
18+
if (minAverageDiff > averageDiff) {
19+
minAverageDiff = averageDiff
20+
index = i
21+
}
22+
if (averageDiff == 0L) {
23+
break
24+
}
25+
}
26+
return index
27+
}
28+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2256\. Minimum Average Difference
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer array `nums` of length `n`.
6+
7+
The **average difference** of the index `i` is the **absolute** **difference** between the average of the **first** `i + 1` elements of `nums` and the average of the **last** `n - i - 1` elements. Both averages should be **rounded down** to the nearest integer.
8+
9+
Return _the index with the **minimum average difference**_. If there are multiple such indices, return the **smallest** one.
10+
11+
**Note:**
12+
13+
* The **absolute difference** of two numbers is the absolute value of their difference.
14+
* The **average** of `n` elements is the **sum** of the `n` elements divided (**integer division**) by `n`.
15+
* The average of `0` elements is considered to be `0`.
16+
17+
**Example 1:**
18+
19+
**Input:** nums = [2,5,3,9,5,3]
20+
21+
**Output:** 3
22+
23+
**Explanation:**
24+
25+
- The average difference of index 0 is: |2 / 1 - (5 + 3 + 9 + 5 + 3) / 5| = |2 / 1 - 25 / 5| = |2 - 5| = 3.
26+
27+
- The average difference of index 1 is: |(2 + 5) / 2 - (3 + 9 + 5 + 3) / 4| = |7 / 2 - 20 / 4| = |3 - 5| = 2.
28+
29+
- The average difference of index 2 is: |(2 + 5 + 3) / 3 - (9 + 5 + 3) / 3| = |10 / 3 - 17 / 3| = |3 - 5| = 2.
30+
31+
- The average difference of index 3 is: |(2 + 5 + 3 + 9) / 4 - (5 + 3) / 2| = |19 / 4 - 8 / 2| = |4 - 4| = 0.
32+
33+
- The average difference of index 4 is: |(2 + 5 + 3 + 9 + 5) / 5 - 3 / 1| = |24 / 5 - 3 / 1| = |4 - 3| = 1.
34+
35+
- The average difference of index 5 is: |(2 + 5 + 3 + 9 + 5 + 3) / 6 - 0| = |27 / 6 - 0| = |4 - 0| = 4.
36+
37+
The average difference of index 3 is the minimum average difference so return 3.
38+
39+
**Example 2:**
40+
41+
**Input:** nums = [0]
42+
43+
**Output:** 0
44+
45+
**Explanation:**
46+
47+
The only index is 0 so return 0.
48+
49+
The average difference of index 0 is: |0 / 1 - 0| = |0 - 0| = 0.
50+
51+
**Constraints:**
52+
53+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
54+
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package g2201_2300.s2257_count_unguarded_cells_in_the_grid
2+
3+
// #Medium #Array #Matrix #Simulation #2023_06_28_Time_901_ms_(100.00%)_Space_76.1_MB_(100.00%)
4+
5+
@Suppress("NAME_SHADOWING")
6+
class Solution {
7+
fun countUnguarded(m: Int, n: Int, guards: Array<IntArray>, walls: Array<IntArray>): Int {
8+
val matrix = Array(m) { CharArray(n) }
9+
var result = 0
10+
for (i in guards.indices) {
11+
val guardRow = guards[i][0]
12+
val guardCol = guards[i][1]
13+
matrix[guardRow][guardCol] = 'G'
14+
}
15+
for (i in walls.indices) {
16+
val wallRow = walls[i][0]
17+
val wallCol = walls[i][1]
18+
matrix[wallRow][wallCol] = 'W'
19+
}
20+
for (i in guards.indices) {
21+
var currentRow = guards[i][0]
22+
var currentCol = guards[i][1] - 1
23+
extracted(matrix, currentRow, currentCol)
24+
currentRow = guards[i][0]
25+
currentCol = guards[i][1] + 1
26+
extracted(n, matrix, currentRow, currentCol)
27+
currentRow = guards[i][0] - 1
28+
currentCol = guards[i][1]
29+
extracted1(matrix, currentRow, currentCol)
30+
currentRow = guards[i][0] + 1
31+
currentCol = guards[i][1]
32+
extracted1(m, matrix, currentRow, currentCol)
33+
}
34+
for (i in 0 until m) {
35+
for (j in 0 until n) {
36+
if (matrix[i][j] != 'R' && matrix[i][j] != 'G' && matrix[i][j] != 'W') {
37+
result++
38+
}
39+
}
40+
}
41+
return result
42+
}
43+
44+
private fun extracted1(m: Int, matrix: Array<CharArray>, currentRow: Int, currentCol: Int) {
45+
var currentRow = currentRow
46+
while (currentRow <= m - 1) {
47+
if (matrix[currentRow][currentCol] != 'W' && matrix[currentRow][currentCol] != 'G') {
48+
matrix[currentRow][currentCol] = 'R'
49+
} else {
50+
break
51+
}
52+
currentRow++
53+
}
54+
}
55+
56+
private fun extracted1(matrix: Array<CharArray>, currentRow: Int, currentCol: Int) {
57+
var currentRow = currentRow
58+
while (currentRow >= 0) {
59+
if (matrix[currentRow][currentCol] != 'W' && matrix[currentRow][currentCol] != 'G') {
60+
matrix[currentRow][currentCol] = 'R'
61+
} else {
62+
break
63+
}
64+
currentRow--
65+
}
66+
}
67+
68+
private fun extracted(n: Int, matrix: Array<CharArray>, currentRow: Int, currentCol: Int) {
69+
var currentCol = currentCol
70+
while (currentCol <= n - 1) {
71+
if (matrix[currentRow][currentCol] != 'W' && matrix[currentRow][currentCol] != 'G') {
72+
matrix[currentRow][currentCol] = 'R'
73+
} else {
74+
break
75+
}
76+
currentCol++
77+
}
78+
}
79+
80+
private fun extracted(matrix: Array<CharArray>, currentRow: Int, currentCol: Int) {
81+
var currentCol = currentCol
82+
while (currentCol >= 0) {
83+
if (matrix[currentRow][currentCol] != 'W' && matrix[currentRow][currentCol] != 'G') {
84+
matrix[currentRow][currentCol] = 'R'
85+
} else {
86+
break
87+
}
88+
currentCol--
89+
}
90+
}
91+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2257\. Count Unguarded Cells in the Grid
2+
3+
Medium
4+
5+
You are given two integers `m` and `n` representing a **0-indexed** `m x n` grid. You are also given two 2D integer arrays `guards` and `walls` where <code>guards[i] = [row<sub>i</sub>, col<sub>i</sub>]</code> and <code>walls[j] = [row<sub>j</sub>, col<sub>j</sub>]</code> represent the positions of the <code>i<sup>th</sup></code> guard and <code>j<sup>th</sup></code> wall respectively.
6+
7+
A guard can see **every** cell in the four cardinal directions (north, east, south, or west) starting from their position unless **obstructed** by a wall or another guard. A cell is **guarded** if there is **at least** one guard that can see it.
8+
9+
Return _the number of unoccupied cells that are **not** **guarded**._
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2022/03/10/example1drawio2.png)
14+
15+
**Input:** m = 4, n = 6, guards = [[0,0],[1,1],[2,3]], walls = [[0,1],[2,2],[1,4]]
16+
17+
**Output:** 7
18+
19+
**Explanation:** The guarded and unguarded cells are shown in red and green respectively in the above diagram. There are a total of 7 unguarded cells, so we return 7.
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2022/03/10/example2drawio.png)
24+
25+
**Input:** m = 3, n = 3, guards = [[1,1]], walls = [[0,1],[1,0],[2,1],[1,2]]
26+
27+
**Output:** 4
28+
29+
**Explanation:** The unguarded cells are shown in green in the above diagram. There are a total of 4 unguarded cells, so we return 4.
30+
31+
**Constraints:**
32+
33+
* <code>1 <= m, n <= 10<sup>5</sup></code>
34+
* <code>2 <= m * n <= 10<sup>5</sup></code>
35+
* <code>1 <= guards.length, walls.length <= 5 * 10<sup>4</sup></code>
36+
* `2 <= guards.length + walls.length <= m * n`
37+
* `guards[i].length == walls[j].length == 2`
38+
* <code>0 <= row<sub>i</sub>, row<sub>j</sub> < m</code>
39+
* <code>0 <= col<sub>i</sub>, col<sub>j</sub> < n</code>
40+
* All the positions in `guards` and `walls` are **unique**.

0 commit comments

Comments
 (0)