Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added tasks 1760, 1761, 1763, 1764, 1765
  • Loading branch information
ThanhNIT committed Apr 30, 2022
commit e7b8e5115f1cb503d2c622af8e230057531b6e7d
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package g1701_1800.s1760_minimum_limit_of_balls_in_a_bag;

// #Medium #Array #Binary_Search #Binary_Search_II_Day_3
// #2022_04_30_Time_44_ms_(78.49%)_Space_85.2_MB_(6.27%)

public class Solution {
public int minimumSize(int[] nums, int maxOperations) {
int left = 1;
int right = 1_000_000_000;

while (left < right) {
int mid = left + (right - left) / 2;
if (operations(nums, mid) > maxOperations) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}

private int operations(int[] nums, int mid) {
int operations = 0;
for (int num : nums) {
operations += (num - 1) / mid;
}

return operations;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
1760\. Minimum Limit of Balls in a Bag

Medium

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`.

You can perform the following operation at most `maxOperations` times:

* Take any bag of balls and divide it into two new bags with a **positive** number of balls.
* 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.

Your penalty is the **maximum** number of balls in a bag. You want to **minimize** your penalty after the operations.

Return _the minimum possible penalty after performing the operations_.

**Example 1:**

**Input:** nums = [9], maxOperations = 2

**Output:** 3

**Explanation:**

- Divide the bag with 9 balls into two bags of sizes 6 and 3. [**9**] -> [6,3].

- 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.

**Example 2:**

**Input:** nums = [2,4,8,2], maxOperations = 4

**Output:** 2

**Explanation:**

- Divide the bag with 8 balls into two bags of sizes 4 and 4. [2,4,**8**,2] -> [2,4,4,4,2].

- 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].

- 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].

- 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.

**Example 3:**

**Input:** nums = [7,17], maxOperations = 2

**Output:** 7

**Constraints:**

* <code>1 <= nums.length <= 10<sup>5</sup></code>
* <code>1 <= maxOperations, nums[i] <= 10<sup>9</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package g1701_1800.s1761_minimum_degree_of_a_connected_trio_in_a_graph;

// #Hard #Graph #2022_04_30_Time_33_ms_(89.17%)_Space_73_MB_(55.41%)

public class Solution {
public int minTrioDegree(int n, int[][] edges) {
int[] degrees = new int[n + 1];
int[][] adjMatrix = new int[n + 1][n + 1];

for (int[] edge : edges) {
adjMatrix[edge[0]][edge[1]] = 1;
adjMatrix[edge[1]][edge[0]] = 1;
degrees[edge[0]]++;
degrees[edge[1]]++;
}

int minTrios = Integer.MAX_VALUE;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (adjMatrix[i][j] == 0) {
continue;
}
for (int k = j + 1; k <= n; k++) {
if (adjMatrix[j][k] == 0 || adjMatrix[i][k] == 0) {
continue;
}
int trioDegree = degrees[i] + degrees[j] + degrees[k] - 6;
minTrios = Math.min(minTrios, trioDegree);
}
}
}
return minTrios == Integer.MAX_VALUE ? -1 : minTrios;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
1761\. Minimum Degree of a Connected Trio in a Graph

Hard

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>.

A **connected trio** is a set of **three** nodes where there is an edge between **every** pair of them.

The **degree of a connected trio** is the number of edges where one endpoint is in the trio, and the other is not.

Return _the **minimum** degree of a connected trio in the graph, or_ `-1` _if the graph has no connected trios._

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/01/26/trios1.png)

**Input:** n = 6, edges = [[1,2],[1,3],[3,2],[4,1],[5,2],[3,6]]

**Output:** 3

**Explanation:** There is exactly one trio, which is [1,2,3]. The edges that form its degree are bolded in the figure above.

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/01/26/trios2.png)

**Input:** n = 7, edges = [[1,3],[4,1],[4,3],[2,5],[5,6],[6,7],[7,5],[2,6]]

**Output:** 0

**Explanation:** There are exactly three trios:

1) [1,4,3] with degree 0.

2) [2,5,6] with degree 2.

3) [5,6,7] with degree 2.

**Constraints:**

* `2 <= n <= 400`
* `edges[i].length == 2`
* `1 <= edges.length <= n * (n-1) / 2`
* <code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code>
* <code>u<sub>i</sub> != v<sub>i</sub></code>
* There are no repeated edges.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package g1701_1800.s1763_longest_nice_substring;

// #Easy #String #Hash_Table #Bit_Manipulation #Sliding_Window
// #2022_04_30_Time_5_ms_(61.88%)_Space_43.7_MB_(27.43%)

import java.util.HashSet;
import java.util.Set;

public class Solution {
public String longestNiceSubstring(String s) {

int index = isNotNiceString(s);

if (index == -1) {
return s;
}

String left = longestNiceSubstring(s.substring(0, index));
String right = longestNiceSubstring(s.substring(index + 1));

return left.length() >= right.length() ? left : right;
}

private int isNotNiceString(String s) {
Set<Character> set = new HashSet<>();

for (char c : s.toCharArray()) {
set.add(c);
}

for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!set.contains(Character.toLowerCase(c))
|| !set.contains(Character.toUpperCase(c))) {
return i;
}
}

return -1;
}
}
36 changes: 36 additions & 0 deletions src/main/java/g1701_1800/s1763_longest_nice_substring/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
1763\. Longest Nice Substring

Easy

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.

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_.

**Example 1:**

**Input:** s = "YazaAay"

**Output:** "aAa"

**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.

**Example 2:**

**Input:** s = "Bb"

**Output:** "Bb"

**Explanation:** "Bb" is a nice string because both 'B' and 'b' appear. The whole string is a substring.

**Example 3:**

**Input:** s = "c"

**Output:** ""

**Explanation:** There are no nice substrings.

**Constraints:**

* `1 <= s.length <= 100`
* `s` consists of uppercase and lowercase English letters.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package g1701_1800.s1764_form_array_by_concatenating_subarrays_of_another_array;

// #Medium #Array #Greedy #String_Matching #2022_04_30_Time_7_ms_(16.90%)_Space_42.4_MB_(85.92%)

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Solution {
public boolean canChoose(int[][] groups, int[] nums) {
int n = groups.length;
List<Integer> numsInt = new ArrayList<>();
for (int num : nums) {
numsInt.add(num);
}
int prevIndex = 0;
for (int[] group : groups) {
List<Integer> groupInt = new ArrayList<>();
for (int num : group) {
groupInt.add(num);
}
int index =
Collections.indexOfSubList(
numsInt.subList(prevIndex, numsInt.size()), groupInt);
if (index != -1) {
prevIndex = index + group.length;
} else {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
1764\. Form Array by Concatenating Subarrays of Another Array

Medium

You are given a 2D integer array `groups` of length `n`. You are also given an integer array `nums`.

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`).

Return `true` _if you can do this task, and_ `false` _otherwise_.

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.

**Example 1:**

**Input:** groups = [[1,-1,-1],[3,-2,0]], nums = [1,-1,0,1,-1,-1,3,-2,0]

**Output:** true

**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.

**Example 2:**

**Input:** groups = [[10,-2],[1,2,3,4]], nums = [1,2,3,4,10,-2]

**Output:** false

**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].

**Example 3:**

**Input:** groups = [[1,2,3],[3,4]], nums = [7,7,1,2,3,4,7,7]

**Output:** false

**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).

