Skip to content

Commit e9cfda2

Browse files
authored
Added tasks 2444, 2446, 2447, 2448, 2449.
1 parent c530cec commit e9cfda2

File tree

15 files changed

+547
-0
lines changed

15 files changed

+547
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g2401_2500.s2444_count_subarrays_with_fixed_bounds;
2+
3+
// #Hard #Array #Sliding_Window #Queue #Monotonic_Queue
4+
// #2022_12_14_Time_9_ms_(83.94%)_Space_56.5_MB_(97.01%)
5+
6+
public class Solution {
7+
public long countSubarrays(int[] nums, int minK, int maxK) {
8+
long ans = 0;
9+
int i = 0;
10+
while ( i < nums.length) {
11+
if (nums[i] >= minK && nums[i] <= maxK) {
12+
int a = i;
13+
int b = i;
14+
int mini = 0;
15+
int maxi = 0;
16+
while (i != nums.length && (nums[i] >= minK && nums[i] <= maxK)) {
17+
i++;
18+
}
19+
while (true) {
20+
for (; b != i && (mini == 0 || maxi == 0); b++) {
21+
if (nums[b] == minK) {
22+
mini++;
23+
}
24+
if (nums[b] == maxK) {
25+
maxi++;
26+
}
27+
}
28+
29+
if (mini == 0 || maxi == 0) {
30+
break;
31+
}
32+
33+
for (; mini != 0 && maxi != 0; ans += 1 + (i - b), a++) {
34+
if (nums[a] == minK) {
35+
mini--;
36+
}
37+
if (nums[a] == maxK) {
38+
maxi--;
39+
}
40+
}
41+
}
42+
}
43+
i++;
44+
}
45+
46+
return ans;
47+
}
48+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2444\. Count Subarrays With Fixed Bounds
2+
3+
Hard
4+
5+
You are given an integer array `nums` and two integers `minK` and `maxK`.
6+
7+
A **fixed-bound subarray** of `nums` is a subarray that satisfies the following conditions:
8+
9+
* The **minimum** value in the subarray is equal to `minK`.
10+
* The **maximum** value in the subarray is equal to `maxK`.
11+
12+
Return _the **number** of fixed-bound subarrays_.
13+
14+
A **subarray** is a **contiguous** part of an array.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [1,3,5,2,7,5], minK = 1, maxK = 5
19+
20+
**Output:** 2
21+
22+
**Explanation:** The fixed-bound subarrays are [1,3,5] and [1,3,5,2].
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [1,1,1,1], minK = 1, maxK = 1
27+
28+
**Output:** 10
29+
30+
**Explanation:** Every subarray of nums is a fixed-bound subarray. There are 10 possible subarrays.
31+
32+
**Constraints:**
33+
34+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
35+
* <code>1 <= nums[i], minK, maxK <= 10<sup>6</sup></code>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g2401_2500.s2446_determine_if_two_events_have_conflict;
2+
3+
// #Easy #Array #String #2022_12_14_Time_0_ms_(100.00%)_Space_40_MB_(87.07%)
4+
5+
public class Solution {
6+
public boolean haveConflict(String[] event1, String[] event2) {
7+
int aStart = getTimeSerial(event1[0]);
8+
int aEnd = getTimeSerial(event1[1]);
9+
int bStart = getTimeSerial(event2[0]);
10+
int bEnd = getTimeSerial(event2[1]);
11+
return (bStart >= aStart && bStart <= aEnd) || (bStart <= aStart && bEnd >= aStart);
12+
}
13+
14+
private int getTimeSerial(String timestamp) {
15+
int hours = 0;
16+
int minutes = 0;
17+
boolean isHours = true;
18+
for (int i = 0; i < timestamp.length(); i += 1) {
19+
if (timestamp.charAt(i) == ':') {
20+
isHours = false;
21+
} else if (isHours) {
22+
hours = hours * 10 + Character.getNumericValue(timestamp.charAt(i));
23+
} else {
24+
minutes = minutes * 10 + Character.getNumericValue(timestamp.charAt(i));
25+
}
26+
}
27+
return 60 * hours + minutes;
28+
}
29+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2446\. Determine if Two Events Have Conflict
2+
3+
Easy
4+
5+
You are given two arrays of strings that represent two inclusive events that happened **on the same day**, `event1` and `event2`, where:
6+
7+
* <code>event1 = [startTime<sub>1</sub>, endTime<sub>1</sub>]</code> and
8+
* <code>event2 = [startTime<sub>2</sub>, endTime<sub>2</sub>]</code>.
9+
10+
Event times are valid 24 hours format in the form of `HH:MM`.
11+
12+
A **conflict** happens when two events have some non-empty intersection (i.e., some moment is common to both events).
13+
14+
Return `true` _if there is a conflict between two events. Otherwise, return_ `false`.
15+
16+
**Example 1:**
17+
18+
**Input:** event1 = ["01:15","02:00"], event2 = ["02:00","03:00"]
19+
20+
**Output:** true
21+
22+
**Explanation:** The two events intersect at time 2:00.
23+
24+
**Example 2:**
25+
26+
**Input:** event1 = ["01:00","02:00"], event2 = ["01:20","03:00"]
27+
28+
**Output:** true
29+
30+
**Explanation:** The two events intersect starting from 01:20 to 02:00.
31+
32+
**Example 3:**
33+
34+
**Input:** event1 = ["10:00","11:00"], event2 = ["14:00","15:00"]
35+
36+
**Output:** false
37+
38+
**Explanation:** The two events do not intersect.
39+
40+
**Constraints:**
41+
42+
* `evnet1.length == event2.length == 2.`
43+
* `event1[i].length == event2[i].length == 5`
44+
* <code>startTime<sub>1</sub> <= endTime<sub>1</sub></code>
45+
* <code>startTime<sub>2</sub> <= endTime<sub>2</sub></code>
46+
* All the event times follow the `HH:MM` format.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package g2401_2500.s2447_number_of_subarrays_with_gcd_equal_to_k;
2+
3+
// #Medium #Array #Math #Number_Theory #2022_12_14_Time_7_ms_(96.60%)_Space_41.9_MB_(82.89%)
4+
5+
public class Solution {
6+
private int sol(int a, int b) {
7+
if (b == 0) {
8+
return a;
9+
}
10+
return sol(b, a % b);
11+
}
12+
13+
public int subarrayGCD(int[] nums, int k) {
14+
int n = nums.length;
15+
int cnt = 0;
16+
for (int i = 0; i < n; i++) {
17+
int gcd = 0;
18+
for (int j = i; j < n; j++) {
19+
gcd = sol(gcd, nums[j]);
20+
if (gcd == k) {
21+
cnt++;
22+
}
23+
if (gcd < k) {
24+
break;
25+
}
26+
}
27+
}
28+
return cnt;
29+
}
30+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2447\. Number of Subarrays With GCD Equal to K
2+
3+
Medium
4+
5+
Given an integer array `nums` and an integer `k`, return _the number of **subarrays** of_ `nums` _where the greatest common divisor of the subarray's elements is_ `k`.
6+
7+
A **subarray** is a contiguous non-empty sequence of elements within an array.
8+
9+
The **greatest common divisor of an array** is the largest integer that evenly divides all the array elements.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [9,3,1,2,6,3], k = 3
14+
15+
**Output:** 4
16+
17+
**Explanation:** The subarrays of nums where 3 is the greatest common divisor of all the subarray's elements are:
18+
19+
- [9,<ins>**3**</ins>,1,2,6,3]
20+
21+
- [9,3,1,2,6,<ins>**3**</ins>]
22+
23+
- [<ins>**9,3**</ins>,1,2,6,3]
24+
25+
- [9,3,1,2,<ins>**6,3**</ins>]
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [4], k = 7
30+
31+
**Output:** 0
32+
33+
**Explanation:** There are no subarrays of nums where 7 is the greatest common divisor of all the subarray's elements.
34+
35+
**Constraints:**
36+
37+
* `1 <= nums.length <= 1000`
38+
* <code>1 <= nums[i], k <= 10<sup>9</sup></code>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g2401_2500.s2448_minimum_cost_to_make_array_equal;
2+
3+
// #Hard #Array #Sorting #Binary_Search #Prefix_Sum
4+
// #2022_12_14_Time_19_ms_(92.19%)_Space_56_MB_(90.89%)
5+
6+
import java.util.ArrayList;
7+
import java.util.Comparator;
8+
import java.util.List;
9+
10+
public class Solution {
11+
private static class Pair {
12+
int e;
13+
int c;
14+
15+
Pair(int e, int c) {
16+
this.e = e;
17+
this.c = c;
18+
}
19+
}
20+
21+
public long minCost(int[] nums, int[] cost) {
22+
long sum = 0;
23+
List<Pair> al = new ArrayList<>();
24+
for (int i = 0; i < nums.length; i++) {
25+
al.add(new Pair(nums[i], cost[i]));
26+
sum += cost[i];
27+
}
28+
al.sort(Comparator.comparingInt(a -> a.e));
29+
long ans = 0;
30+
long mid = (sum + 1) / 2;
31+
long s2 = 0;
32+
int t = 0;
33+
for (int i = 0; i < al.size() && s2 < mid; i++) {
34+
s2 += al.get(i).c;
35+
t = al.get(i).e;
36+
}
37+
for (int i = 0; i < al.size(); i++) {
38+
ans += Math.abs((long) nums[i] - (long) t) * cost[i];
39+
}
40+
return ans;
41+
}
42+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2448\. Minimum Cost to Make Array Equal
2+
3+
Hard
4+
5+
You are given two **0-indexed** arrays `nums` and `cost` consisting each of `n` **positive** integers.
6+
7+
You can do the following operation **any** number of times:
8+
9+
* Increase or decrease **any** element of the array `nums` by `1`.
10+
11+
The cost of doing one operation on the <code>i<sup>th</sup></code> element is `cost[i]`.
12+
13+
Return _the **minimum** total cost such that all the elements of the array_ `nums` _become **equal**_.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [1,3,5,2], cost = [2,3,1,14]
18+
19+
**Output:** 8
20+
21+
**Explanation:** We can make all the elements equal to 2 in the following way:
22+
23+
- Increase the 0<sup>th</sup> element one time. The cost is 2.
24+
25+
- Decrease the 1<sup>st</sup> element one time. The cost is 3.
26+
27+
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
28+
29+
- The total cost is 2 + 3 + 3 = 8. It can be shown that we cannot make the array equal with a smaller cost.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [2,2,2,2,2], cost = [4,2,8,1,3]
34+
35+
**Output:** 0
36+
37+
**Explanation:** All the elements are already equal, so no operations are needed.
38+
39+
**Constraints:**
40+
41+
* `n == nums.length == cost.length`
42+
* <code>1 <= n <= 10<sup>5</sup></code>
43+
* <code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package g2401_2500.s2449_minimum_number_of_operations_to_make_arrays_similar;
2+
3+
// #Hard #Array #Sorting #Greedy #2022_12_14_Time_57_ms_(87.86%)_Space_59.7_MB_(93.46%)
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
8+
public class Solution {
9+
public long makeSimilar(int[] nums, int[] target) {
10+
ArrayList<Integer> evenNums = new ArrayList<>();
11+
ArrayList<Integer> oddNums = new ArrayList<>();
12+
ArrayList<Integer> evenTar = new ArrayList<>();
13+
ArrayList<Integer> oddTar = new ArrayList<>();
14+
Arrays.sort(nums);
15+
Arrays.sort(target);
16+
17+
for (int i = 0; i < nums.length; i++) {
18+
if (nums[i] % 2 == 0) {
19+
evenNums.add(nums[i]);
20+
} else {
21+
oddNums.add(nums[i]);
22+
}
23+
24+
if (target[i] % 2 == 0) {
25+
evenTar.add(target[i]);
26+
} else {
27+
oddTar.add(target[i]);
28+
}
29+
}
30+
31+
long countPositiveIteration = 0;
32+
long countNegativeIteration = 0;
33+
34+
for (int i = 0; i < evenNums.size(); i++) {
35+
int num = evenNums.get(i);
36+
int tar = evenTar.get(i);
37+
long diff = (long) num - tar;
38+
long iteration = diff / 2;
39+
if (diff > 0) {
40+
countNegativeIteration += iteration;
41+
} else if (diff < 0) {
42+
countPositiveIteration += Math.abs(iteration);
43+
}
44+
}
45+
46+
for (int i = 0; i < oddNums.size(); i++) {
47+
int num = oddNums.get(i);
48+
int tar = oddTar.get(i);
49+
long diff = (long) num - tar;
50+
long iteration = diff / 2;
51+
if (diff > 0) {
52+
countNegativeIteration += iteration;
53+
} else if (diff < 0) {
54+
countPositiveIteration += Math.abs(iteration);
55+
}
56+
}
57+
58+
long totalDifference = countPositiveIteration - countNegativeIteration;
59+
60+
return totalDifference == 0
61+
? countPositiveIteration
62+
: countPositiveIteration + Math.abs(totalDifference);
63+
}
64+
}

0 commit comments

Comments
 (0)