Skip to content

Commit 627b866

Browse files
authored
Added tasks 2201, 2203.
1 parent 293bf60 commit 627b866

File tree

6 files changed

+267
-0
lines changed

6 files changed

+267
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package g2201_2300.s2201_count_artifacts_that_can_be_extracted;
2+
3+
// #Medium #Array #Hash_Table #Simulation #2022_06_11_Time_7_ms_(82.97%)_Space_91.5_MB_(99.45%)
4+
5+
public class Solution {
6+
public int digArtifacts(int n, int[][] artifacts, int[][] dig) {
7+
int[][] ar = new int[n][n];
8+
for (int[] ints : dig) {
9+
ar[ints[0]][ints[1]] = 1;
10+
}
11+
int ans = 0;
12+
for (int[] artifact : artifacts) {
13+
int x1 = artifact[0];
14+
int y1 = artifact[1];
15+
int x2 = artifact[2];
16+
int y2 = artifact[3];
17+
int flag = 0;
18+
int a = x1;
19+
int b = y1;
20+
while (a <= x2) {
21+
b = y1;
22+
while (b <= y2) {
23+
if (ar[a][b] != 1) {
24+
flag = 1;
25+
break;
26+
}
27+
b++;
28+
}
29+
if (flag == 1) {
30+
break;
31+
}
32+
a++;
33+
}
34+
if (a == x2 + 1 && b == y2 + 1) {
35+
ans++;
36+
}
37+
}
38+
return ans;
39+
}
40+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2201\. Count Artifacts That Can Be Extracted
2+
3+
Medium
4+
5+
There is an `n x n` **0-indexed** grid with some artifacts buried in it. You are given the integer `n` and a **0-indexed** 2D integer array `artifacts` describing the positions of the rectangular artifacts where <code>artifacts[i] = [r1<sub>i</sub>, c1<sub>i</sub>, r2<sub>i</sub>, c2<sub>i</sub>]</code> denotes that the <code>i<sup>th</sup></code> artifact is buried in the subgrid where:
6+
7+
* <code>(r1<sub>i</sub>, c1<sub>i</sub>)</code> is the coordinate of the **top-left** cell of the <code>i<sup>th</sup></code> artifact and
8+
* <code>(r2<sub>i</sub>, c2<sub>i</sub>)</code> is the coordinate of the **bottom-right** cell of the <code>i<sup>th</sup></code> artifact.
9+
10+
You will excavate some cells of the grid and remove all the mud from them. If the cell has a part of an artifact buried underneath, it will be uncovered. If all the parts of an artifact are uncovered, you can extract it.
11+
12+
Given a **0-indexed** 2D integer array `dig` where <code>dig[i] = [r<sub>i</sub>, c<sub>i</sub>]</code> indicates that you will excavate the cell <code>(r<sub>i</sub>, c<sub>i</sub>)</code>, return _the number of artifacts that you can extract_.
13+
14+
The test cases are generated such that:
15+
16+
* No two artifacts overlap.
17+
* Each artifact only covers at most `4` cells.
18+
* The entries of `dig` are unique.
19+
20+
**Example 1:**
21+
22+
![](https://assets.leetcode.com/uploads/2019/09/16/untitled-diagram.jpg)
23+
24+
**Input:** n = 2, artifacts = [[0,0,0,0],[0,1,1,1]], dig = [[0,0],[0,1]]
25+
26+
**Output:** 1
27+
28+
**Explanation:** The different colors represent different artifacts. Excavated cells are labeled with a 'D' in the grid. There is 1 artifact that can be extracted, namely the red artifact. The blue artifact has one part in cell (1,1) which remains uncovered, so we cannot extract it. Thus, we return 1.
29+
30+
**Example 2:**
31+
32+
![](https://assets.leetcode.com/uploads/2019/09/16/untitled-diagram-1.jpg)
33+
34+
**Input:** n = 2, artifacts = [[0,0,0,0],[0,1,1,1]], dig = [[0,0],[0,1],[1,1]]
35+
36+
**Output:** 2
37+
38+
**Explanation:** Both the red and blue artifacts have all parts uncovered (labeled with a 'D') and can be extracted, so we return 2.
39+
40+
**Constraints:**
41+
42+
* `1 <= n <= 1000`
43+
* <code>1 <= artifacts.length, dig.length <= min(n<sup>2</sup>, 10<sup>5</sup>)</code>
44+
* `artifacts[i].length == 4`
45+
* `dig[i].length == 2`
46+
* <code>0 <= r1<sub>i</sub>, c1<sub>i</sub>, r2<sub>i</sub>, c2<sub>i</sub>, r<sub>i</sub>, c<sub>i</sub> <= n - 1</code>
47+
* <code>r1<sub>i</sub> <= r2<sub>i</sub></code>
48+
* <code>c1<sub>i</sub> <= c2<sub>i</sub></code>
49+
* No two artifacts will overlap.
50+
* The number of cells covered by an artifact is **at most** `4`.
51+
* The entries of `dig` are unique.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package g2201_2300.s2203_minimum_weighted_subgraph_with_the_required_paths;
2+
3+
// #Hard #Graph #Shortest_Path #2022_06_12_Time_241_ms_(50.34%)_Space_205.1_MB_(10.20%)
4+
5+
import java.util.Comparator;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
import java.util.PriorityQueue;
9+
10+
public class Solution {
11+
public long minimumWeight(int n, int[][] edges, int src1, int src2, int dest) {
12+
Map<Integer, Integer>[] g1 = new Map[n];
13+
Map<Integer, Integer>[] g2 = new Map[n];
14+
for (int[] edge : edges) {
15+
int s1 = edge[0];
16+
int s2 = edge[1];
17+
int w = edge[2];
18+
if (g1[s1] == null) {
19+
g1[s1] = new HashMap<>();
20+
}
21+
g1[s1].put(s2, Math.min(w, g1[s1].getOrDefault(s2, Integer.MAX_VALUE)));
22+
if (g2[s2] == null) {
23+
g2[s2] = new HashMap<>();
24+
}
25+
g2[s2].put(s1, Math.min(w, g2[s2].getOrDefault(s1, Integer.MAX_VALUE)));
26+
}
27+
Long[] dist1 = dijkstra(g1, src1);
28+
Long[] dist2 = dijkstra(g1, src2);
29+
Long[] dist3 = dijkstra(g2, dest);
30+
Long res = null;
31+
for (int i = 0; i < n; i += 1) {
32+
if (dist1[i] != null && dist2[i] != null && dist3[i] != null) {
33+
long sum = dist1[i] + dist2[i] + dist3[i];
34+
if (res == null || res > sum) {
35+
res = sum;
36+
}
37+
}
38+
}
39+
return res == null ? -1 : res;
40+
}
41+
42+
private Long[] dijkstra(Map<Integer, Integer>[] graph, int start) {
43+
Long[] res = new Long[graph.length];
44+
PriorityQueue<State> q = new PriorityQueue<>(Comparator.comparingLong(a -> a.w));
45+
q.offer(new State(start, 0L));
46+
while (!q.isEmpty()) {
47+
State cur = q.poll();
48+
if (res[cur.id] != null) {
49+
continue;
50+
}
51+
res[cur.id] = (long) cur.w;
52+
if (graph[cur.id] != null) {
53+
for (int next : graph[cur.id].keySet()) {
54+
if (res[next] == null) {
55+
q.offer(new State(next, cur.w + graph[cur.id].get(next)));
56+
}
57+
}
58+
}
59+
}
60+
return res;
61+
}
62+
63+
private static class State {
64+
long w;
65+
int id;
66+
67+
State(int id, long w) {
68+
this.id = id;
69+
this.w = w;
70+
}
71+
}
72+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2203\. Minimum Weighted Subgraph With the Required Paths
2+
3+
Hard
4+
5+
You are given an integer `n` denoting the number of nodes of a **weighted directed** graph. The nodes are numbered from `0` to `n - 1`.
6+
7+
You are also given a 2D integer array `edges` where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> denotes that there exists a **directed** edge from <code>from<sub>i</sub></code> to <code>to<sub>i</sub></code> with weight <code>weight<sub>i</sub></code>.
8+
9+
Lastly, you are given three **distinct** integers `src1`, `src2`, and `dest` denoting three distinct nodes of the graph.
10+
11+
Return _the **minimum weight** of a subgraph of the graph such that it is **possible** to reach_ `dest` _from both_ `src1` _and_ `src2` _via a set of edges of this subgraph_. In case such a subgraph does not exist, return `-1`.
12+
13+
A **subgraph** is a graph whose vertices and edges are subsets of the original graph. The **weight** of a subgraph is the sum of weights of its constituent edges.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2022/02/17/example1drawio.png)
18+
19+
**Input:** n = 6, edges = [[0,2,2],[0,5,6],[1,0,3],[1,4,5],[2,1,1],[2,3,3],[2,3,4],[3,4,2],[4,5,1]], src1 = 0, src2 = 1, dest = 5
20+
21+
**Output:** 9
22+
23+
**Explanation:** The above figure represents the input graph. The blue edges represent one of the subgraphs that yield the optimal answer. Note that the subgraph [[1,0,3],[0,5,6]] also yields the optimal answer. It is not possible to get a subgraph with less weight satisfying all the constraints.
24+
25+
**Example 2:**
26+
27+
![](https://assets.leetcode.com/uploads/2022/02/17/example2-1drawio.png)
28+
29+
**Input:** n = 3, edges = [[0,1,1],[2,1,1]], src1 = 0, src2 = 1, dest = 2
30+
31+
**Output:** -1
32+
33+
**Explanation:** The above figure represents the input graph. It can be seen that there does not exist any path from node 1 to node 2, hence there are no subgraphs satisfying all the constraints.
34+
35+
**Constraints:**
36+
37+
* <code>3 <= n <= 10<sup>5</sup></code>
38+
* <code>0 <= edges.length <= 10<sup>5</sup></code>
39+
* `edges[i].length == 3`
40+
* <code>0 <= from<sub>i</sub>, to<sub>i</sub>, src1, src2, dest <= n - 1</code>
41+
* <code>from<sub>i</sub> != to<sub>i</sub></code>
42+
* `src1`, `src2`, and `dest` are pairwise distinct.
43+
* <code>1 <= weight[i] <= 10<sup>5</sup></code>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g2201_2300.s2201_count_artifacts_that_can_be_extracted;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void digArtifacts() {
11+
assertThat(
12+
new Solution()
13+
.digArtifacts(
14+
2,
15+
new int[][] {{0, 0, 0, 0}, {0, 1, 1, 1}},
16+
new int[][] {{0, 0}, {0, 1}}),
17+
equalTo(1));
18+
}
19+
20+
@Test
21+
void digArtifacts2() {
22+
assertThat(
23+
new Solution()
24+
.digArtifacts(
25+
2,
26+
new int[][] {{0, 0, 0, 0}, {0, 1, 1, 1}},
27+
new int[][] {{0, 0}, {0, 1}, {1, 1}}),
28+
equalTo(2));
29+
}
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g2201_2300.s2203_minimum_weighted_subgraph_with_the_required_paths;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minimumWeight() {
11+
assertThat(
12+
new Solution()
13+
.minimumWeight(
14+
6,
15+
new int[][] {
16+
{0, 2, 2}, {0, 5, 6}, {1, 0, 3}, {1, 4, 5}, {2, 1, 1},
17+
{2, 3, 3}, {2, 3, 4}, {3, 4, 2}, {4, 5, 1}
18+
},
19+
0,
20+
1,
21+
5),
22+
equalTo(9L));
23+
}
24+
25+
@Test
26+
void minimumWeight2() {
27+
assertThat(
28+
new Solution().minimumWeight(3, new int[][] {{0, 1, 1}, {2, 1, 1}}, 0, 1, 2),
29+
equalTo(-1L));
30+
}
31+
}

0 commit comments

Comments
 (0)