Skip to content

Commit 2f6873e

Browse files
authored
Added tasks 1760, 1761, 1763, 1764, 1765.
1 parent 48f8168 commit 2f6873e

File tree

15 files changed

+542
-0
lines changed

15 files changed

+542
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g1701_1800.s1760_minimum_limit_of_balls_in_a_bag;
2+
3+
// #Medium #Array #Binary_Search #Binary_Search_II_Day_3
4+
// #2022_04_30_Time_44_ms_(78.49%)_Space_85.2_MB_(6.27%)
5+
6+
public class Solution {
7+
public int minimumSize(int[] nums, int maxOperations) {
8+
int left = 1;
9+
int right = 1_000_000_000;
10+
11+
while (left < right) {
12+
int mid = left + (right - left) / 2;
13+
if (operations(nums, mid) > maxOperations) {
14+
left = mid + 1;
15+
} else {
16+
right = mid;
17+
}
18+
}
19+
return left;
20+
}
21+
22+
private int operations(int[] nums, int mid) {
23+
int operations = 0;
24+
for (int num : nums) {
25+
operations += (num - 1) / mid;
26+
}
27+
28+
return operations;
29+
}
30+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
1760\. Minimum Limit of Balls in a Bag
2+
3+
Medium
4+
5+
You are given an integer array `nums` where the <code>i<sup>th</sup></code> bag contains `nums[i]` balls. You are also given an integer `maxOperations`.
6+
7+
You can perform the following operation at most `maxOperations` times:
8+
9+
* Take any bag of balls and divide it into two new bags with a **positive** number of balls.
10+
* For example, a bag of `5` balls can become two new bags of `1` and `4` balls, or two new bags of `2` and `3` balls.
11+
12+
Your penalty is the **maximum** number of balls in a bag. You want to **minimize** your penalty after the operations.
13+
14+
Return _the minimum possible penalty after performing the operations_.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [9], maxOperations = 2
19+
20+
**Output:** 3
21+
22+
**Explanation:**
23+
24+
- Divide the bag with 9 balls into two bags of sizes 6 and 3. [**9**] -> [6,3].
25+
26+
- Divide the bag with 6 balls into two bags of sizes 3 and 3. [**6**,3] -> [3,3,3]. The bag with the most number of balls has 3 balls, so your penalty is 3 and you should return 3.
27+
28+
**Example 2:**
29+
30+
**Input:** nums = [2,4,8,2], maxOperations = 4
31+
32+
**Output:** 2
33+
34+
**Explanation:**
35+
36+
- Divide the bag with 8 balls into two bags of sizes 4 and 4. [2,4,**8**,2] -> [2,4,4,4,2].
37+
38+
- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,**4**,4,4,2] -> [2,2,2,4,4,2].
39+
40+
- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,2,2,**4**,4,2] -> [2,2,2,2,2,4,2].
41+
42+
- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,2,2,2,2,**4**,2] -> [2,2,2,2,2,2,2,2]. The bag with the most number of balls has 2 balls, so your penalty is 2 an you should return 2.
43+
44+
**Example 3:**
45+
46+
**Input:** nums = [7,17], maxOperations = 2
47+
48+
**Output:** 7
49+
50+
**Constraints:**
51+
52+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
53+
* <code>1 <= maxOperations, nums[i] <= 10<sup>9</sup></code>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g1701_1800.s1761_minimum_degree_of_a_connected_trio_in_a_graph;
2+
3+
// #Hard #Graph #2022_04_30_Time_33_ms_(89.17%)_Space_73_MB_(55.41%)
4+
5+
public class Solution {
6+
public int minTrioDegree(int n, int[][] edges) {
7+
int[] degrees = new int[n + 1];
8+
int[][] adjMatrix = new int[n + 1][n + 1];
9+
10+
for (int[] edge : edges) {
11+
adjMatrix[edge[0]][edge[1]] = 1;
12+
adjMatrix[edge[1]][edge[0]] = 1;
13+
degrees[edge[0]]++;
14+
degrees[edge[1]]++;
15+
}
16+
17+
int minTrios = Integer.MAX_VALUE;
18+
for (int i = 1; i <= n; i++) {
19+
for (int j = i + 1; j <= n; j++) {
20+
if (adjMatrix[i][j] == 0) {
21+
continue;
22+
}
23+
for (int k = j + 1; k <= n; k++) {
24+
if (adjMatrix[j][k] == 0 || adjMatrix[i][k] == 0) {
25+
continue;
26+
}
27+
int trioDegree = degrees[i] + degrees[j] + degrees[k] - 6;
28+
minTrios = Math.min(minTrios, trioDegree);
29+
}
30+
}
31+
}
32+
return minTrios == Integer.MAX_VALUE ? -1 : minTrios;
33+
}
34+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
1761\. Minimum Degree of a Connected Trio in a Graph
2+
3+
Hard
4+
5+
You are given an undirected graph. You are given an integer `n` which is the number of nodes in the graph and an array `edges`, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an undirected edge between <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>.
6+
7+
A **connected trio** is a set of **three** nodes where there is an edge between **every** pair of them.
8+
9+
The **degree of a connected trio** is the number of edges where one endpoint is in the trio, and the other is not.
10+
11+
Return _the **minimum** degree of a connected trio in the graph, or_ `-1` _if the graph has no connected trios._
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2021/01/26/trios1.png)
16+
17+
**Input:** n = 6, edges = [[1,2],[1,3],[3,2],[4,1],[5,2],[3,6]]
18+
19+
**Output:** 3
20+
21+
**Explanation:** There is exactly one trio, which is [1,2,3]. The edges that form its degree are bolded in the figure above.
22+
23+
**Example 2:**
24+
25+
![](https://assets.leetcode.com/uploads/2021/01/26/trios2.png)
26+
27+
**Input:** n = 7, edges = [[1,3],[4,1],[4,3],[2,5],[5,6],[6,7],[7,5],[2,6]]
28+
29+
**Output:** 0
30+
31+
**Explanation:** There are exactly three trios:
32+
33+
1) [1,4,3] with degree 0.
34+
35+
2) [2,5,6] with degree 2.
36+
37+
3) [5,6,7] with degree 2.
38+
39+
**Constraints:**
40+
41+
* `2 <= n <= 400`
42+
* `edges[i].length == 2`
43+
* `1 <= edges.length <= n * (n-1) / 2`
44+
* <code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code>
45+
* <code>u<sub>i</sub> != v<sub>i</sub></code>
46+
* There are no repeated edges.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g1701_1800.s1763_longest_nice_substring;
2+
3+
// #Easy #String #Hash_Table #Bit_Manipulation #Sliding_Window
4+
// #2022_04_30_Time_5_ms_(61.88%)_Space_43.7_MB_(27.43%)
5+
6+
import java.util.HashSet;
7+
import java.util.Set;
8+
9+
public class Solution {
10+
public String longestNiceSubstring(String s) {
11+
12+
int index = isNotNiceString(s);
13+
14+
if (index == -1) {
15+
return s;
16+
}
17+
18+
String left = longestNiceSubstring(s.substring(0, index));
19+
String right = longestNiceSubstring(s.substring(index + 1));
20+
21+
return left.length() >= right.length() ? left : right;
22+
}
23+
24+
private int isNotNiceString(String s) {
25+
Set<Character> set = new HashSet<>();
26+
27+
for (char c : s.toCharArray()) {
28+
set.add(c);
29+
}
30+
31+
for (int i = 0; i < s.length(); i++) {
32+
char c = s.charAt(i);
33+
if (!set.contains(Character.toLowerCase(c))
34+
|| !set.contains(Character.toUpperCase(c))) {
35+
return i;
36+
}
37+
}
38+
39+
return -1;
40+
}
41+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
1763\. Longest Nice Substring
2+
3+
Easy
4+
5+
A string `s` is **nice** if, for every letter of the alphabet that `s` contains, it appears **both** in uppercase and lowercase. For example, `"abABB"` is nice because `'A'` and `'a'` appear, and `'B'` and `'b'` appear. However, `"abA"` is not because `'b'` appears, but `'B'` does not.
6+
7+
Given a string `s`, return _the longest **substring** of `s` that is **nice**. If there are multiple, return the substring of the **earliest** occurrence. If there are none, return an empty string_.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "YazaAay"
12+
13+
**Output:** "aAa"
14+
15+
**Explanation:** "aAa" is a nice string because 'A/a' is the only letter of the alphabet in s, and both 'A' and 'a' appear. "aAa" is the longest nice substring.
16+
17+
**Example 2:**
18+
19+
**Input:** s = "Bb"
20+
21+
**Output:** "Bb"
22+
23+
**Explanation:** "Bb" is a nice string because both 'B' and 'b' appear. The whole string is a substring.
24+
25+
**Example 3:**
26+
27+
**Input:** s = "c"
28+
29+
**Output:** ""
30+
31+
**Explanation:** There are no nice substrings.
32+
33+
**Constraints:**
34+
35+
* `1 <= s.length <= 100`
36+
* `s` consists of uppercase and lowercase English letters.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g1701_1800.s1764_form_array_by_concatenating_subarrays_of_another_array;
2+
3+
// #Medium #Array #Greedy #String_Matching #2022_04_30_Time_7_ms_(16.90%)_Space_42.4_MB_(85.92%)
4+
5+
import java.util.ArrayList;
6+
import java.util.Collections;
7+
import java.util.List;
8+
9+
public class Solution {
10+
public boolean canChoose(int[][] groups, int[] nums) {
11+
List<Integer> numsInt = new ArrayList<>();
12+
for (int num : nums) {
13+
numsInt.add(num);
14+
}
15+
int prevIndex = 0;
16+
for (int[] group : groups) {
17+
List<Integer> groupInt = new ArrayList<>();
18+
for (int num : group) {
19+
groupInt.add(num);
20+
}
21+
int index =
22+
Collections.indexOfSubList(
23+
numsInt.subList(prevIndex, numsInt.size()), groupInt);
24+
if (index != -1) {
25+
prevIndex = index + group.length;
26+
} else {
27+
return false;
28+
}
29+
}
30+
return true;
31+
}
32+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
1764\. Form Array by Concatenating Subarrays of Another Array
2+
3+
Medium
4+
5+
You are given a 2D integer array `groups` of length `n`. You are also given an integer array `nums`.
6+
7+
You are asked if you can choose `n` **disjoint** subarrays from the array `nums` such that the <code>i<sup>th</sup></code> subarray is equal to `groups[i]` (**0-indexed**), and if `i > 0`, the <code>(i-1)<sup>th</sup></code> subarray appears **before** the <code>i<sup>th</sup></code> subarray in `nums` (i.e. the subarrays must be in the same order as `groups`).
8+
9+
Return `true` _if you can do this task, and_ `false` _otherwise_.
10+
11+
Note that the subarrays are **disjoint** if and only if there is no index `k` such that `nums[k]` belongs to more than one subarray. A subarray is a contiguous sequence of elements within an array.
12+
13+
**Example 1:**
14+
15+
**Input:** groups = [[1,-1,-1],[3,-2,0]], nums = [1,-1,0,1,-1,-1,3,-2,0]
16+
17+
**Output:** true
18+
19+
**Explanation:** You can choose the 0<sup>th</sup> subarray as [1,-1,0,**1,-1,-1**,3,-2,0] and the 1<sup>st</sup> one as [1,-1,0,1,-1,-1,**3,-2,0**]. These subarrays are disjoint as they share no common nums[k] element.
20+
21+
**Example 2:**
22+
23+
**Input:** groups = [[10,-2],[1,2,3,4]], nums = [1,2,3,4,10,-2]
24+
25+
**Output:** false
26+
27+
**Explanation:** Note that choosing the subarrays [**1,2,3,4**,10,-2] and [1,2,3,4,**10,-2**] is incorrect because they are not in the same order as in groups. [10,-2] must come before [1,2,3,4].
28+
29+
**Example 3:**
30+
31+
**Input:** groups = [[1,2,3],[3,4]], nums = [7,7,1,2,3,4,7,7]
32+
33+
**Output:** false
34+
35+
**Explanation:** Note that choosing the subarrays [7,7,**1,2,3**,4,7,7] and [7,7,1,2,**3,4**,7,7] is invalid because they are not disjoint. They share a common elements nums[4] (0-indexed).
36+
37+
**Constraints:**
38+
39+
* `groups.length == n`
40+
* <code>1 <= n <= 10<sup>3</sup></code>
41+
* <code>1 <= groups[i].length, sum(groups[i].length) <= 10<sup>3</sup></code>
42+
* <code>1 <= nums.length <= 10<sup>3</sup></code>
43+
* <code>-10<sup>7</sup> <= groups[i][j], nums[k] <= 10<sup>7</sup></code>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g1701_1800.s1765_map_of_highest_peak;
2+
3+
// #Medium #Array #Breadth_First_Search #Matrix
4+
// #2022_04_30_Time_64_ms_(85.40%)_Space_139.8_MB_(98.14%)
5+
6+
import java.util.LinkedList;
7+
import java.util.Queue;
8+
9+
public class Solution {
10+
private final int[] dir = {0, 1, 0, -1, 0};
11+
12+
public int[][] highestPeak(int[][] isWater) {
13+
int h = 1;
14+
Queue<int[]> q = new LinkedList<>();
15+
for (int i = 0; i < isWater.length; i++) {
16+
for (int j = 0; j < isWater[0].length; j++) {
17+
isWater[i][j] = isWater[i][j] == 1 ? 0 : -1;
18+
if (isWater[i][j] == 0) {
19+
q.add(new int[] {i, j});
20+
}
21+
}
22+
}
23+
while (!q.isEmpty()) {
24+
Queue<int[]> q1 = new LinkedList<>();
25+
for (int[] cur : q) {
26+
int x = cur[0];
27+
int y = cur[1];
28+
for (int i = 0; i < 4; i++) {
29+
int nx = x + dir[i];
30+
int ny = y + dir[i + 1];
31+
if (nx >= 0
32+
&& nx < isWater.length
33+
&& ny >= 0
34+
&& ny < isWater[0].length
35+
&& isWater[nx][ny] == -1) {
36+
isWater[nx][ny] = h;
37+
q1.add(new int[] {nx, ny});
38+
}
39+
}
40+
}
41+
h++;
42+
q = q1;
43+
}
44+
return isWater;
45+
}
46+
}

0 commit comments

Comments
 (0)