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 2101, 2102, 2103, 2104, 2105
  • Loading branch information
ThanhNIT committed May 31, 2022
commit ace4d2418eed87cd951f3bfdf5eebc1c8fe24bf2
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package g2101_2200.s2101_detonate_the_maximum_bombs;

// #Medium #Array #Math #Depth_First_Search #Breadth_First_Search #Graph #Geometry
// #2022_05_31_Time_27_ms_(94.17%)_Space_49.6_MB_(48.45%)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {
public int maximumDetonation(int[][] bombs) {
int n = bombs.length;
List<Integer>[] graph = new List[n];
for (int i = 0; i < n; i++) graph[i] = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
double dx = bombs[i][0] - bombs[j][0];
double dy = bombs[i][1] - bombs[j][1];
double r1 = bombs[i][2], r2 = bombs[j][2];
double dist = dx * dx + dy * dy;
if (dist <= r1 * r1) {
graph[i].add(j);
}
if (dist <= r2 * r2) {
graph[j].add(i);
}
}
}
boolean[] visited = new boolean[n];
int ans = 0;
for (int i = 0; i < n; i++) {
ans = Math.max(ans, dfs(graph, i, visited));
if (ans == n) {
return ans;
}
Arrays.fill(visited, false);
}
return ans;
}

private int dfs(List<Integer>[] graph, int i, boolean[] visited) {
int cc = 0;
if (visited[i]) {
return 0;
}
visited[i] = true;
for (int neigh : graph[i]) {
cc += dfs(graph, neigh, visited);
}
return cc + 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
2101\. Detonate the Maximum Bombs

Medium

You are given a list of bombs. The **range** of a bomb is defined as the area where its effect can be felt. This area is in the shape of a **circle** with the center as the location of the bomb.

The bombs are represented by a **0-indexed** 2D integer array `bombs` where <code>bombs[i] = [x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub>]</code>. <code>x<sub>i</sub></code> and <code>y<sub>i</sub></code> denote the X-coordinate and Y-coordinate of the location of the <code>i<sup>th</sup></code> bomb, whereas <code>r<sub>i</sub></code> denotes the **radius** of its range.

You may choose to detonate a **single** bomb. When a bomb is detonated, it will detonate **all bombs** that lie in its range. These bombs will further detonate the bombs that lie in their ranges.

Given the list of `bombs`, return _the **maximum** number of bombs that can be detonated if you are allowed to detonate **only one** bomb_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/11/06/desmos-eg-3.png)

**Input:** bombs = [[2,1,3],[6,1,4]]

**Output:** 2

**Explanation:**

The above figure shows the positions and ranges of the 2 bombs.

If we detonate the left bomb, the right bomb will not be affected.

But if we detonate the right bomb, both bombs will be detonated.

So the maximum bombs that can be detonated is max(1, 2) = 2.

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/11/06/desmos-eg-2.png)

**Input:** bombs = [[1,1,5],[10,10,5]]

**Output:** 1

**Explanation:** Detonating either bomb will not detonate the other bomb, so the maximum number of bombs that can be detonated is 1.

**Example 3:**

