Skip to content

Commit 354a219

Browse files
authored
Added tasks 2656-2660
1 parent 88c5c47 commit 354a219

File tree

15 files changed

+520
-0
lines changed

15 files changed

+520
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package g2601_2700.s2656_maximum_sum_with_exactly_k_elements
2+
3+
// #Easy #Array #Greedy #2023_07_21_Time_241_ms_(93.94%)_Space_40.5_MB_(63.64%)
4+
5+
class Solution {
6+
fun maximizeSum(nums: IntArray, k: Int): Int {
7+
return k * nums.max() + k * (k - 1) / 2
8+
}
9+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2656\. Maximum Sum With Exactly K Elements
2+
3+
Easy
4+
5+
You are given a **0-indexed** integer array `nums` and an integer `k`. Your task is to perform the following operation **exactly** `k` times in order to maximize your score:
6+
7+
1. Select an element `m` from `nums`.
8+
2. Remove the selected element `m` from the array.
9+
3. Add a new element with a value of `m + 1` to the array.
10+
4. Increase your score by `m`.
11+
12+
Return _the maximum score you can achieve after performing the operation exactly_ `k` _times._
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,2,3,4,5], k = 3
17+
18+
**Output:** 18
19+
20+
**Explanation:** We need to choose exactly 3 elements from nums to maximize the sum.
21+
22+
For the first iteration, we choose 5. Then sum is 5 and nums = [1,2,3,4,6]
23+
24+
For the second iteration, we choose 6. Then sum is 5 + 6 and nums = [1,2,3,4,7]
25+
26+
For the third iteration, we choose 7. Then sum is 5 + 6 + 7 = 18 and nums = [1,2,3,4,8]
27+
28+
So, we will return 18. It can be proven, that 18 is the maximum answer that we can achieve.
29+
30+
**Example 2:**
31+
32+
**Input:** nums = [5,5,5], k = 2
33+
34+
**Output:** 11
35+
36+
**Explanation:** We need to choose exactly 2 elements from nums to maximize the sum.
37+
38+
For the first iteration, we choose 5. Then sum is 5 and nums = [5,5,6]
39+
40+
For the second iteration, we choose 6. Then sum is 5 + 6 = 11 and nums = [5,5,7]
41+
42+
So, we will return 11.
43+
44+
It can be proven, that 11 is the maximum answer that we can achieve.
45+
46+
**Constraints:**
47+
48+
* `1 <= nums.length <= 100`
49+
* `1 <= nums[i] <= 100`
50+
* `1 <= k <= 100`
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g2601_2700.s2657_find_the_prefix_common_array_of_two_arrays
2+
3+
// #Medium #Array #Hash_Table #2023_07_21_Time_288_ms_(88.89%)_Space_39.9_MB_(100.00%)
4+
5+
class Solution {
6+
fun findThePrefixCommonArray(a: IntArray, b: IntArray): IntArray {
7+
val hsA = HashSet<Int>()
8+
val hsB = HashSet<Int>()
9+
val addedA = HashSet<Int>()
10+
val addedB = HashSet<Int>()
11+
val res = IntArray(a.size)
12+
for (i in a.indices) {
13+
val numA = a[i]
14+
val numB = b[i]
15+
hsA.add(numA)
16+
hsB.add(numB)
17+
if (i > 0) res[i] += res[i - 1] else res[i] = 0
18+
if (numA in hsB && numA !in addedB) {
19+
addedA.add(numA)
20+
res[i] += 1
21+
}
22+
if (numB in hsA && numB !in addedA) {
23+
addedB.add(numB)
24+
res[i] += 1
25+
}
26+
}
27+
return res
28+
}
29+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2657\. Find the Prefix Common Array of Two Arrays
2+
3+
Medium
4+
5+
You are given two **0-indexed** integer permutations `A` and `B` of length `n`.
6+
7+
A **prefix common array** of `A` and `B` is an array `C` such that `C[i]` is equal to the count of numbers that are present at or before the index `i` in both `A` and `B`.
8+
9+
Return _the **prefix common array** of_ `A` _and_ `B`.
10+
11+
A sequence of `n` integers is called a **permutation** if it contains all integers from `1` to `n` exactly once.
12+
13+
**Example 1:**
14+
15+
**Input:** A = [1,3,2,4], B = [3,1,2,4]
16+
17+
**Output:** [0,2,3,4]
18+
19+
**Explanation:** At i = 0: no number is common, so C[0] = 0.
20+
21+
At i = 1: 1 and 3 are common in A and B, so C[1] = 2.
22+
23+
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.
24+
25+
At i = 3: 1, 2, 3, and 4 are common in A and B, so C[3] = 4.
26+
27+
**Example 2:**
28+
29+
**Input:** A = [2,3,1], B = [3,1,2]
30+
31+
**Output:** [0,1,3]
32+
33+
**Explanation:** At i = 0: no number is common, so C[0] = 0.
34+
35+
At i = 1: only 3 is common in A and B, so C[1] = 1.
36+
37+
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.
38+
39+
**Constraints:**
40+
41+
* `1 <= A.length == B.length == n <= 50`
42+
* `1 <= A[i], B[i] <= n`
43+
* `It is guaranteed that A and B are both a permutation of n integers.`
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g2601_2700.s2658_maximum_number_of_fish_in_a_grid
2+
3+
// #Medium #Array #Matrix #Union_Find #Depth_First_Search #Breadth_First_Search
4+
// #2023_07_21_Time_269_ms_(80.00%)_Space_45.4_MB_(80.00%)
5+
6+
class Solution {
7+
fun findMaxFish(grid: Array<IntArray>): Int {
8+
val visited = Array(grid.size) { BooleanArray(grid[0].size) }
9+
val dir = arrayOf(
10+
intArrayOf(0, 1),
11+
intArrayOf(0, -1),
12+
intArrayOf(1, 0),
13+
intArrayOf(-1, 0)
14+
)
15+
16+
fun isValid(x: Int, y: Int) = x in (0..grid.lastIndex) && y in (0..grid[0].lastIndex) &&
17+
grid[x][y] != 0 && !visited[x][y]
18+
19+
fun dfs(x: Int, y: Int): Int {
20+
if (!isValid(x, y)) {
21+
return 0
22+
}
23+
visited[x][y] = true
24+
var total = grid[x][y]
25+
for (d in dir) {
26+
total += dfs(x + d[0], y + d[1])
27+
}
28+
return total
29+
}
30+
31+
var res = 0
32+
for (i in grid.indices) {
33+
for (j in grid[0].indices) {
34+
if (grid[i][j] != 0)
35+
res = maxOf(res, dfs(i, j))
36+
}
37+
}
38+
39+
return res
40+
}
41+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2658\. Maximum Number of Fish in a Grid
2+
3+
Medium
4+
5+
You are given a **0-indexed** 2D matrix `grid` of size `m x n`, where `(r, c)` represents:
6+
7+
* A **land** cell if `grid[r][c] = 0`, or
8+
* A **water** cell containing `grid[r][c]` fish, if `grid[r][c] > 0`.
9+
10+
A fisher can start at any **water** cell `(r, c)` and can do the following operations any number of times:
11+
12+
* Catch all the fish at cell `(r, c)`, or
13+
* Move to any adjacent **water** cell.
14+
15+
Return _the **maximum** number of fish the fisher can catch if he chooses his starting cell optimally, or_ `0` if no water cell exists.
16+
17+
An **adjacent** cell of the cell `(r, c)`, is one of the cells `(r, c + 1)`, `(r, c - 1)`, `(r + 1, c)` or `(r - 1, c)` if it exists.
18+
19+
**Example 1:**
20+
21+
![](https://assets.leetcode.com/uploads/2023/03/29/example.png)
22+
23+
**Input:** grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]
24+
25+
**Output:** 7
26+
27+
**Explanation:** The fisher can start at cell `(1,3)` and collect 3 fish, then move to cell `(2,3)` and collect 4 fish.
28+
29+
**Example 2:**
30+
31+
![](https://assets.leetcode.com/uploads/2023/03/29/example2.png)
32+
33+
**Input:** grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]
34+
35+
**Output:** 1
36+
37+
**Explanation:** The fisher can start at cells (0,0) or (3,3) and collect a single fish.
38+
39+
**Constraints:**
40+
41+
* `m == grid.length`
42+
* `n == grid[i].length`
43+
* `1 <= m, n <= 10`
44+
* `0 <= grid[i][j] <= 10`
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g2601_2700.s2659_make_array_empty
2+
3+
// #Hard #Array #Sorting #Greedy #Binary_Search #Ordered_Set #Segment_Tree #Binary_Indexed_Tree
4+
// #2023_07_21_Time_728_ms_(100.00%)_Space_71_MB_(100.00%)
5+
6+
class Solution {
7+
fun countOperationsToEmptyArray(nums: IntArray): Long {
8+
val sortNums = Array(nums.size) { IntArray(2) }
9+
for (i in nums.indices) {
10+
sortNums[i][0] = nums[i]
11+
sortNums[i][1] = i
12+
}
13+
sortNums.sortBy { it[0] }
14+
var res = 0L + nums.size
15+
for (i in 1..sortNums.lastIndex) {
16+
if (sortNums[i - 1][1] > sortNums[i][1])
17+
res += nums.size - i
18+
}
19+
return res
20+
}
21+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2659\. Make Array Empty
2+
3+
Hard
4+
5+
You are given an integer array `nums` containing **distinct** numbers, and you can perform the following operations **until the array is empty**:
6+
7+
* If the first element has the **smallest** value, remove it
8+
* Otherwise, put the first element at the **end** of the array.
9+
10+
Return _an integer denoting the number of operations it takes to make_ `nums` _empty._
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [3,4,-1]
15+
16+
**Output:** 5
17+
18+
Operation Array
19+
1 [4, -1, 3]
20+
2 [-1, 3, 4]
21+
3 [3, 4]
22+
4 [4]
23+
5 []
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [1,2,4,3]
28+
29+
**Output:** 5
30+
31+
Operation Array
32+
1 [2, 4, 3]
33+
2 [4, 3]
34+
3 [3, 4]
35+
4 [4]
36+
5 []
37+
38+
**Example 3:**
39+
40+
**Input:** nums = [1,2,3]
41+
42+
**Output:** 3
43+
44+
Operation Array
45+
1 [2, 3]
46+
2 [3]
47+
3 []
48+
49+
**Constraints:**
50+
51+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
52+
* <code>-10<sup>9 </sup><= nums[i] <= 10<sup>9</sup></code>
53+
* All values in `nums` are **distinct**.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g2601_2700.s2660_determine_the_winner_of_a_bowling_game
2+
3+
// #Easy #Array #Simulation #2023_07_21_Time_263_ms_(85.71%)_Space_40.3_MB_(85.71%)
4+
5+
class Solution {
6+
fun isWinner(player1: IntArray, player2: IntArray): Int {
7+
var p1Score = 0
8+
var p2Score = 0
9+
var isTen = 0
10+
for (score in player1) {
11+
p1Score += if (isTen > 0) 2 * score else score
12+
if (isTen > 0) isTen--
13+
if (score == 10) isTen = 2
14+
}
15+
isTen = 0
16+
for (score in player2) {
17+
p2Score += if (isTen > 0) 2 * score else score
18+
if (isTen > 0) isTen--
19+
if (score == 10) isTen = 2
20+
}
21+
return when {
22+
p1Score == p2Score -> 0
23+
p1Score > p2Score -> 1
24+
else -> 2
25+
}
26+
}
27+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2660\. Determine the Winner of a Bowling Game
2+
3+
Easy
4+
5+
You are given two **0-indexed** integer arrays `player1` and `player2`, that represent the number of pins that player 1 and player 2 hit in a bowling game, respectively.
6+
7+
The bowling game consists of `n` turns, and the number of pins in each turn is exactly `10`.
8+
9+
Assume a player hit <code>x<sub>i</sub></code> pins in the <code>i<sup>th</sup></code> turn. The value of the <code>i<sup>th</sup></code> turn for the player is:
10+
11+
* <code>2x<sub>i</sub></code> if the player hit `10` pins in any of the previous two turns.
12+
* Otherwise, It is <code>x<sub>i</sub></code>.
13+
14+
The score of the player is the sum of the values of their `n` turns.
15+
16+
Return
17+
18+
* `1` _if the score of player 1 is more than the score of player 2,_
19+
* `2` _if the score of player 2 is more than the score of player 1, and_
20+
* `0` _in case of a draw._
21+
22+
**Example 1:**
23+
24+
**Input:** player1 = [4,10,7,9], player2 = [6,5,2,3]
25+
26+
**Output:** 1
27+
28+
**Explanation:** The score of player1 is 4 + 10 + 2*7 + 2*9 = 46.
29+
30+
The score of player2 is 6 + 5 + 2 + 3 = 16.
31+
32+
Score of player1 is more than the score of player2, so, player1 is the winner, and the answer is 1.
33+
34+
**Example 2:**
35+
36+
**Input:** player1 = [3,5,7,6], player2 = [8,10,10,2]
37+
38+
**Output:** 2
39+
40+
**Explanation:** The score of player1 is 3 + 5 + 7 + 6 = 21.
41+
42+
The score of player2 is 8 + 10 + 2*10 + 2*2 = 42.
43+
44+
Score of player2 is more than the score of player1, so, player2 is the winner, and the answer is 2.
45+
46+
**Example 3:**
47+
48+
**Input:** player1 = [2,3], player2 = [4,1]
49+
50+
**Output:** 0
51+
52+
**Explanation:** The score of player1 is 2 + 3 = 5
53+
54+
The score of player2 is 4 + 1 = 5
55+
56+
The score of player1 equals to the score of player2, so, there is a draw, and the answer is 0.
57+
58+
**Constraints:**
59+
60+
* `n == player1.length == player2.length`
61+
* `1 <= n <= 1000`
62+
* `0 <= player1[i], player2[i] <= 10`

0 commit comments

Comments
 (0)