Skip to content

Commit feead0c

Browse files
authored
Added tasks 1834, 1835, 1837, 1838, 1839.
1 parent 11a5c2c commit feead0c

File tree

15 files changed

+477
-0
lines changed

15 files changed

+477
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package g1801_1900.s1834_single_threaded_cpu;
2+
3+
// #Medium #Array #Sorting #Heap_Priority_Queue
4+
// #2022_05_07_Time_134_ms_(83.22%)_Space_100.6_MB_(75.23%)
5+
6+
import java.util.Arrays;
7+
import java.util.Comparator;
8+
import java.util.PriorityQueue;
9+
10+
public class Solution {
11+
public int[] getOrder(int[][] tasks1) {
12+
int n = tasks1.length;
13+
14+
int[][] tasks = new int[n][3];
15+
16+
for (int i = 0; i < n; i++) {
17+
tasks[i] = new int[] {tasks1[i][0], tasks1[i][1], i};
18+
}
19+
20+
Arrays.sort(tasks, Comparator.comparingInt(a -> a[0]));
21+
PriorityQueue<int[]> minHeap =
22+
new PriorityQueue<>(
23+
(a, b) -> {
24+
if (a[1] == b[1]) {
25+
return a[2] - b[2];
26+
} else {
27+
return a[1] - b[1];
28+
}
29+
});
30+
31+
int time = tasks[0][0];
32+
33+
int[] taskOrderResult = new int[n];
34+
35+
int i = 0;
36+
int index = 0;
37+
while (!minHeap.isEmpty() || i < n) {
38+
while (i < n && time >= tasks[i][0]) {
39+
minHeap.add(tasks[i++]);
40+
}
41+
42+
if (!minHeap.isEmpty()) {
43+
int[] task = minHeap.remove();
44+
taskOrderResult[index++] = task[2];
45+
time += task[1];
46+
} else {
47+
time = tasks[i][0];
48+
}
49+
}
50+
51+
return taskOrderResult;
52+
}
53+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
1834\. Single-Threaded CPU
2+
3+
Medium
4+
5+
You are given `n` tasks labeled from `0` to `n - 1` represented by a 2D integer array `tasks`, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>th</sup></code> task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code> to finish processing.
6+
7+
You have a single-threaded CPU that can process **at most one** task at a time and will act in the following way:
8+
9+
* If the CPU is idle and there are no available tasks to process, the CPU remains idle.
10+
* If the CPU is idle and there are available tasks, the CPU will choose the one with the **shortest processing time**. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.
11+
* Once a task is started, the CPU will **process the entire task** without stopping.
12+
* The CPU can finish a task then start a new one instantly.
13+
14+
Return _the order in which the CPU will process the tasks._
15+
16+
**Example 1:**
17+
18+
**Input:** tasks = [[1,2],[2,4],[3,2],[4,1]]
19+
20+
**Output:** [0,2,3,1]
21+
22+
**Explanation:** The events go as follows:
23+
24+
- At time = 1, task 0 is available to process. Available tasks = {0}.
25+
26+
- Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}.
27+
28+
- At time = 2, task 1 is available to process. Available tasks = {1}.
29+
30+
- At time = 3, task 2 is available to process. Available tasks = {1, 2}.
31+
32+
- Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}.
33+
34+
- At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}.
35+
36+
- At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}.
37+
38+
- At time = 10, the CPU finishes task 1 and becomes idle.
39+
40+
**Example 2:**
41+
42+
**Input:** tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]]
43+
44+
**Output:** [4,3,2,0,1]
45+
46+
**Explanation:****:** The events go as follows:
47+
48+
- At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}.
49+
50+
- Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}.
51+
52+
- At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}.
53+
54+
- At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}.
55+
56+
- At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}.
57+
58+
- At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}.
59+
60+
- At time = 40, the CPU finishes task 1 and becomes idle.
61+
62+
**Constraints:**
63+
64+
* `tasks.length == n`
65+
* <code>1 <= n <= 10<sup>5</sup></code>
66+
* <code>1 <= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> <= 10<sup>9</sup></code>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g1801_1900.s1835_find_xor_sum_of_all_pairs_bitwise_and;
2+
3+
// #Hard #Array #Math #Bit_Manipulation #2022_05_07_Time_1_ms_(100.00%)_Space_57.9_MB_(83.33%)
4+
5+
public class Solution {
6+
public int getXORSum(int[] arr1, int[] arr2) {
7+
int xor1 = 0;
8+
int xor2 = 0;
9+
for (int i : arr1) {
10+
xor1 ^= i;
11+
}
12+
for (int j : arr2) {
13+
xor2 ^= j;
14+
}
15+
return xor1 & xor2;
16+
}
17+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
1835\. Find XOR Sum of All Pairs Bitwise AND
2+
3+
Hard
4+
5+
The **XOR sum** of a list is the bitwise `XOR` of all its elements. If the list only contains one element, then its **XOR sum** will be equal to this element.
6+
7+
* For example, the **XOR sum** of `[1,2,3,4]` is equal to `1 XOR 2 XOR 3 XOR 4 = 4`, and the **XOR sum** of `[3]` is equal to `3`.
8+
9+
You are given two **0-indexed** arrays `arr1` and `arr2` that consist only of non-negative integers.
10+
11+
Consider the list containing the result of `arr1[i] AND arr2[j]` (bitwise `AND`) for every `(i, j)` pair where `0 <= i < arr1.length` and `0 <= j < arr2.length`.
12+
13+
Return _the **XOR sum** of the aforementioned list_.
14+
15+
**Example 1:**
16+
17+
**Input:** arr1 = [1,2,3], arr2 = [6,5]
18+
19+
**Output:** 0
20+
21+
**Explanation:** The list = [1 AND 6, 1 AND 5, 2 AND 6, 2 AND 5, 3 AND 6, 3 AND 5] = [0,1,2,0,2,1]. The XOR sum = 0 XOR 1 XOR 2 XOR 0 XOR 2 XOR 1 = 0.
22+
23+
**Example 2:**
24+
25+
**Input:** arr1 = [12], arr2 = [4]
26+
27+
**Output:** 4
28+
29+
**Explanation:** The list = [12 AND 4] = [4]. The XOR sum = 4.
30+
31+
**Constraints:**
32+
33+
* <code>1 <= arr1.length, arr2.length <= 10<sup>5</sup></code>
34+
* <code>0 <= arr1[i], arr2[j] <= 10<sup>9</sup></code>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package g1801_1900.s1837_sum_of_digits_in_base_k;
2+
3+
// #Easy #Math #2022_05_07_Time_1_ms_(10.42%)_Space_38.9_MB_(91.55%)
4+
5+
public class Solution {
6+
public int sumBase(int n, int k) {
7+
String str = Integer.toString(Integer.parseInt(n + "", 10), k);
8+
int sum = 0;
9+
for (char c : str.toCharArray()) {
10+
sum += Character.getNumericValue(c);
11+
}
12+
return sum;
13+
}
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
1837\. Sum of Digits in Base K
2+
3+
Easy
4+
5+
Given an integer `n` (in base `10`) and a base `k`, return _the **sum** of the digits of_ `n` _**after** converting_ `n` _from base_ `10` _to base_ `k`.
6+
7+
After converting, each digit should be interpreted as a base `10` number, and the sum should be returned in base `10`.
8+
9+
**Example 1:**
10+
11+
**Input:** n = 34, k = 6
12+
13+
**Output:** 9
14+
15+
**Explanation:** 34 (base 10) expressed in base 6 is 54. 5 + 4 = 9.
16+
17+
**Example 2:**
18+
19+
**Input:** n = 10, k = 10
20+
21+
**Output:** 1
22+
23+
**Explanation:** n is already in base 10. 1 + 0 = 1.
24+
25+
**Constraints:**
26+
27+
* `1 <= n <= 100`
28+
* `2 <= k <= 10`
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g1801_1900.s1838_frequency_of_the_most_frequent_element;
2+
3+
// #Medium #Array #Sorting #Greedy #Binary_Search #Prefix_Sum #Sliding_Window
4+
// #Binary_Search_II_Day_9 #2022_05_07_Time_11_ms_(100.00%)_Space_57_MB_(97.86%)
5+
6+
public class Solution {
7+
public int maxFrequency(int[] nums, int k) {
8+
countingSort(nums);
9+
int start = 0;
10+
int preSum = 0;
11+
int total = 1;
12+
for (int i = 0; i < nums.length; i++) {
13+
int length = i - start + 1;
14+
int product = nums[i] * length;
15+
preSum += nums[i];
16+
while (product - preSum > k) {
17+
preSum -= nums[start++];
18+
length--;
19+
product = nums[i] * length;
20+
}
21+
total = Math.max(total, length);
22+
}
23+
24+
return total;
25+
}
26+
27+
private void countingSort(int[] nums) {
28+
int max = Integer.MIN_VALUE;
29+
for (int num : nums) {
30+
max = Math.max(max, num);
31+
}
32+
int[] map = new int[max + 1];
33+
for (int num : nums) {
34+
map[num]++;
35+
}
36+
int i = 0;
37+
int j = 0;
38+
while (i <= max) {
39+
if (map[i]-- > 0) {
40+
nums[j++] = i;
41+
} else {
42+
i++;
43+
}
44+
}
45+
}
46+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
1838\. Frequency of the Most Frequent Element
2+
3+
Medium
4+
5+
The **frequency** of an element is the number of times it occurs in an array.
6+
7+
You are given an integer array `nums` and an integer `k`. In one operation, you can choose an index of `nums` and increment the element at that index by `1`.
8+
9+
Return _the **maximum possible frequency** of an element after performing **at most**_ `k` _operations_.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,4], k = 5
14+
15+
**Output:** 3
16+
17+
**Explanation:** Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [1,4,8,13], k = 5
22+
23+
**Output:** 2
24+
25+
**Explanation:** There are multiple optimal solutions:
26+
27+
- Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2.
28+
29+
- Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2.
30+
31+
- Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2.
32+
33+
**Example 3:**
34+
35+
**Input:** nums = [3,9,6], k = 2
36+
37+
**Output:** 1
38+
39+
**Constraints:**
40+
41+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
42+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
43+
* <code>1 <= k <= 10<sup>5</sup></code>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g1801_1900.s1839_longest_substring_of_all_vowels_in_order;
2+
3+
// #Medium #String #Sliding_Window #2022_05_07_Time_24_ms_(86.13%)_Space_58.7_MB_(49.58%)
4+
5+
public class Solution {
6+
public int longestBeautifulSubstring(String word) {
7+
int cnt = 1;
8+
int len = 1;
9+
int maxLen = 0;
10+
for (int i = 1; i != word.length(); ++i) {
11+
if (word.charAt(i - 1) == word.charAt(i)) {
12+
++len;
13+
} else if (word.charAt(i - 1) < word.charAt(i)) {
14+
++len;
15+
++cnt;
16+
} else {
17+
cnt = 1;
18+
len = 1;
19+
}
20+
21+
if (cnt == 5) {
22+
maxLen = Math.max(maxLen, len);
23+
}
24+
}
25+
return maxLen;
26+
}
27+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
1839\. Longest Substring Of All Vowels in Order
2+
3+
Medium
4+
5+
A string is considered **beautiful** if it satisfies the following conditions:
6+
7+
* Each of the 5 English vowels (`'a'`, `'e'`, `'i'`, `'o'`, `'u'`) must appear **at least once** in it.
8+
* The letters must be sorted in **alphabetical order** (i.e. all `'a'`s before `'e'`s, all `'e'`s before `'i'`s, etc.).
9+
10+
For example, strings `"aeiou"` and `"aaaaaaeiiiioou"` are considered **beautiful**, but `"uaeio"`, `"aeoiu"`, and `"aaaeeeooo"` are **not beautiful**.
11+
12+
Given a string `word` consisting of English vowels, return _the **length of the longest beautiful substring** of_ `word`_. If no such substring exists, return_ `0`.
13+
14+
A **substring** is a contiguous sequence of characters in a string.
15+
16+
**Example 1:**
17+
18+
**Input:** word = "aeiaaioaaaaeiiiiouuuooaauuaeiu"
19+
20+
**Output:** 13
21+
22+
**Explanation:** The longest beautiful substring in word is "aaaaeiiiiouuu" of length 13.
23+
24+
**Example 2:**
25+
26+
**Input:** word = "aeeeiiiioooauuuaeiou"
27+
28+
**Output:** 5
29+
30+
**Explanation:** The longest beautiful substring in word is "aeiou" of length 5.
31+
32+
**Example 3:**
33+
34+
**Input:** word = "a"
35+
36+
**Output:** 0
37+
38+
**Explanation:** There is no beautiful substring, so return 0.
39+
40+
**Constraints:**
41+
42+
* <code>1 <= word.length <= 5 * 10<sup>5</sup></code>
43+
* `word` consists of characters `'a'`, `'e'`, `'i'`, `'o'`, and `'u'`.

0 commit comments

Comments
 (0)