Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc7aaf53

Browse files
authored
Added tasks 2895-2900
1 parent1339de0 commitc7aaf53

File tree

15 files changed

+451
-0
lines changed

15 files changed

+451
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
packageg2801_2900.s2895_minimum_processing_time;
2+
3+
// #Medium #Array #Sorting #Greedy #2023_12_20_Time_23_ms_(99.30%)_Space_59.9_MB_(23.37%)
4+
5+
importjava.util.Arrays;
6+
importjava.util.List;
7+
8+
publicclassSolution {
9+
publicintminProcessingTime(List<Integer>processorTime,List<Integer>tasks) {
10+
int[]proc =newint[processorTime.size()];
11+
for (inti =0,n =processorTime.size();i <n;i++) {
12+
proc[i] =processorTime.get(i);
13+
}
14+
int[]jobs =newint[tasks.size()];
15+
for (inti =0,n =tasks.size();i <n;i++) {
16+
jobs[i] =tasks.get(i);
17+
}
18+
Arrays.sort(proc);
19+
Arrays.sort(jobs);
20+
intmaxTime =0;
21+
for (inti =0,n =proc.length;i <n;i++) {
22+
intprocTime =proc[i] +jobs[jobs.length -1 -i *4];
23+
if (procTime >maxTime) {
24+
maxTime =procTime;
25+
}
26+
}
27+
returnmaxTime;
28+
}
29+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2895\. Minimum Processing Time
2+
3+
Medium
4+
5+
You have`n` processors each having`4` cores and`n * 4` tasks that need to be executed such that each core should perform only**one** task.
6+
7+
Given a**0-indexed** integer array`processorTime` representing the time at which each processor becomes available for the first time and a**0-indexed** integer array`tasks` representing the time it takes to execute each task, return_the**minimum** time when all of the tasks have been executed by the processors._
8+
9+
**Note:** Each core executes the task independently of the others.
10+
11+
**Example 1:**
12+
13+
**Input:** processorTime =[8,10], tasks =[2,2,3,1,8,7,4,5]
14+
15+
**Output:** 16
16+
17+
**Explanation:**
18+
19+
It's optimal to assign the tasks at indexes 4, 5, 6, 7 to the first processor which becomes available at time = 8, and the tasks at indexes 0, 1, 2, 3 to the second processor which becomes available at time = 10.
20+
21+
Time taken by the first processor to finish execution of all tasks = max(8 + 8, 8 + 7, 8 + 4, 8 + 5) = 16.
22+
23+
Time taken by the second processor to finish execution of all tasks = max(10 + 2, 10 + 2, 10 + 3, 10 + 1) = 13.
24+
25+
Hence, it can be shown that the minimum time taken to execute all the tasks is 16.
26+
27+
**Example 2:**
28+
29+
**Input:** processorTime =[10,20], tasks =[2,3,1,2,5,8,4,3]
30+
31+
**Output:** 23
32+
33+
**Explanation:**
34+
35+
It's optimal to assign the tasks at indexes 1, 4, 5, 6 to the first processor which becomes available at time = 10, and the tasks at indexes 0, 2, 3, 7 to the second processor which becomes available at time = 20. Time taken by the first processor to finish execution of all tasks = max(10 + 3, 10 + 5, 10 + 8, 10 + 4) = 18. Time taken by the second processor to finish execution of all tasks = max(20 + 2, 20 + 1, 20 + 2, 20 + 3) = 23. Hence, it can be shown that the minimum time taken to execute all the tasks is 23.
36+
37+
**Constraints:**
38+
39+
*`1 <= n == processorTime.length <= 25000`
40+
* <code>1 <= tasks.length <= 10<sup>5</sup></code>
41+
* <code>0 <= processorTime[i] <= 10<sup>9</sup></code>
42+
* <code>1 <= tasks[i] <= 10<sup>9</sup></code>
43+
*`tasks.length == 4 * n`
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
packageg2801_2900.s2896_apply_operations_to_make_two_strings_equal;
2+
3+
// #Medium #String #Dynamic_Programming #2023_12_20_Time_1_ms_(100.00%)_Space_43_MB_(50.87%)
4+
5+
importjava.util.ArrayList;
6+
7+
publicclassSolution {
8+
publicintminOperations(Strings1,Strings2,intx) {
9+
intn =s1.length();
10+
ArrayList<Integer>diffs =newArrayList<>();
11+
for (inti =0;i <n;i++) {
12+
if (s1.charAt(i) !=s2.charAt(i)) {
13+
diffs.add(i);
14+
}
15+
}
16+
intm =diffs.size();
17+
if ((m &1) ==1) {
18+
return -1;
19+
}elseif (m ==0) {
20+
return0;
21+
}
22+
int[]dp =newint[m];
23+
dp[0] =0;
24+
dp[1] =Math.min(x,diffs.get(1) -diffs.get(0));
25+
for (inti =2;i <m;i++) {
26+
if ((i &1) ==1) {
27+
dp[i] =Math.min(dp[i -1] +x,dp[i -2] +diffs.get(i) -diffs.get(i -1));
28+
}else {
29+
dp[i] =Math.min(dp[i -1],dp[i -2] +diffs.get(i) -diffs.get(i -1));
30+
}
31+
}
32+
returndp[m -1];
33+
}
34+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2896\.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
packageg2801_2900.s2897_apply_operations_on_array_to_maximize_sum_of_squares;
2+
3+
// #Hard #Array #Hash_Table #Greedy #Bit_Manipulation
4+
// #2023_12_20_Time_29_ms_(98.00%)_Space_63.8_MB_(14.00%)
5+
6+
importjava.util.List;
7+
8+
publicclassSolution {
9+
publicintmaxSum(List<Integer>nums,intk) {
10+
int[]bits =newint[32];
11+
for (intn :nums) {
12+
for (inti =0;i <32; ++i) {
13+
bits[i] += (n >>i) &1;
14+
}
15+
}
16+
intmod =1_000_000_007;
17+
longsum =0;
18+
for (inti =0;i <k; ++i) {
19+
longn =0;
20+
for (intj =0;j <32; ++j) {
21+
if (bits[j] >i) {
22+
n |=1 <<j;
23+
}
24+
}
25+
sum = (sum +n *n %mod) %mod;
26+
}
27+
return (int)sum;
28+
}
29+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2897\. Apply Operations on Array to Maximize Sum of Squares
2+
3+
Hard
4+
5+
You are given a**0-indexed** integer array`nums` and a**positive** integer`k`.
6+
7+
You can do the following operation on the array**any** number of times:
8+
9+
* Choose any two distinct indices`i` and`j` and**simultaneously** update the values of`nums[i]` to`(nums[i] AND nums[j])` and`nums[j]` to`(nums[i] OR nums[j])`. Here,`OR` denotes the bitwise`OR` operation, and`AND` denotes the bitwise`AND` operation.
10+
11+
You have to choose`k` elements from the final array and calculate the sum of their**squares**.
12+
13+
Return_the**maximum** sum of squares you can achieve_.
14+
15+
Since the answer can be very large, return it**modulo** <code>10<sup>9</sup> + 7</code>.
16+
17+
**Example 1:**
18+
19+
**Input:** nums =[2,6,5,8], k = 2
20+
21+
**Output:** 261
22+
23+
**Explanation:** We can do the following operations on the array:
24+
25+
- Choose i = 0 and j = 3, then change nums[0] to (2 AND 8) = 0 and nums[3] to (2 OR 8) = 10. The resulting array is nums =[0,6,5,10].
26+
27+
- Choose i = 2 and j = 3, then change nums[2] to (5 AND 10) = 0 and nums[3] to (5 OR 10) = 15. The resulting array is nums =[0,6,0,15].
28+
29+
We can choose the elements 15 and 6 from the final array. The sum of squares is 15<sup>2</sup> + 6<sup>2</sup> = 261.
30+
31+
It can be shown that this is the maximum value we can get.
32+
33+
**Example 2:**
34+
35+
**Input:** nums =[4,5,4,7], k = 3
36+
37+
**Output:** 90
38+
39+
**Explanation:** We do not need to apply any operations.
40+
41+
We can choose the elements 7, 5, and 4 with a sum of squares: 7<sup>2</sup> + 5<sup>2</sup> + 4<sup>2</sup> = 90.
42+
43+
It can be shown that this is the maximum value we can get.
44+
45+
**Constraints:**
46+
47+
* <code>1 <= k <= nums.length <= 10<sup>5</sup></code>
48+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
packageg2801_2900.s2899_last_visited_integers;
2+
3+
// #Easy #Array #String #Simulation #2023_12_20_Time_2_ms_(96.41%)_Space_44.5_MB_(5.24%)
4+
5+
importjava.util.ArrayList;
6+
importjava.util.List;
7+
8+
publicclassSolution {
9+
publicList<Integer>lastVisitedIntegers(List<String>words) {
10+
List<String>prevEle =newArrayList<>();
11+
List<Integer>res =newArrayList<>();
12+
intcount =0;
13+
for (inti =0;i <words.size();i++) {
14+
if (!words.get(i).equals("prev")) {
15+
count =0;
16+
prevEle.add(words.get(i));
17+
continue;
18+
}
19+
if (count >=prevEle.size()) {
20+
res.add(-1);
21+
}else {
22+
res.add(Integer.parseInt(prevEle.get(prevEle.size() -count -1)));
23+
}
24+
count++;
25+
}
26+
returnres;
27+
}
28+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2899\. Last Visited Integers
2+
3+
Easy
4+
5+
Given a**0-indexed** array of strings`words` where`words[i]` is either a positive integer represented as a string or the string`"prev"`.
6+
7+
Start iterating from the beginning of the array; for every`"prev"` string seen in`words`, find the**last visited integer** in`words` which is defined as follows:
8+
9+
* Let`k` be the number of consecutive`"prev"` strings seen so far (containing the current string). Let`nums` be the**0-indexed** array of**integers** seen so far and`nums_reverse` be the reverse of`nums`, then the integer at <code>(k - 1)<sup>th</sup></code> index of`nums_reverse` will be the**last visited integer** for this`"prev"`.
10+
* If`k` is**greater** than the total visited integers, then the last visited integer will be`-1`.
11+
12+
Return_an integer array containing the last visited integers._
13+
14+
**Example 1:**
15+
16+
**Input:** words =["1","2","prev","prev","prev"]
17+
18+
**Output:**[2,1,-1]
19+
20+
**Explanation:**
21+
22+
For "prev" at index = 2, last visited integer will be 2 as here the number of consecutive "prev" strings is 1, and in the array reverse\_nums, 2 will be the first element.
23+
24+
For "prev" at index = 3, last visited integer will be 1 as there are a total of two consecutive "prev" strings including this "prev" which are visited, and 1 is the second last visited integer.
25+
26+
For "prev" at index = 4, last visited integer will be -1 as there are a total of three consecutive "prev" strings including this "prev" which are visited, but the total number of integers visited is two.
27+
28+
**Example 2:**
29+
30+
**Input:** words =["1","prev","2","prev","prev"]
31+
32+
**Output:**[1,2,1]
33+
34+
**Explanation:**
35+
36+
For "prev" at index = 1, last visited integer will be 1.
37+
38+
For "prev" at index = 3, last visited integer will be 2.
39+
40+
For "prev" at index = 4, last visited integer will be 1 as there are a total of two consecutive "prev" strings including this "prev" which are visited, and 1 is the second last visited integer.
41+
42+
**Constraints:**
43+
44+
*`1 <= words.length <= 100`
45+
*`words[i] == "prev"` or`1 <= int(words[i]) <= 100`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
packageg2801_2900.s2900_longest_unequal_adjacent_groups_subsequence_i;
2+
3+
// #Medium #Array #String #Dynamic_Programming #Greedy
4+
// #2023_12_20_Time_1_ms_(100.00%)_Space_45_MB_(7.16%)
5+
6+
importjava.util.ArrayList;
7+
importjava.util.List;
8+
9+
@SuppressWarnings("java:S1172")
10+
publicclassSolution {
11+
publicList<String>getWordsInLongestSubsequence(intn,String[]words,int[]groups) {
12+
List<String>ans =newArrayList<>();
13+
ans.add(words[0]);
14+
intprev =groups[0];
15+
for (inti =1;i <groups.length;i++) {
16+
if (prev !=groups[i]) {
17+
ans.add(words[i]);
18+
prev =groups[i];
19+
}
20+
}
21+
returnans;
22+
}
23+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2900\. Longest Unequal Adjacent Groups Subsequence I
2+
3+
Medium
4+
5+
You are given an integer`n`, a**0-indexed** string array`words`, and a**0-indexed****binary** array`groups`, both arrays having length`n`.
6+
7+
You need to select the**longest****subsequence** from an array of indices`[0, 1, ..., n - 1]`, such that for the subsequence denoted as <code>[i<sub>0</sub>, i<sub>1</sub>, ..., i<sub>k - 1</sub>]</code> having length`k`, <code>groups[i<sub>j</sub>] != groups[i<sub>j + 1</sub>]</code>, for each`j` where`0 < j + 1 < k`.
8+
9+
Return_a string array containing the words corresponding to the indices**(in order)** in the selected subsequence_. If there are multiple answers, return_any of them_.
10+
11+
A**subsequence** of an array is a new array that is formed from the original array by deleting some (possibly none) of the elements without disturbing the relative positions of the remaining elements.
12+
13+
**Note:** strings in`words` may be**unequal** in length.
14+
15+
**Example 1:**
16+
17+
**Input:** n = 3, words =["e","a","b"], groups =[0,0,1]
18+
19+
**Output:**["e","b"]
20+
21+
**Explanation:** A subsequence that can be selected is[0,2] because groups[0] != groups[2].
22+
23+
So, a valid answer is[words[0],words[2]] =["e","b"].
24+
25+
Another subsequence that can be selected is[1,2] because groups[1] != groups[2].
26+
27+
This results in[words[1],words[2]] =["a","b"].
28+
29+
It is also a valid answer.
30+
31+
It can be shown that the length of the longest subsequence of indices that satisfies the condition is 2.
32+
33+
**Example 2:**
34+
35+
**Input:** n = 4, words =["a","b","c","d"], groups =[1,0,1,1]
36+
37+
**Output:**["a","b","c"]
38+
39+
**Explanation:** A subsequence that can be selected is[0,1,2] because groups[0] != groups[1] and groups[1] != groups[2].
40+
41+
So, a valid answer is[words[0],words[1],words[2]] =["a","b","c"].
42+
43+
Another subsequence that can be selected is[0,1,3] because groups[0] != groups[1] and groups[1] != groups[3].
44+
45+
This results in[words[0],words[1],words[3]] =["a","b","d"].
46+
47+
It is also a valid answer.
48+
49+
It can be shown that the length of the longest subsequence of indices that satisfies the condition is 3.
50+
51+
**Constraints:**
52+
53+
*`1 <= n == words.length == groups.length <= 100`
54+
*`1 <= words[i].length <= 10`
55+
*`0 <= groups[i] < 2`
56+
*`words` consists of**distinct** strings.
57+
*`words[i]` consists of lowercase English letters.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
packageg2801_2900.s2895_minimum_processing_time;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importjava.util.Arrays;
7+
importorg.junit.jupiter.api.Test;
8+
9+
classSolutionTest {
10+
@Test
11+
voidminProcessingTime() {
12+
assertThat(
13+
newSolution()
14+
.minProcessingTime(
15+
Arrays.asList(8,10),Arrays.asList(2,2,3,1,8,7,4,5)),
16+
equalTo(16));
17+
}
18+
19+
@Test
20+
voidminProcessingTime2() {
21+
assertThat(
22+
newSolution()
23+
.minProcessingTime(
24+
Arrays.asList(10,20),Arrays.asList(2,3,1,2,5,8,4,3)),
25+
equalTo(23));
26+
}
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
packageg2801_2900.s2896_apply_operations_to_make_two_strings_equal;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importorg.junit.jupiter.api.Test;
7+
8+
classSolutionTest {
9+
@Test
10+
voidminOperations() {
11+
assertThat(newSolution().minOperations("1100011000","0101001010",2),equalTo(4));
12+
}
13+
14+
@Test
15+
voidminOperations2() {
16+
assertThat(newSolution().minOperations("10110","00011",4),equalTo(-1));
17+
}
18+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp