Skip to content

Commit a08d116

Browse files
committed
300423
1 parent 271e482 commit a08d116

File tree

4 files changed

+413
-0
lines changed

4 files changed

+413
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package leetcode.editor.en.Q2656;
2+
import java.util.*;
3+
import javafx.util.Pair;
4+
5+
//You are given a 0-indexed integer array nums and an integer k. Your task is
6+
//to perform the following operation exactly k times in order to maximize your
7+
//score:
8+
//
9+
//
10+
// Select an element m from nums.
11+
// Remove the selected element m from the array.
12+
// Add a new element with a value of m + 1 to the array.
13+
// Increase your score by m.
14+
//
15+
//
16+
// Return the maximum score you can achieve after performing the operation
17+
//exactly k times.
18+
//
19+
//
20+
// Example 1:
21+
//
22+
//
23+
//Input: nums = [1,2,3,4,5], k = 3
24+
//Output: 18
25+
//Explanation: We need to choose exactly 3 elements from nums to maximize the
26+
//sum.
27+
//For the first iteration, we choose 5. Then sum is 5 and nums = [1,2,3,4,6]
28+
//For the second iteration, we choose 6. Then sum is 5 + 6 and nums = [1,2,3,4,7
29+
//]
30+
//For the third iteration, we choose 7. Then sum is 5 + 6 + 7 = 18 and nums = [1
31+
//,2,3,4,8]
32+
//So, we will return 18.
33+
//It can be proven, that 18 is the maximum answer that we can achieve.
34+
//
35+
//
36+
// Example 2:
37+
//
38+
//
39+
//Input: nums = [5,5,5], k = 2
40+
//Output: 11
41+
//Explanation: We need to choose exactly 2 elements from nums to maximize the
42+
//sum.
43+
//For the first iteration, we choose 5. Then sum is 5 and nums = [5,5,6]
44+
//For the second iteration, we choose 6. Then sum is 5 + 6 = 11 and nums = [5,5,
45+
//7]
46+
//So, we will return 11.
47+
//It can be proven, that 11 is the maximum answer that we can achieve.
48+
//
49+
//
50+
//
51+
// Constraints:
52+
//
53+
//
54+
// 1 <= nums.length <= 100
55+
// 1 <= nums[i] <= 100
56+
// 1 <= k <= 100
57+
//
58+
//
59+
//
60+
//
61+
//
62+
// 👍 63 👎 3
63+
64+
65+
//leetcode submit region begin(Prohibit modification and deletion)
66+
class Solution {
67+
public int maximizeSum(int[] nums, int k) {
68+
PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder());
69+
for (int num : nums) {
70+
pq.add(num);
71+
}
72+
int sum = 0;
73+
74+
while (k > 0) {
75+
int next = pq.poll();
76+
sum += next;
77+
pq.add(next + 1);
78+
k--;
79+
}
80+
81+
return sum;
82+
83+
}
84+
85+
86+
}
87+
//leetcode submit region end(Prohibit modification and deletion)
88+
89+
90+
91+
public class MaximumSumWithExactlyKElements extends Solution {}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package leetcode.editor.en.Q2657;
2+
import java.util.*;
3+
import javafx.util.Pair;
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
8+
//the count of numbers that are present at or before the index i in both A and B.
9+
//
10+
// Return the prefix common array of A and B.
11+
//
12+
// A sequence of n integers is called a permutation if it contains all integers
13+
//from 1 to n exactly once.
14+
//
15+
//
16+
// Example 1:
17+
//
18+
//
19+
//Input: A = [1,3,2,4], B = [3,1,2,4]
20+
//Output: [0,2,3,4]
21+
//Explanation: At i = 0: no number is common, so C[0] = 0.
22+
//At i = 1: 1 and 3 are common in A and B, so C[1] = 2.
23+
//At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.
24+
//At i = 3: 1, 2, 3, and 4 are common in A and B, so C[3] = 4.
25+
//
26+
//
27+
// Example 2:
28+
//
29+
//
30+
//Input: A = [2,3,1], B = [3,1,2]
31+
//Output: [0,1,3]
32+
//Explanation: At i = 0: no number is common, so C[0] = 0.
33+
//At i = 1: only 3 is common in A and B, so C[1] = 1.
34+
//At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.
35+
//
36+
//
37+
//
38+
// Constraints:
39+
//
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.
44+
//
45+
//
46+
// 👍 101 👎 2
47+
48+
49+
//leetcode submit region begin(Prohibit modification and deletion)
50+
class Solution {
51+
public int[] findThePrefixCommonArray(int[] A, int[] B) {
52+
int[] ans = new int[A.length];
53+
54+
HashSet<Integer> pending = new HashSet<>();
55+
56+
for (int i = 0; i < A.length; i++) {
57+
if (i > 0) {
58+
ans[i] = ans[i - 1];
59+
}
60+
while (pending.contains(A[i]) || pending.contains(B[i])) {
61+
if (pending.contains(A[i])) {
62+
ans[i]++;
63+
pending.remove(A[i]);
64+
}
65+
if (pending.contains(B[i])) {
66+
ans[i]++;
67+
pending.remove(B[i]);
68+
}
69+
}
70+
71+
if (A[i] != B[i]) {
72+
pending.add(A[i]);
73+
pending.add(B[i]);
74+
} else {
75+
ans[i]++;
76+
}
77+
78+
79+
}
80+
return ans;
81+
}
82+
}
83+
//leetcode submit region end(Prohibit modification and deletion)
84+
85+
86+
87+
public class FindThePrefixCommonArrayOfTwoArrays extends Solution {}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package leetcode.editor.en.Q2658;
2+
import java.util.*;
3+
import javafx.util.Pair;
4+
5+
//You are given a 0-indexed 2D matrix grid of size m x n, where (r, c)
6+
//represents:
7+
//
8+
//
9+
// A land cell if grid[r][c] = 0, or
10+
// A water cell containing grid[r][c] fish, if grid[r][c] > 0.
11+
//
12+
//
13+
// A fisher can start at any water cell (r, c) and can do the following
14+
//operations any number of times:
15+
//
16+
//
17+
// Catch all the fish at cell (r, c), or
18+
// Move to any adjacent water cell.
19+
//
20+
//
21+
// Return the maximum number of fish the fisher can catch if he chooses his
22+
//starting cell optimally, or 0 if no water cell exists.
23+
//
24+
// An adjacent cell of the cell (r, c), is one of the cells (r, c + 1), (r, c -
25+
//1), (r + 1, c) or (r - 1, c) if it exists.
26+
//
27+
//
28+
// Example 1:
29+
//
30+
//
31+
//Input: grid = [[0,2,1,0],[4,0,0,3],[1,0,0,4],[0,3,2,0]]
32+
//Output: 7
33+
//Explanation: The fisher can start at cell (1,3) and collect 3 fish, then move
34+
//to cell (2,3) and collect 4 fish.
35+
//
36+
//
37+
// Example 2:
38+
//
39+
//
40+
//Input: grid = [[1,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1]]
41+
//Output: 1
42+
//Explanation: The fisher can start at cells (0,0) or (3,3) and collect a
43+
//single fish.
44+
//
45+
//
46+
//
47+
// Constraints:
48+
//
49+
//
50+
// m == grid.length
51+
// n == grid[i].length
52+
// 1 <= m, n <= 10
53+
// 0 <= grid[i][j] <= 10
54+
//
55+
//
56+
// 👍 148 👎 12
57+
58+
59+
//leetcode submit region begin(Prohibit modification and deletion)
60+
class Solution {
61+
62+
int[][] DIRECTIONS = new int[][]{
63+
new int[]{-1, 0},
64+
new int[]{1, 0},
65+
new int[]{0, -1},
66+
new int[]{0, 1}
67+
};
68+
69+
Integer[][] cache;
70+
boolean[][] visiting;
71+
72+
public int findMaxFish(int[][] grid) {
73+
Queue<int[]> q = new LinkedList<>();
74+
for (int i = 0; i < grid.length; i++) {
75+
for (int j = 0; j < grid[i].length; j++) {
76+
if (grid[i][j] != 0) {
77+
q.add(new int[]{i, j});
78+
}
79+
}
80+
}
81+
82+
83+
int ans = 0;
84+
if (q.isEmpty()) return ans;
85+
86+
cache = new Integer[grid.length][grid[0].length];
87+
while (!q.isEmpty()) {
88+
89+
int[] start = q.poll();
90+
int row = start[0];
91+
int col = start[1];
92+
visiting = new boolean[grid.length][grid[0].length];
93+
94+
95+
ans = Math.max(ans, dfs(row, col, grid));
96+
}
97+
98+
99+
return ans;
100+
101+
102+
}
103+
104+
private int dfs(int row, int col, int[][] grid) {
105+
106+
int best = 0;
107+
108+
if (cache[row][col] != null) {
109+
return cache[row][col];
110+
}
111+
int cell = grid[row][col];
112+
visiting[row][col] = true;
113+
grid[row][col] = 0;
114+
115+
for (int[] dir : DIRECTIONS) {
116+
int newRow = row + dir[0];
117+
int newCol = col + dir[1];
118+
if (isValidIdx(newRow, newCol, grid)) {
119+
int fish = grid[newRow][newCol];
120+
grid[newRow][newCol] = 0;
121+
best += fish + dfs(newRow, newCol, grid);
122+
grid[newRow][newCol] = fish;
123+
}
124+
}
125+
best += cell;
126+
grid[row][col] = cell;
127+
cache[row][col] = best;
128+
// visiting[row][col] = false;
129+
return best;
130+
131+
}
132+
133+
private boolean isValidIdx(int row, int col, int[][] grid) {
134+
return row >= 0 && row < grid.length && col >= 0 && col < grid[row].length && grid[row][col] > 0 && !visiting[row][col];
135+
}
136+
137+
}
138+
139+
140+
//leetcode submit region end(Prohibit modification and deletion)
141+
142+
143+
144+
public class MaximumNumberOfFishInAGrid extends Solution {}

0 commit comments

Comments
 (0)