Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package g1601_1700.s1626_best_team_with_no_conflicts;

// #Medium #Array #Dynamic_Programming #Sorting
// #2022_04_18_Time_38_ms_(92.31%)_Space_42.2_MB_(93.12%)

import java.util.Arrays;

public class Solution {
static class Pair {
int score;
int age;

Pair(int score, int age) {
this.score = score;
this.age = age;
}
}

private final int[][] memo = new int[1001][1001];

public int bestTeamScore(int[] scores, int[] ages) {
int n = ages.length;
Pair[] p = new Pair[n];

for (int[] x : memo) {
Arrays.fill(x, -1);
}

for (int i = 0; i < n; i++) {
p[i] = new Pair(scores[i], ages[i]);
}

Arrays.sort(p, (a, b) -> (a.score == b.score) ? (a.age - b.age) : a.score - b.score);
return find(p, 0, 0, n);
}

private int find(Pair[] p, int i, int max, int n) {
if (i >= n) {
return 0;
}

if (memo[i][max] != -1) {
return memo[i][max];
}

if (p[i].age >= max) {
int x1 = p[i].score + find(p, i + 1, p[i].age, n);
int x2 = find(p, i + 1, max, n);
memo[i][max] = Math.max(x1, x2);
return memo[i][max];
} else {
memo[i][max] = find(p, i + 1, max, n);
return memo[i][max];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
1626\. Best Team With No Conflicts

Medium

You are the manager of a basketball team. For the upcoming tournament, you want to choose the team with the highest overall score. The score of the team is the **sum** of scores of all the players in the team.

However, the basketball team is not allowed to have **conflicts**. A **conflict** exists if a younger player has a **strictly higher** score than an older player. A conflict does **not** occur between players of the same age.

Given two lists, `scores` and `ages`, where each `scores[i]` and `ages[i]` represents the score and age of the <code>i<sup>th</sup></code> player, respectively, return _the highest overall score of all possible basketball teams_.

**Example 1:**

**Input:** scores = [1,3,5,10,15], ages = [1,2,3,4,5]

**Output:** 34

**Explanation:** You can choose all the players.

**Example 2:**

**Input:** scores = [4,5,6,5], ages = [2,1,2,1]

**Output:** 16

**Explanation:** It is best to choose the last 3 players. Notice that you are allowed to choose multiple people of the same age.

**Example 3:**

**Input:** scores = [1,2,3,5], ages = [8,9,10,1]

**Output:** 6

**Explanation:** It is best to choose the first 3 players.

**Constraints:**

* `1 <= scores.length, ages.length <= 1000`
* `scores.length == ages.length`
* <code>1 <= scores[i] <= 10<sup>6</sup></code>
* `1 <= ages[i] <= 1000`
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package g1601_1700.s1627_graph_connectivity_with_threshold;

// #Hard #Array #Math #Union_Find #2022_04_18_Time_7_ms_(98.45%)_Space_75.7_MB_(97.93%)

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

public class Solution {
public List<Boolean> areConnected(int n, int threshold, int[][] queries) {
if (n < 1 || queries == null || queries.length == 0) {
return new ArrayList<>();
}

int i;
int j;
int k;
int x;
DisjointSetUnion set = new DisjointSetUnion(n + 1);
int edges = queries.length;

for (i = threshold + 1; i <= n; i++) {
k = n / i;
x = i;
for (j = 2; j <= k; j++) {
x = x + i;
set.union(i, x);
}
}

List<Boolean> result = new ArrayList<>(edges);
for (int[] query : queries) {
result.add(set.find(query[0]) == set.find(query[1]));
}

return result;
}

static class DisjointSetUnion {
private final int[] rank;
private final int[] parent;

public DisjointSetUnion(int n) {
rank = new int[n];
parent = new int[n];

for (int i = 0; i < n; i++) {
this.rank[i] = 1;
this.parent[i] = i;
}
}

public int find(int u) {
int x = u;
while (x != parent[x]) {
x = parent[x];
}

parent[u] = x;
return x;
}

public void union(int u, int v) {
if (u != v) {
int x = find(u);
int y = find(v);
if (x != y) {
if (rank[x] > rank[y]) {
rank[x] += rank[y];
parent[y] = x;
} else {
rank[y] += rank[x];
parent[x] = y;
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
1627\. Graph Connectivity With Threshold

Hard

We have `n` cities labeled from `1` to `n`. Two different cities with labels `x` and `y` are directly connected by a bidirectional road if and only if `x` and `y` share a common divisor **strictly greater** than some `threshold`. More formally, cities with labels `x` and `y` have a road between them if there exists an integer `z` such that all of the following are true:

* `x % z == 0`,
* `y % z == 0`, and
* `z > threshold`.

Given the two integers, `n` and `threshold`, and an array of `queries`, you must determine for each <code>queries[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> if cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> are connected directly or indirectly. (i.e. there is some path between them).

Return _an array_ `answer`_, where_ `answer.length == queries.length` _and_ `answer[i]` _is_ `true` _if for the_ <code>i<sup>th</sup></code> _query, there is a path between_ <code>a<sub>i</sub></code> _and_ <code>b<sub>i</sub></code>_, or_ `answer[i]` _is_ `false` _if there is no path._

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/10/09/ex1.jpg)

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

**Output:** [false,false,true]

**Explanation:** The divisors for each number:

1: 1

2: 1, 2

3: 1, 3

4: 1, 2, 4

5: 1, 5

6: 1, 2, 3, 6

Using the underlined divisors above the threshold, only cities 3 and 6 share a common divisor, so they are the only ones directly connected. The result of each query:

[1,4] 1 is not connected to 4

[2,5] 2 is not connected to 5

[3,6] 3 is connected to 6 through path 3--6

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/10/10/tmp.jpg)

**Input:** n = 6, threshold = 0, queries = [[4,5],[3,4],[3,2],[2,6],[1,3]]

**Output:** [true,true,true,true,true]

**Explanation:** The divisors for each number are the same as the previous example. However, since the threshold is 0, all divisors can be used. Since all numbers share 1 as a divisor, all cities are connected.

**Example 3:**

![](https://assets.leetcode.com/uploads/2020/10/17/ex3.jpg)

**Input:** n = 5, threshold = 1, queries = [[4,5],[4,5],[3,2],[2,3],[3,4]]

**Output:** [false,false,false,false,false]

**Explanation:** Only cities 2 and 4 share a common divisor 2 which is strictly greater than the threshold 1, so they are the only ones directly connected. Please notice that there can be multiple queries for the same pair of nodes [x, y], and that the query [x, y] is equivalent to the query [y, x].

**Constraints:**

* <code>2 <= n <= 10<sup>4</sup></code>
* `0 <= threshold <= n`
* <code>1 <= queries.length <= 10<sup>5</sup></code>
* `queries[i].length == 2`
* <code>1 <= a<sub>i</sub>, b<sub>i</sub> <= cities</code>
* <code>a<sub>i</sub> != b<sub>i</sub></code>
48 changes: 48 additions & 0 deletions src/main/java/g1601_1700/s1629_slowest_key/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package g1601_1700.s1629_slowest_key;

// #Easy #Array #String #2022_04_18_Time_4_ms_(14.60%)_Space_42.9_MB_(72.62%)

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

public class Solution {
public char slowestKey(int[] releaseTimes, String keysPressed) {
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < releaseTimes.length; i++) {
char c = keysPressed.charAt(i);
int duration;
if (i == 0) {
duration = releaseTimes[i];
} else {
duration = releaseTimes[i] - releaseTimes[i - 1];
}
if (!map.containsKey(c)) {
map.put(c, duration);
} else {
int val = map.get(c);
if (duration > val) {
map.put(c, duration);
}
}
}
Map<Integer, List<Character>> map2 = new HashMap<>();
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
int duration = entry.getValue();
if (!map2.containsKey(duration)) {
map2.put(duration, new ArrayList<>());
}
map2.get(duration).add(entry.getKey());
}
int max = -1;
for (Map.Entry<Integer, List<Character>> entry : map2.entrySet()) {
List<Character> chars = entry.getValue();
Collections.sort(chars);
map2.put(entry.getKey(), chars);
max = Math.max(max, entry.getKey());
}
return map2.get(max).get(map2.get(max).size() - 1);
}
}
60 changes: 60 additions & 0 deletions src/main/java/g1601_1700/s1629_slowest_key/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
1629\. Slowest Key

Easy

A newly designed keypad was tested, where a tester pressed a sequence of `n` keys, one at a time.

You are given a string `keysPressed` of length `n`, where `keysPressed[i]` was the <code>i<sup>th</sup></code> key pressed in the testing sequence, and a sorted list `releaseTimes`, where `releaseTimes[i]` was the time the <code>i<sup>th</sup></code> key was released. Both arrays are **0-indexed**. The <code>0<sup>th</sup></code> key was pressed at the time `0`, and every subsequent key was pressed at the **exact** time the previous key was released.

The tester wants to know the key of the keypress that had the **longest duration**. The <code>i<sup>th</sup></code> keypress had a **duration** of `releaseTimes[i] - releaseTimes[i - 1]`, and the <code>0<sup>th</sup></code> keypress had a duration of `releaseTimes[0]`.

Note that the same key could have been pressed multiple times during the test, and these multiple presses of the same key **may not** have had the same **duration**.

_Return the key of the keypress that had the **longest duration**. If there are multiple such keypresses, return the lexicographically largest key of the keypresses._

**Example 1:**

**Input:** releaseTimes = [9,29,49,50], keysPressed = "cbcd"

**Output:** "c"

**Explanation:** The keypresses were as follows:

Keypress for 'c' had a duration of 9 (pressed at time 0 and released at time 9).

Keypress for 'b' had a duration of 29 - 9 = 20 (pressed at time 9 right after the release of the previous character and released at time 29).

Keypress for 'c' had a duration of 49 - 29 = 20 (pressed at time 29 right after the release of the previous character and released at time 49).

Keypress for 'd' had a duration of 50 - 49 = 1 (pressed at time 49 right after the release of the previous character and released at time 50).

The longest of these was the keypress for 'b' and the second keypress for 'c', both with duration 20. 'c' is lexicographically larger than 'b', so the answer is 'c'.

**Example 2:**

**Input:** releaseTimes = [12,23,36,46,62], keysPressed = "spuda"

**Output:** "a"

**Explanation:**

The keypresses were as follows: Keypress for 's' had a duration of 12.

Keypress for 'p' had a duration of 23 - 12 = 11.

Keypress for 'u' had a duration of 36 - 23 = 13.

Keypress for 'd' had a duration of 46 - 36 = 10.

Keypress for 'a' had a duration of 62 - 46 = 16.

The longest of these was the keypress for 'a' with duration 16.

**Constraints:**

* `releaseTimes.length == n`
* `keysPressed.length == n`
* `2 <= n <= 1000`
* <code>1 <= releaseTimes[i] <= 10<sup>9</sup></code>
* `releaseTimes[i] < releaseTimes[i+1]`
* `keysPressed` contains only lowercase English letters.
Loading