Skip to content

Commit f3ac886

Browse files
authored
Added tasks 2432, 2433, 2434.
1 parent 8b52bff commit f3ac886

File tree

9 files changed

+320
-0
lines changed

9 files changed

+320
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g2401_2500.s2432_the_employee_that_worked_on_the_longest_task;
2+
3+
// #Easy #Array #2022_12_07_Time_2_ms_(74.60%)_Space_53.7_MB_(69.30%)
4+
5+
public class Solution {
6+
public int hardestWorker(int[][] logs) {
7+
int result;
8+
int i;
9+
int max;
10+
int tid;
11+
int temp;
12+
max = 0;
13+
tid = Integer.MAX_VALUE;
14+
for (i = 0; i < logs.length; i++) {
15+
temp = logs[i][1];
16+
if (i > 0) {
17+
temp -= logs[i - 1][1];
18+
}
19+
if (temp > max) {
20+
max = temp;
21+
tid = logs[i][0];
22+
} else if (temp == max && tid > logs[i][0]) {
23+
tid = logs[i][0];
24+
}
25+
}
26+
result = tid;
27+
return result;
28+
}
29+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2432\. The Employee That Worked on the Longest Task
2+
3+
Easy
4+
5+
There are `n` employees, each with a unique id from `0` to `n - 1`.
6+
7+
You are given a 2D integer array `logs` where <code>logs[i] = [id<sub>i</sub>, leaveTime<sub>i</sub>]</code> where:
8+
9+
* <code>id<sub>i</sub></code> is the id of the employee that worked on the <code>i<sup>th</sup></code> task, and
10+
* <code>leaveTime<sub>i</sub></code> is the time at which the employee finished the <code>i<sup>th</sup></code> task. All the values <code>leaveTime<sub>i</sub></code> are **unique**.
11+
12+
Note that the <code>i<sup>th</sup></code> task starts the moment right after the <code>(i - 1)<sup>th</sup></code> task ends, and the <code>0<sup>th</sup></code> task starts at time `0`.
13+
14+
Return _the id of the employee that worked the task with the longest time._ If there is a tie between two or more employees, return _the **smallest** id among them_.
15+
16+
**Example 1:**
17+
18+
**Input:** n = 10, logs = [[0,3],[2,5],[0,9],[1,15]]
19+
20+
**Output:** 1
21+
22+
**Explanation:**
23+
24+
Task 0 started at 0 and ended at 3 with 3 units of times.
25+
26+
Task 1 started at 3 and ended at 5 with 2 units of times.
27+
28+
Task 2 started at 5 and ended at 9 with 4 units of times.
29+
30+
Task 3 started at 9 and ended at 15 with 6 units of times.
31+
32+
The task with the longest time is task 3 and the employee with id 1 is the one that worked on it, so we return 1.
33+
34+
**Example 2:**
35+
36+
**Input:** n = 26, logs = [[1,1],[3,7],[2,12],[7,17]]
37+
38+
**Output:** 3
39+
40+
**Explanation:**
41+
42+
Task 0 started at 0 and ended at 1 with 1 unit of times.
43+
44+
Task 1 started at 1 and ended at 7 with 6 units of times.
45+
46+
Task 2 started at 7 and ended at 12 with 5 units of times.
47+
48+
Task 3 started at 12 and ended at 17 with 5 units of times.
49+
50+
The tasks with the longest time is task 1. The employees that worked on it is 3, so we return 3.
51+
52+
**Example 3:**
53+
54+
**Input:** n = 2, logs = [[0,10],[1,20]]
55+
56+
**Output:** 0
57+
58+
**Explanation:**
59+
60+
Task 0 started at 0 and ended at 10 with 10 units of times.
61+
62+
Task 1 started at 10 and ended at 20 with 10 units of times.
63+
64+
The tasks with the longest time are tasks 0 and 1. The employees that worked on them are 0 and 1, so we return the smallest id 0.
65+
66+
**Constraints:**
67+
68+
* `2 <= n <= 500`
69+
* `1 <= logs.length <= 500`
70+
* `logs[i].length == 2`
71+
* <code>0 <= id<sub>i</sub> <= n - 1</code>
72+
* <code>1 <= leaveTime<sub>i</sub> <= 500</code>
73+
* <code>id<sub>i</sub> != id<sub>i+1</sub></code>
74+
* <code>leaveTime<sub>i</sub></code> are sorted in a strictly increasing order.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package g2401_2500.s2433_find_the_original_array_of_prefix_xor;
2+
3+
// #Medium #Array #Bit_Manipulation #2022_12_07_Time_2_ms_(96.00%)_Space_54.8_MB_(93.08%)
4+
5+
public class Solution {
6+
public int[] findArray(int[] pref) {
7+
int[] result = new int[pref.length];
8+
result[0] = pref[0];
9+
for (int i = 1; i < pref.length; i++) {
10+
result[i] = pref[i] ^ pref[i - 1];
11+
}
12+
return result;
13+
}
14+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2433\. Find The Original Array of Prefix Xor
2+
3+
Medium
4+
5+
You are given an **integer** array `pref` of size `n`. Find and return _the array_ `arr` _of size_ `n` _that satisfies_:
6+
7+
* `pref[i] = arr[0] ^ arr[1] ^ ... ^ arr[i]`.
8+
9+
Note that `^` denotes the **bitwise-xor** operation.
10+
11+
It can be proven that the answer is **unique**.
12+
13+
**Example 1:**
14+
15+
**Input:** pref = [5,2,0,3,1]
16+
17+
**Output:** [5,7,2,3,2]
18+
19+
**Explanation:** From the array [5,7,2,3,2] we have the following:
20+
21+
- pref[0] = 5.
22+
23+
- pref[1] = 5 ^ 7 = 2.
24+
25+
- pref[2] = 5 ^ 7 ^ 2 = 0.
26+
27+
- pref[3] = 5 ^ 7 ^ 2 ^ 3 = 3.
28+
29+
- pref[4] = 5 ^ 7 ^ 2 ^ 3 ^ 2 = 1.
30+
31+
**Example 2:**
32+
33+
**Input:** pref = [13]
34+
35+
**Output:** [13]
36+
37+
**Explanation:** We have pref[0] = arr[0] = 13.
38+
39+
**Constraints:**
40+
41+
* <code>1 <= pref.length <= 10<sup>5</sup></code>
42+
* <code>0 <= pref[i] <= 10<sup>6</sup></code>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g2401_2500.s2434_using_a_robot_to_print_the_lexicographically_smallest_string;
2+
3+
// #Medium #String #Hash_Table #Greedy #Stack #2022_12_07_Time_32_ms_(99.61%)_Space_72.8_MB_(76.43%)
4+
5+
public class Solution {
6+
public String robotWithString(String s) {
7+
int n = s.length();
8+
char[] c = s.toCharArray();
9+
char[] next = new char[n + 1];
10+
next[n] = (char) ('z' + 1);
11+
for (int i = n - 1; i >= 0; i--) {
12+
next[i] = (char) ('a' + Math.min(c[i] - 'a', next[i + 1] - 'a'));
13+
}
14+
char[] stack = new char[n];
15+
int j = 0;
16+
int k = 0;
17+
for (int i = 0; i < n; ++i) {
18+
if (c[i] == next[i]) {
19+
c[j++] = c[i];
20+
while (k > 0 && stack[k - 1] <= next[i + 1]) {
21+
c[j++] = stack[--k];
22+
}
23+
} else {
24+
stack[k++] = c[i];
25+
}
26+
}
27+
while (k > 0) {
28+
c[j++] = stack[--k];
29+
}
30+
return new String(c);
31+
}
32+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2434\. Using a Robot to Print the Lexicographically Smallest String
2+
3+
Medium
4+
5+
You are given a string `s` and a robot that currently holds an empty string `t`. Apply one of the following operations until `s` and `t` **are both empty**:
6+
7+
* Remove the **first** character of a string `s` and give it to the robot. The robot will append this character to the string `t`.
8+
* Remove the **last** character of a string `t` and give it to the robot. The robot will write this character on paper.
9+
10+
Return _the lexicographically smallest string that can be written on the paper._
11+
12+
**Example 1:**
13+
14+
**Input:** s = "zza"
15+
16+
**Output:** "azz"
17+
18+
**Explanation:** Let p denote the written string.
19+
20+
Initially p="", s="zza", t="".
21+
22+
Perform first operation three times p="", s="", t="zza".
23+
24+
Perform second operation three times p="azz", s="", t="".
25+
26+
**Example 2:**
27+
28+
**Input:** s = "bac"
29+
30+
**Output:** "abc"
31+
32+
**Explanation:** Let p denote the written string.
33+
34+
Perform first operation twice p="", s="c", t="ba".
35+
36+
Perform second operation twice p="ab", s="c", t="".
37+
38+
Perform first operation p="ab", s="", t="c".
39+
40+
Perform second operation p="abc", s="", t="".
41+
42+
**Example 3:**
43+
44+
**Input:** s = "bdda"
45+
46+
**Output:** "addb"
47+
48+
**Explanation:** Let p denote the written string.
49+
50+
Initially p="", s="bdda", t="".
51+
52+
Perform first operation four times p="", s="", t="bdda".
53+
54+
Perform second operation four times p="addb", s="", t="".
55+
56+
**Constraints:**
57+
58+
* <code>1 <= s.length <= 10<sup>5</sup></code>
59+
* `s` consists of only English lowercase letters.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g2401_2500.s2432_the_employee_that_worked_on_the_longest_task;
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 hardestWorker() {
11+
assertThat(
12+
new Solution().hardestWorker(new int[][] {{0, 3}, {2, 5}, {0, 9}, {1, 15}}),
13+
equalTo(1));
14+
}
15+
16+
@Test
17+
void hardestWorker2() {
18+
assertThat(
19+
new Solution().hardestWorker(new int[][] {{1, 1}, {3, 7}, {2, 12}, {7, 17}}),
20+
equalTo(3));
21+
}
22+
23+
@Test
24+
void hardestWorker3() {
25+
assertThat(new Solution().hardestWorker(new int[][] {{0, 10}, {10, 20}}), equalTo(0));
26+
}
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g2401_2500.s2433_find_the_original_array_of_prefix_xor;
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 findArray() {
11+
assertThat(
12+
new Solution().findArray(new int[] {5, 2, 0, 3, 1}),
13+
equalTo(new int[] {5, 7, 2, 3, 2}));
14+
}
15+
16+
@Test
17+
void findArray2() {
18+
assertThat(new Solution().findArray(new int[] {13}), equalTo(new int[] {13}));
19+
}
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2401_2500.s2434_using_a_robot_to_print_the_lexicographically_smallest_string;
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 robotWithString() {
11+
assertThat(new Solution().robotWithString("zza"), equalTo("azz"));
12+
}
13+
14+
@Test
15+
void robotWithString2() {
16+
assertThat(new Solution().robotWithString("bac"), equalTo("abc"));
17+
}
18+
19+
@Test
20+
void robotWithString3() {
21+
assertThat(new Solution().robotWithString("bdda"), equalTo("addb"));
22+
}
23+
}

0 commit comments

Comments
 (0)