Skip to content

Commit 959a92b

Browse files
committed
Added tasks 3105-3108
1 parent 855d861 commit 959a92b

File tree

12 files changed

+458
-0
lines changed

12 files changed

+458
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g3101_3200.s3105_longest_strictly_increasing_or_strictly_decreasing_subarray;
2+
3+
// #Easy #Array #2024_04_11_Time_1_ms_(98.13%)_Space_42.7_MB_(57.07%)
4+
5+
public class Solution {
6+
public int longestMonotonicSubarray(int[] nums) {
7+
int inc = 1;
8+
int dec = 1;
9+
int res = 1;
10+
for (int i = 1; i < nums.length; i++) {
11+
if (nums[i] > nums[i - 1]) {
12+
inc += 1;
13+
dec = 1;
14+
} else if (nums[i] < nums[i - 1]) {
15+
dec += 1;
16+
inc = 1;
17+
} else {
18+
inc = 1;
19+
dec = 1;
20+
}
21+
res = Math.max(res, Math.max(inc, dec));
22+
}
23+
return res;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
3105\. Longest Strictly Increasing or Strictly Decreasing Subarray
2+
3+
Easy
4+
5+
You are given an array of integers `nums`. Return _the length of the **longest** subarray of_ `nums` _which is either **strictly increasing** or **strictly decreasing**_.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [1,4,3,3,2]
10+
11+
**Output:** 2
12+
13+
**Explanation:**
14+
15+
The strictly increasing subarrays of `nums` are `[1]`, `[2]`, `[3]`, `[3]`, `[4]`, and `[1,4]`.
16+
17+
The strictly decreasing subarrays of `nums` are `[1]`, `[2]`, `[3]`, `[3]`, `[4]`, `[3,2]`, and `[4,3]`.
18+
19+
Hence, we return `2`.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [3,3,3,3]
24+
25+
**Output:** 1
26+
27+
**Explanation:**
28+
29+
The strictly increasing subarrays of `nums` are `[3]`, `[3]`, `[3]`, and `[3]`.
30+
31+
The strictly decreasing subarrays of `nums` are `[3]`, `[3]`, `[3]`, and `[3]`.
32+
33+
Hence, we return `1`.
34+
35+
**Example 3:**
36+
37+
**Input:** nums = [3,2,1]
38+
39+
**Output:** 3
40+
41+
**Explanation:**
42+
43+
The strictly increasing subarrays of `nums` are `[3]`, `[2]`, and `[1]`.
44+
45+
The strictly decreasing subarrays of `nums` are `[3]`, `[2]`, `[1]`, `[3,2]`, `[2,1]`, and `[3,2,1]`.
46+
47+
Hence, we return `3`.
48+
49+
**Constraints:**
50+
51+
* `1 <= nums.length <= 50`
52+
* `1 <= nums[i] <= 50`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g3101_3200.s3106_lexicographically_smallest_string_after_operations_with_constraint;
2+
3+
// #Medium #String #Greedy #2024_04_11_Time_1_ms_(100.00%)_Space_42.4_MB_(91.10%)
4+
5+
public class Solution {
6+
public String getSmallestString(String s, int k) {
7+
char[] sArray = s.toCharArray();
8+
for (int i = 0; i < sArray.length; i++) {
9+
int distToA = cyclicDistance(sArray[i], 'a');
10+
if (distToA <= k) {
11+
sArray[i] = 'a';
12+
k -= distToA;
13+
} else if (k > 0) {
14+
sArray[i] = (char) (sArray[i] - k);
15+
k = 0;
16+
}
17+
}
18+
return new String(sArray);
19+
}
20+
21+
private int cyclicDistance(char ch1, char ch2) {
22+
int dist = Math.abs(ch1 - ch2);
23+
return Math.min(dist, 26 - dist);
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
3106\. Lexicographically Smallest String After Operations With Constraint
2+
3+
Medium
4+
5+
You are given a string `s` and an integer `k`.
6+
7+
Define a function <code>distance(s<sub>1</sub>, s<sub>2</sub>)</code> between two strings <code>s<sub>1</sub></code> and <code>s<sub>2</sub></code> of the same length `n` as:
8+
9+
* The **sum** of the **minimum distance** between <code>s<sub>1</sub>[i]</code> and <code>s<sub>2</sub>[i]</code> when the characters from `'a'` to `'z'` are placed in a **cyclic** order, for all `i` in the range `[0, n - 1]`.
10+
11+
For example, `distance("ab", "cd") == 4`, and `distance("a", "z") == 1`.
12+
13+
You can **change** any letter of `s` to **any** other lowercase English letter, **any** number of times.
14+
15+
Return a string denoting the **lexicographically smallest** string `t` you can get after some changes, such that `distance(s, t) <= k`.
16+
17+
**Example 1:**
18+
19+
**Input:** s = "zbbz", k = 3
20+
21+
**Output:** "aaaz"
22+
23+
**Explanation:**
24+
25+
Change `s` to `"aaaz"`. The distance between `"zbbz"` and `"aaaz"` is equal to `k = 3`.
26+
27+
**Example 2:**
28+
29+
**Input:** s = "xaxcd", k = 4
30+
31+
**Output:** "aawcd"
32+
33+
**Explanation:**
34+
35+
The distance between "xaxcd" and "aawcd" is equal to k = 4.
36+
37+
**Example 3:**
38+
39+
**Input:** s = "lol", k = 0
40+
41+
**Output:** "lol"
42+
43+
**Explanation:**
44+
45+
It's impossible to change any character as `k = 0`.
46+
47+
**Constraints:**
48+
49+
* `1 <= s.length <= 100`
50+
* `0 <= k <= 2000`
51+
* `s` consists only of lowercase English letters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g3101_3200.s3107_minimum_operations_to_make_median_of_array_equal_to_k;
2+
3+
// #Medium #Array #Sorting #Greedy #2024_04_11_Time_28_ms_(98.49%)_Space_61.8_MB_(98.64%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public long minOperationsToMakeMedianK(int[] nums, int k) {
9+
Arrays.sort(nums);
10+
int n = nums.length;
11+
int medianIndex = n / 2;
12+
long result = 0;
13+
int totalElements = 0;
14+
long totalSum = 0;
15+
int i = medianIndex;
16+
if (nums[medianIndex] > k) {
17+
while (i >= 0 && nums[i] > k) {
18+
totalElements += 1;
19+
totalSum += nums[i];
20+
i -= 1;
21+
}
22+
} else if (nums[medianIndex] < k) {
23+
while (i < n && nums[i] < k) {
24+
totalElements += 1;
25+
totalSum += nums[i];
26+
i += 1;
27+
}
28+
}
29+
result += Math.abs(totalSum - ((long) totalElements * k));
30+
return result;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
3107\. Minimum Operations to Make Median of Array Equal to K
2+
3+
Medium
4+
5+
You are given an integer array `nums` and a **non-negative** integer `k`. In one operation, you can increase or decrease any element by 1.
6+
7+
Return the **minimum** number of operations needed to make the **median** of `nums` _equal_ to `k`.
8+
9+
The median of an array is defined as the middle element of the array when it is sorted in non-decreasing order. If there are two choices for a median, the larger of the two values is taken.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [2,5,6,8,5], k = 4
14+
15+
**Output:** 2
16+
17+
**Explanation:**
18+
19+
We can subtract one from `nums[1]` and `nums[4]` to obtain `[2, 4, 6, 8, 4]`. The median of the resulting array is equal to `k`.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [2,5,6,8,5], k = 7
24+
25+
**Output:** 3
26+
27+
**Explanation:**
28+
29+
We can add one to `nums[1]` twice and add one to `nums[2]` once to obtain `[2, 7, 7, 8, 5]`.
30+
31+
**Example 3:**
32+
33+
**Input:** nums = [1,2,3,4,5,6], k = 4
34+
35+
**Output:** 0
36+
37+
**Explanation:**
38+
39+
The median of the array is already equal to `k`.
40+
41+
**Constraints:**
42+
43+
* <code>1 <= nums.length <= 2 * 10<sup>5</sup></code>
44+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
45+
* <code>1 <= k <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package g3101_3200.s3108_minimum_cost_walk_in_weighted_graph;
2+
3+
// #Hard #Array #Bit_Manipulation #Graph #Union_Find
4+
// #2024_04_11_Time_3_ms_(100.00%)_Space_118.6_MB_(21.36%)
5+
6+
public class Solution {
7+
public int[] minimumCost(int n, int[][] edges, int[][] query) {
8+
int i;
9+
int[] parent = new int[n];
10+
int[] bitwise = new int[n];
11+
int[] size = new int[n];
12+
for (i = 0; i < n; i++) {
13+
parent[i] = i;
14+
size[i] = 1;
15+
bitwise[i] = -1;
16+
}
17+
int len = edges.length;
18+
for (i = 0; i < len; i++) {
19+
int node1 = edges[i][0];
20+
int node2 = edges[i][1];
21+
int weight = edges[i][2];
22+
int parent1 = findParent(node1, parent);
23+
int parent2 = findParent(node2, parent);
24+
if (parent1 == parent2) {
25+
bitwise[parent1] &= weight;
26+
} else {
27+
int bitwiseVal = 0;
28+
boolean check1 = bitwise[parent1] == -1;
29+
boolean check2 = bitwise[parent2] == -1;
30+
if (check1 && check2) {
31+
bitwiseVal = weight;
32+
} else if (check1) {
33+
bitwiseVal = weight & bitwise[parent2];
34+
} else if (check2) {
35+
bitwiseVal = weight & bitwise[parent1];
36+
} else {
37+
bitwiseVal = weight & bitwise[parent1] & bitwise[parent2];
38+
}
39+
if (size[parent1] >= size[parent2]) {
40+
parent[parent2] = parent1;
41+
size[parent1] += size[parent2];
42+
bitwise[parent1] = bitwiseVal;
43+
} else {
44+
parent[parent1] = parent2;
45+
size[parent2] += size[parent1];
46+
bitwise[parent2] = bitwiseVal;
47+
}
48+
}
49+
}
50+
int queryLen = query.length;
51+
int[] result = new int[queryLen];
52+
for (i = 0; i < queryLen; i++) {
53+
int start = query[i][0];
54+
int end = query[i][1];
55+
int parentStart = findParent(start, parent);
56+
int parentEnd = findParent(end, parent);
57+
if (start == end) {
58+
result[i] = 0;
59+
} else if (parentStart == parentEnd) {
60+
result[i] = bitwise[parentStart];
61+
} else {
62+
result[i] = -1;
63+
}
64+
}
65+
return result;
66+
}
67+
68+
private int findParent(int node, int[] parent) {
69+
while (parent[node] != node) {
70+
node = parent[node];
71+
}
72+
return node;
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
3108\. Minimum Cost Walk in Weighted Graph
2+
3+
Hard
4+
5+
There is an undirected weighted graph with `n` vertices labeled from `0` to `n - 1`.
6+
7+
You are given the integer `n` and an array `edges`, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge between vertices <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> with a weight of <code>w<sub>i</sub></code>.
8+
9+
A walk on a graph is a sequence of vertices and edges. The walk starts and ends with a vertex, and each edge connects the vertex that comes before it and the vertex that comes after it. It's important to note that a walk may visit the same edge or vertex more than once.
10+
11+
The **cost** of a walk starting at node `u` and ending at node `v` is defined as the bitwise `AND` of the weights of the edges traversed during the walk. In other words, if the sequence of edge weights encountered during the walk is <code>w<sub>0</sub>, w<sub>1</sub>, w<sub>2</sub>, ..., w<sub>k</sub></code>, then the cost is calculated as <code>w<sub>0</sub> & w<sub>1</sub> & w<sub>2</sub> & ... & w<sub>k</sub></code>, where `&` denotes the bitwise `AND` operator.
12+
13+
You are also given a 2D array `query`, where <code>query[i] = [s<sub>i</sub>, t<sub>i</sub>]</code>. For each query, you need to find the minimum cost of the walk starting at vertex <code>s<sub>i</sub></code> and ending at vertex <code>t<sub>i</sub></code>. If there exists no such walk, the answer is `-1`.
14+
15+
Return _the array_ `answer`_, where_ `answer[i]` _denotes the **minimum** cost of a walk for query_ `i`.
16+
17+
**Example 1:**
18+
19+
**Input:** n = 5, edges = [[0,1,7],[1,3,7],[1,2,1]], query = [[0,3],[3,4]]
20+
21+
**Output:** [1,-1]
22+
23+
**Explanation:**
24+
25+
![](https://assets.leetcode.com/uploads/2024/01/31/q4_example1-1.png)
26+
27+
To achieve the cost of 1 in the first query, we need to move on the following edges: `0->1` (weight 7), `1->2` (weight 1), `2->1` (weight 1), `1->3` (weight 7).
28+
29+
In the second query, there is no walk between nodes 3 and 4, so the answer is -1.
30+
31+
**Example 2:**
32+
33+
**Input:** n = 3, edges = [[0,2,7],[0,1,15],[1,2,6],[1,2,1]], query = [[1,2]]
34+
35+
**Output:** [0]
36+
37+
**Explanation:**
38+
39+
![](https://assets.leetcode.com/uploads/2024/01/31/q4_example2e.png)
40+
41+
To achieve the cost of 0 in the first query, we need to move on the following edges: `1->2` (weight 1), `2->1` (weight 6), `1->2` (weight 1).
42+
43+
**Constraints:**
44+
45+
* <code>2 <= n <= 10<sup>5</sup></code>
46+
* <code>0 <= edges.length <= 10<sup>5</sup></code>
47+
* `edges[i].length == 3`
48+
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code>
49+
* <code>u<sub>i</sub> != v<sub>i</sub></code>
50+
* <code>0 <= w<sub>i</sub> <= 10<sup>5</sup></code>
51+
* <code>1 <= query.length <= 10<sup>5</sup></code>
52+
* `query[i].length == 2`
53+
* <code>0 <= s<sub>i</sub>, t<sub>i</sub> <= n - 1</code>
54+
* <code>s<sub>i</sub> != t<sub>i</sub></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3101_3200.s3105_longest_strictly_increasing_or_strictly_decreasing_subarray;
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 longestMonotonicSubarray() {
11+
assertThat(new Solution().longestMonotonicSubarray(new int[] {1, 4, 3, 3, 2}), equalTo(2));
12+
}
13+
14+
@Test
15+
void longestMonotonicSubarray2() {
16+
assertThat(new Solution().longestMonotonicSubarray(new int[] {3, 3, 3, 3}), equalTo(1));
17+
}
18+
}

0 commit comments

Comments
 (0)