Skip to content

Commit ead1c3d

Browse files
authored
Added tasks 1766, 1769, 1770, 1771, 1773.
1 parent 3319272 commit ead1c3d

File tree

15 files changed

+527
-0
lines changed

15 files changed

+527
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package g1701_1800.s1766_tree_of_coprimes;
2+
3+
// #Hard #Math #Depth_First_Search #Breadth_First_Search #Tree
4+
// #2022_04_30_Time_111_ms_(94.07%)_Space_155.4_MB_(40.68%)
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
9+
public class Solution {
10+
private void dfs(
11+
int[] v2n,
12+
int[] v2d,
13+
int depth,
14+
int parent,
15+
int node,
16+
int[] ans,
17+
int[] nums,
18+
ArrayList<Integer>[] neighbors) {
19+
20+
int d = Integer.MIN_VALUE;
21+
int n = -1;
22+
23+
int v = nums[node];
24+
for (int i = 1; i <= 50; i++) {
25+
if (v2n[i] != -1 && v2d[i] > d && gcd(i, v) == 1) {
26+
d = v2d[i];
27+
n = v2n[i];
28+
}
29+
}
30+
ans[node] = n;
31+
32+
int v2NOld = v2n[v];
33+
int v2DOld = v2d[v];
34+
35+
v2n[v] = node;
36+
v2d[v] = depth;
37+
for (int child : neighbors[node]) {
38+
if (child == parent) {
39+
continue;
40+
}
41+
dfs(v2n, v2d, depth + 1, node, child, ans, nums, neighbors);
42+
}
43+
v2n[v] = v2NOld;
44+
v2d[v] = v2DOld;
45+
}
46+
47+
private int gcd(int x, int y) {
48+
return x == 0 ? y : gcd(y % x, x);
49+
}
50+
51+
public int[] getCoprimes(int[] nums, int[][] edges) {
52+
53+
int n = nums.length;
54+
55+
ArrayList<Integer>[] neighbors = new ArrayList[n];
56+
for (int i = 0; i < n; i++) {
57+
neighbors[i] = new ArrayList<>();
58+
}
59+
for (int[] edge : edges) {
60+
neighbors[edge[0]].add(edge[1]);
61+
neighbors[edge[1]].add(edge[0]);
62+
}
63+
64+
int[] ans = new int[n];
65+
int[] v2n = new int[51];
66+
int[] v2d = new int[51];
67+
Arrays.fill(v2n, -1);
68+
dfs(v2n, v2d, 0, -1, 0, ans, nums, neighbors);
69+
return ans;
70+
}
71+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
1766\. Tree of Coprimes
2+
3+
Hard
4+
5+
There is a tree (i.e., a connected, undirected graph that has no cycles) consisting of `n` nodes numbered from `0` to `n - 1` and exactly `n - 1` edges. Each node has a value associated with it, and the **root** of the tree is node `0`.
6+
7+
To represent this tree, you are given an integer array `nums` and a 2D array `edges`. Each `nums[i]` represents the <code>i<sup>th</sup></code> node's value, and each <code>edges[j] = [u<sub>j</sub>, v<sub>j</sub>]</code> represents an edge between nodes <code>u<sub>j</sub></code> and <code>v<sub>j</sub></code> in the tree.
8+
9+
Two values `x` and `y` are **coprime** if `gcd(x, y) == 1` where `gcd(x, y)` is the **greatest common divisor** of `x` and `y`.
10+
11+
An ancestor of a node `i` is any other node on the shortest path from node `i` to the **root**. A node is **not** considered an ancestor of itself.
12+
13+
Return _an array_ `ans` _of size_ `n`, _where_ `ans[i]` _is the closest ancestor to node_ `i` _such that_ `nums[i]` _and_ `nums[ans[i]]` are **coprime**, or `-1` _if there is no such ancestor_.
14+
15+
**Example 1:**
16+
17+
**![](https://assets.leetcode.com/uploads/2021/01/06/untitled-diagram.png)**
18+
19+
**Input:** nums = [2,3,3,2], edges = [[0,1],[1,2],[1,3]]
20+
21+
**Output:** [-1,0,0,1]
22+
23+
**Explanation:** In the above figure, each node's value is in parentheses.
24+
25+
- Node 0 has no coprime ancestors.
26+
27+
- Node 1 has only one ancestor, node 0. Their values are coprime (gcd(2,3) == 1). - Node 2 has two ancestors, nodes 1 and 0. Node 1's value is not coprime (gcd(3,3) == 3), but node 0's value is (gcd(2,3) == 1), so node 0 is the closest valid ancestor.
28+
29+
- Node 3 has two ancestors, nodes 1 and 0. It is coprime with node 1 (gcd(3,2) == 1), so node 1 is its closest valid ancestor.
30+
31+
**Example 2:**
32+
33+
![](https://assets.leetcode.com/uploads/2021/01/06/untitled-diagram1.png)
34+
35+
**Input:** nums = [5,6,10,2,3,6,15], edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]]
36+
37+
**Output:** [-1,0,-1,0,0,0,-1]
38+
39+
**Constraints:**
40+
41+
* `nums.length == n`
42+
* `1 <= nums[i] <= 50`
43+
* <code>1 <= n <= 10<sup>5</sup></code>
44+
* `edges.length == n - 1`
45+
* `edges[j].length == 2`
46+
* <code>0 <= u<sub>j</sub>, v<sub>j</sub> < n</code>
47+
* <code>u<sub>j</sub> != v<sub>j</sub></code>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package g1701_1800.s1769_minimum_number_of_operations_to_move_all_balls_to_each_box;
2+
3+
// #Medium #Array #String #2022_04_30_Time_3_ms_(91.66%)_Space_47.8_MB_(35.95%)
4+
5+
public class Solution {
6+
public int[] minOperations(String boxes) {
7+
8+
int countFromLeft = 0;
9+
int countFromRight = 0;
10+
int moves = 0;
11+
int[] result = new int[boxes.length()];
12+
13+
for (char c : boxes.toCharArray()) {
14+
moves += countFromLeft;
15+
if (c == '1') {
16+
countFromLeft++;
17+
}
18+
}
19+
20+
for (int i = boxes.length() - 1; i >= 0; i--) {
21+
char c = boxes.charAt(i);
22+
result[i] = moves;
23+
24+
if (c == '1') {
25+
countFromLeft--;
26+
countFromRight++;
27+
}
28+
moves -= countFromLeft;
29+
moves += countFromRight;
30+
}
31+
32+
return result;
33+
}
34+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
1769\. Minimum Number of Operations to Move All Balls to Each Box
2+
3+
Medium
4+
5+
You have `n` boxes. You are given a binary string `boxes` of length `n`, where `boxes[i]` is `'0'` if the <code>i<sup>th</sup></code> box is **empty**, and `'1'` if it contains **one** ball.
6+
7+
In one operation, you can move **one** ball from a box to an adjacent box. Box `i` is adjacent to box `j` if `abs(i - j) == 1`. Note that after doing so, there may be more than one ball in some boxes.
8+
9+
Return an array `answer` of size `n`, where `answer[i]` is the **minimum** number of operations needed to move all the balls to the <code>i<sup>th</sup></code> box.
10+
11+
Each `answer[i]` is calculated considering the **initial** state of the boxes.
12+
13+
**Example 1:**
14+
15+
**Input:** boxes = "110"
16+
17+
**Output:** [1,1,3]
18+
19+
**Explanation:** The answer for each box is as follows:
20+
21+
1) First box: you will have to move one ball from the second box to the first box in one operation.
22+
23+
2) Second box: you will have to move one ball from the first box to the second box in one operation.
24+
25+
3) Third box: you will have to move one ball from the first box to the third box in two operations, and move one ball from the second box to the third box in one operation.
26+
27+
**Example 2:**
28+
29+
**Input:** boxes = "001011"
30+
31+
**Output:** [11,8,5,4,3,4]
32+
33+
**Constraints:**
34+
35+
* `n == boxes.length`
36+
* `1 <= n <= 2000`
37+
* `boxes[i]` is either `'0'` or `'1'`.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package g1701_1800.s1770_maximum_score_from_performing_multiplication_operations;
2+
3+
// #Medium #Array #Dynamic_Programming #2022_04_30_Time_31_ms_(92.41%)_Space_53.2_MB_(88.74%)
4+
5+
public class Solution {
6+
7+
public int maximumScore(int[] nums, int[] mult) {
8+
int n = nums.length;
9+
int m = mult.length;
10+
int row = m;
11+
int[] dp = new int[m];
12+
int[] prev = new int[m + 1];
13+
14+
while (--row >= 0) {
15+
for (int i = 0; i <= row; ++i) {
16+
dp[i] =
17+
Math.max(
18+
prev[i] + mult[row] * nums[n - row + i - 1],
19+
prev[i + 1] + mult[row] * nums[i]);
20+
}
21+
prev = dp;
22+
}
23+
24+
return dp[0];
25+
}
26+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
1770\. Maximum Score from Performing Multiplication Operations
2+
3+
Medium
4+
5+
You are given two integer arrays `nums` and `multipliers` of size `n` and `m` respectively, where `n >= m`. The arrays are **1-indexed**.
6+
7+
You begin with a score of `0`. You want to perform **exactly** `m` operations. On the <code>i<sup>th</sup></code> operation **(1-indexed)**, you will:
8+
9+
* Choose one integer `x` from **either the start or the end** of the array `nums`.
10+
* Add `multipliers[i] * x` to your score.
11+
* Remove `x` from the array `nums`.
12+
13+
Return _the **maximum** score after performing_ `m` _operations._
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [1,2,3], multipliers = [3,2,1]
18+
19+
**Output:** 14
20+
21+
**Explanation:** An optimal solution is as follows:
22+
23+
- Choose from the end, [1,2,**3**], adding 3 \* 3 = 9 to the score.
24+
25+
- Choose from the end, [1,**2**], adding 2 \* 2 = 4 to the score.
26+
27+
- Choose from the end, [**1**], adding 1 \* 1 = 1 to the score.
28+
29+
The total score is 9 + 4 + 1 = 14.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [-5,-3,-3,-2,7,1], multipliers = [-10,-5,3,4,6]
34+
35+
**Output:** 102
36+
37+
**Explanation:** An optimal solution is as follows:
38+
39+
- Choose from the start, [**\-5**,-3,-3,-2,7,1], adding -5 \* -10 = 50 to the score.
40+
41+
- Choose from the start, [**\-3**,-3,-2,7,1], adding -3 \* -5 = 15 to the score.
42+
43+
- Choose from the start, [**\-3**,-2,7,1], adding -3 \* 3 = -9 to the score.
44+
45+
- Choose from the end, [-2,7,**1**], adding 1 \* 4 = 4 to the score.
46+
47+
- Choose from the end, [-2,**7**], adding 7 \* 6 = 42 to the score.
48+
49+
The total score is 50 + 15 - 9 + 4 + 42 = 102.
50+
51+
**Constraints:**
52+
53+
* `n == nums.length`
54+
* `m == multipliers.length`
55+
* <code>1 <= m <= 10<sup>3</sup></code>
56+
* <code>m <= n <= 10<sup>5</sup></code>
57+
* `-1000 <= nums[i], multipliers[i] <= 1000`
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g1701_1800.s1771_maximize_palindrome_length_from_subsequences;
2+
3+
// #Hard #String #Dynamic_Programming #2022_04_30_Time_58_ms_(87.88%)_Space_67.1_MB_(92.42%)
4+
5+
public class Solution {
6+
public int longestPalindrome(String word1, String word2) {
7+
int len1 = word1.length();
8+
int len2 = word2.length();
9+
int len = len1 + len2;
10+
String word = word1 + word2;
11+
int[][] dp = new int[len][len];
12+
int max = 0;
13+
char[] arr = word.toCharArray();
14+
for (int d = 1; d <= len; d++) {
15+
for (int i = 0; i + d - 1 < len; i++) {
16+
if (arr[i] == arr[i + d - 1]) {
17+
dp[i][i + d - 1] =
18+
d == 1 ? 1 : Math.max(dp[i + 1][i + d - 2] + 2, dp[i][i + d - 1]);
19+
if (i < len1 && i + d - 1 >= len1) {
20+
max = Math.max(max, dp[i][i + d - 1]);
21+
}
22+
} else {
23+
dp[i][i + d - 1] = Math.max(dp[i + 1][i + d - 1], dp[i][i + d - 2]);
24+
}
25+
}
26+
}
27+
return max;
28+
}
29+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
1771\. Maximize Palindrome Length From Subsequences
2+
3+
Hard
4+
5+
You are given two strings, `word1` and `word2`. You want to construct a string in the following manner:
6+
7+
* Choose some **non-empty** subsequence `subsequence1` from `word1`.
8+
* Choose some **non-empty** subsequence `subsequence2` from `word2`.
9+
* Concatenate the subsequences: `subsequence1 + subsequence2`, to make the string.
10+
11+
Return _the **length** of the longest **palindrome** that can be constructed in the described manner._ If no palindromes can be constructed, return `0`.
12+
13+
A **subsequence** of a string `s` is a string that can be made by deleting some (possibly none) characters from `s` without changing the order of the remaining characters.
14+
15+
A **palindrome** is a string that reads the same forward as well as backward.
16+
17+
**Example 1:**
18+
19+
**Input:** word1 = "cacb", word2 = "cbba"
20+
21+
**Output:** 5
22+
23+
**Explanation:** Choose "ab" from word1 and "cba" from word2 to make "abcba", which is a palindrome.
24+
25+
**Example 2:**
26+
27+
**Input:** word1 = "ab", word2 = "ab"
28+
29+
**Output:** 3
30+
31+
**Explanation:** Choose "ab" from word1 and "a" from word2 to make "aba", which is a palindrome.
32+
33+
**Example 3:**
34+
35+
**Input:** word1 = "aa", word2 = "bb"
36+
37+
**Output:** 0
38+
39+
**Explanation:** You cannot construct a palindrome from the described method, so return 0.
40+
41+
**Constraints:**
42+
43+
* `1 <= word1.length, word2.length <= 1000`
44+
* `word1` and `word2` consist of lowercase English letters.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g1701_1800.s1773_count_items_matching_a_rule;
2+
3+
// #Easy #Array #String #2022_04_30_Time_7_ms_(25.91%)_Space_56.9_MB_(35.49%)
4+
5+
import java.util.List;
6+
7+
public class Solution {
8+
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
9+
int match = 0;
10+
for (List<String> item : items) {
11+
if ((ruleKey.equals("type") && item.get(0).equals(ruleValue))
12+
|| (ruleKey.equals("color") && item.get(1).equals(ruleValue))
13+
|| (ruleKey.equals("name") && item.get(2).equals(ruleValue))) {
14+
match++;
15+
}
16+
}
17+
return match;
18+
}
19+
}

0 commit comments

Comments
 (0)