![](https://assets.leetcode.com/uploads/2021/11/07/desmos-eg1.png)

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

**Output:** 5

**Explanation:** The best bomb to detonate is bomb 0 because:

- Bomb 0 detonates bombs 1 and 2. The red circle denotes the range of bomb 0.

- Bomb 2 detonates bomb 3. The blue circle denotes the range of bomb 2.

- Bomb 3 detonates bomb 4. The green circle denotes the range of bomb 3.

Thus all 5 bombs are detonated.

**Constraints:**

* `1 <= bombs.length <= 100`
* `bombs[i].length == 3`
* <code>1 <= x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub> <= 10<sup>5</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package g2101_2200.s2102_sequentially_ordinal_rank_tracker;

// #Hard #Design #Heap_Priority_Queue #Ordered_Set #Data_Stream
// #2022_05_31_Time_194_ms_(79.48%)_Space_79.6_MB_(69.32%)

import java.util.TreeSet;

public class SORTracker {
static class Location {
String name;
int score;

public Location(String name, int score) {
this.name = name;
this.score = score;
}

public String getName() {
return name;
}

public int getScore() {
return score;
}
}

TreeSet<Location> tSet1;
TreeSet<Location> tSet2;

public SORTracker() {
tSet1 =
new TreeSet<Location>(
(a, b) -> {
if (a.score != b.score) {
return b.getScore() - a.getScore();
} else {
return a.getName().compareTo(b.getName());
}
});

tSet2 =
new TreeSet<Location>(
(a, b) -> {
if (a.score != b.score) {
return b.getScore() - a.getScore();
} else {
return a.getName().compareTo(b.getName());
}
});
}

public void add(String name, int score) {
tSet1.add(new Location(name, score));
tSet2.add(tSet1.pollLast());
}

public String get() {
Location res = tSet2.pollFirst();
tSet1.add(res);
assert res != null;
return res.name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
2102\. Sequentially Ordinal Rank Tracker

Hard

A scenic location is represented by its `name` and attractiveness `score`, where `name` is a **unique** string among all locations and `score` is an integer. Locations can be ranked from the best to the worst. The **higher** the score, the better the location. If the scores of two locations are equal, then the location with the **lexicographically smaller** name is better.

You are building a system that tracks the ranking of locations with the system initially starting with no locations. It supports:

* **Adding** scenic locations, **one at a time**.
* **Querying** the <code>i<sup>th</sup></code> **best** location of **all locations already added**, where `i` is the number of times the system has been queried (including the current query).
* For example, when the system is queried for the <code>4<sup>th</sup></code> time, it returns the <code>4<sup>th</sup></code> best location of all locations already added.

Note that the test data are generated so that **at any time**, the number of queries **does not exceed** the number of locations added to the system.

Implement the `SORTracker` class:

* `SORTracker()` Initializes the tracker system.
* `void add(string name, int score)` Adds a scenic location with `name` and `score` to the system.
* `string get()` Queries and returns the <code>i<sup>th</sup></code> best location, where `i` is the number of times this method has been invoked (including this invocation).

**Example 1:**

**Input** ["SORTracker", "add", "add", "get", "add", "get", "add", "get", "add", "get", "add", "get", "get"] [[], ["bradford", 2], ["branford", 3], [], ["alps", 2], [], ["orland", 2], [], ["orlando", 3], [], ["alpine", 2], [], []]

**Output:** [null, null, null, "branford", null, "alps", null, "bradford", null, "bradford", null, "bradford", "orland"]

**Explanation:**

SORTracker tracker = new SORTracker(); // Initialize the tracker system.
tracker.add("bradford", 2); // Add location with name="bradford" and score=2 to the system.
tracker.add("branford", 3); // Add location with name="branford" and score=3 to the system.
tracker.get(); // The sorted locations, from best to worst, are: branford, bradford.
// Note that branford precedes bradford due to its **higher score** (3 > 2).
// This is the 1<sup>st</sup> time get() is called, so return the best location: "branford".
tracker.add("alps", 2); // Add location with name="alps" and score=2 to the system.
tracker.get(); // Sorted locations: branford, alps, bradford.
// Note that alps precedes bradford even though they have the same score (2).
// This is because "alps" is **lexicographically smaller** than "bradford".
// Return the 2<sup>nd</sup> best location "alps", as it is the 2<sup>nd</sup> time get() is called.
tracker.add("orland", 2); // Add location with name="orland" and score=2 to the system.
tracker.get(); // Sorted locations: branford, alps, bradford, orland.
// Return "bradford", as it is the 3<sup>rd</sup> time get() is called.
tracker.add("orlando", 3); // Add location with name="orlando" and score=3 to the system.
tracker.get(); // Sorted locations: branford, orlando, alps, bradford, orland.
// Return "bradford".
tracker.add("alpine", 2); // Add location with name="alpine" and score=2 to the system.
tracker.get(); // Sorted locations: branford, orlando, alpine, alps, bradford, orland.
// Return "bradford".
tracker.get();
// Sorted locations: branford, orlando, alpine, alps, bradford, orland.
// Return "orland".

**Constraints:**

* `name` consists of lowercase English letters, and is unique among all locations.
* `1 <= name.length <= 10`
* <code>1 <= score <= 10<sup>5</sup></code>
* At any time, the number of calls to `get` does not exceed the number of calls to `add`.
* At most <code>4 * 10<sup>4</sup></code> calls **in total** will be made to `add` and `get`.
35 changes: 35 additions & 0 deletions src/main/java/g2101_2200/s2103_rings_and_rods/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package g2101_2200.s2103_rings_and_rods;

// #Easy #String #Hash_Table #2022_05_31_Time_2_ms_(46.84%)_Space_42.2_MB_(29.77%)

import java.util.HashMap;
import java.util.Map;

public class Solution {
public int countPoints(String rings) {
Map<Integer, Integer> redHashMap = new HashMap<>();
Map<Integer, Integer> greenHashMap = new HashMap<>();
Map<Integer, Integer> blueHashMap = new HashMap<>();
for (int i = 0; i <= rings.length() - 2; i = i + 2) {
char charOne = rings.charAt(i);
char charTwo = rings.charAt(i + 1);

if (charOne == 'R') {
redHashMap.put(Character.getNumericValue(charTwo), 123);
} else if (charOne == 'G') {
greenHashMap.put(Character.getNumericValue(charTwo), 123);
} else {
blueHashMap.put(Character.getNumericValue(charTwo), 123);
}
}
int result = 0;
for (int i = 0; i <= 9; i++) {
if (redHashMap.containsKey(i)
&& greenHashMap.containsKey(i)
&& blueHashMap.containsKey(i)) {
result++;
}
}
return result;
}
}
63 changes: 63 additions & 0 deletions src/main/java/g2101_2200/s2103_rings_and_rods/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
2103\. Rings and Rods

Easy

There are `n` rings and each ring is either red, green, or blue. The rings are distributed **across ten rods** labeled from `0` to `9`.

You are given a string `rings` of length `2n` that describes the `n` rings that are placed onto the rods. Every two characters in `rings` forms a **color-position pair** that is used to describe each ring where:

* The **first** character of the <code>i<sup>th</sup></code> pair denotes the <code>i<sup>th</sup></code> ring's **color** (`'R'`, `'G'`, `'B'`).
* The **second** character of the <code>i<sup>th</sup></code> pair denotes the **rod** that the <code>i<sup>th</sup></code> ring is placed on (`'0'` to `'9'`).

For example, `"R3G2B1"` describes `n == 3` rings: a red ring placed onto the rod labeled 3, a green ring placed onto the rod labeled 2, and a blue ring placed onto the rod labeled 1.

Return _the number of rods that have **all three colors** of rings on them._

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/11/23/ex1final.png)

**Input:** rings = "B0B6G0R6R0R6G9"

**Output:** 1

**Explanation:**

- The rod labeled 0 holds 3 rings with all colors: red, green, and blue.

- The rod labeled 6 holds 3 rings, but it only has red and blue.

- The rod labeled 9 holds only a green ring.

Thus, the number of rods with all three colors is 1.

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/11/23/ex2final.png)

**Input:** rings = "B0R0G0R9R0B0G0"

**Output:** 1

**Explanation:**

- The rod labeled 0 holds 6 rings with all colors: red, green, and blue.

- The rod labeled 9 holds only a red ring.

Thus, the number of rods with all three colors is 1.

**Example 3:**

**Input:** rings = "G4"

**Output:** 0

**Explanation:** Only one ring is given. Thus, no rods have all three colors.

**Constraints:**

* `rings.length == 2 * n`
* `1 <= n <= 100`
* `rings[i]` where `i` is **even** is either `'R'`, `'G'`, or `'B'` (**0-indexed**).
* `rings[i]` where `i` is **odd** is a digit from `'0'` to `'9'` (**0-indexed**).
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package g2101_2200.s2104_sum_of_subarray_ranges;

// #Medium #Array #Stack #Monotonic_Stack #2022_05_31_Time_21_ms_(77.85%)_Space_46.4_MB_(23.68%)

import java.util.ArrayDeque;
import java.util.Deque;

public class Solution {
public long subArrayRanges(int[] nums) {
int n = nums.length;
long sum = 0;
Deque<Integer> q = new ArrayDeque<>();

q.add(-1);
for (int i = 0; i <= n; i++) {
while (q.peekLast() != -1 && (i == n || nums[q.peekLast()] <= nums[i])) {
int cur = q.removeLast();
int left = q.peekLast();
int right = i;
sum += 1L * (cur - left) * (right - cur) * nums[cur];
}
q.add(i);
}

q.clear();
q.add(-1);
for (int i = 0; i <= n; i++) {
while (q.peekLast() != -1 && (i == n || nums[q.peekLast()] >= nums[i])) {
int cur = q.removeLast();
int left = q.peekLast();
int right = i;
sum -= 1L * (cur - left) * (right - cur) * nums[cur];
}
q.add(i);
}
return sum;
}
}
Loading