**Constraints:**

* `groups.length == n`
* <code>1 <= n <= 10<sup>3</sup></code>
* <code>1 <= groups[i].length, sum(groups[i].length) <= 10<sup>3</sup></code>
* <code>1 <= nums.length <= 10<sup>3</sup></code>
* <code>-10<sup>7</sup> <= groups[i][j], nums[k] <= 10<sup>7</sup></code>
44 changes: 44 additions & 0 deletions src/main/java/g1701_1800/s1765_map_of_highest_peak/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package g1701_1800.s1765_map_of_highest_peak;

// #Medium #Array #Breadth_First_Search #Matrix
// #2022_04_30_Time_64_ms_(85.40%)_Space_139.8_MB_(98.14%)

import java.util.LinkedList;
import java.util.Queue;

public class Solution {
private final int[] dir = {0, 1, 0, -1, 0};

public int[][] highestPeak(int[][] isWater) {
int h = 1;
Queue<int[]> q = new LinkedList<>();
for (int i = 0; i < isWater.length; i++) {
for (int j = 0; j < isWater[0].length; j++) {
isWater[i][j] = isWater[i][j] == 1 ? 0 : -1;
if (isWater[i][j] == 0) {
q.add(new int[] {i, j});
}
}
}
while (!q.isEmpty()) {
Queue<int[]> q1 = new LinkedList<>();
for (int[] cur : q) {
int x = cur[0], y = cur[1];
for (int i = 0; i < 4; i++) {
int nx = x + dir[i], ny = y + dir[i + 1];
if (nx >= 0
&& nx < isWater.length
&& ny >= 0
&& ny < isWater[0].length
&& isWater[nx][ny] == -1) {
isWater[nx][ny] = h;
q1.add(new int[] {nx, ny});
}
}
}
h++;
q = q1;
}
return isWater;
}
}
Loading