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

Commitf433c16

Browse files
authored
Added tasks 2677-2681
1 parent35c1405 commitf433c16

File tree

15 files changed

+413
-0
lines changed

15 files changed

+413
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2677\. Chunk Array
2+
3+
Easy
4+
5+
Given an array`arr` and a chunk size`size`, return a**chunked** array. A**chunked** array contains the original elements in`arr`, but consists of subarrays each of length`size`. The length of the last subarray may be less than`size` if`arr.length` is not evenly divisible by`size`.
6+
7+
You may assume the array is the output of`JSON.parse`. In other words, it is valid JSON.
8+
9+
Please solve it without using lodash's`_.chunk` function.
10+
11+
**Example 1:**
12+
13+
**Input:** arr =[1,2,3,4,5], size = 1
14+
15+
**Output:**[[1],[2],[3],[4],[5]]
16+
17+
**Explanation:** The arr has been split into subarrays each with 1 element.
18+
19+
**Example 2:**
20+
21+
**Input:** arr =[1,9,6,3,2], size = 3
22+
23+
**Output:**[[1,9,6],[3,2]]
24+
25+
**Explanation:** The arr has been split into subarrays with 3 elements. However, only two elements are left for the 2nd subarray.
26+
27+
**Example 3:**
28+
29+
**Input:** arr =[8,5,3,2,6], size = 6
30+
31+
**Output:**[[8,5,3,2,6]]
32+
33+
**Explanation:** Size is greater than arr.length thus all elements are in the first subarray.
34+
35+
**Example 4:**
36+
37+
**Input:** arr =[], size = 1
38+
39+
**Output:**[]
40+
41+
**Explanation:** There are no elements to be chunked so an empty array is returned.
42+
43+
**Constraints:**
44+
45+
*`arr is a valid JSON array`
46+
* <code>2 <= JSON.stringify(arr).length <= 10<sup>5</sup></code>
47+
*`1 <= size <= arr.length + 1`
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// #Easy #2023_09_11_Time_49_ms_(96.15%)_Space_44.4_MB_(96.63%)
2+
3+
functionchunk(arr:any[],size:number):any[][]{
4+
if(arr.length===0)return[]
5+
if(size>=arr.length)return[arr]
6+
leti:number=0
7+
letres:Array<Array<number>>=[]
8+
while(i<arr.length){
9+
res.push(arr.slice(i,i+size))
10+
i+=size
11+
}
12+
returnres
13+
}
14+
15+
export{chunk}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
packageg2601_2700.s2678_number_of_senior_citizens;
2+
3+
// #Easy #Array #String #2023_09_11_Time_0_ms_(100.00%)_Space_40.7_MB_(97.65%)
4+
5+
publicclassSolution {
6+
publicintcountSeniors(String[]details) {
7+
intcount =0;
8+
for (Stringdetail :details) {
9+
if (((detail.charAt(11) -'0' ==6) && (detail.charAt(12) -'0' >0))
10+
|| (detail.charAt(11) -'0' >6)) {
11+
count++;
12+
}
13+
}
14+
returncount;
15+
}
16+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2678\. Number of Senior Citizens
2+
3+
Easy
4+
5+
You are given a**0-indexed** array of strings`details`. Each element of`details` provides information about a given passenger compressed into a string of length`15`. The system is such that:
6+
7+
* The first ten characters consist of the phone number of passengers.
8+
* The next character denotes the gender of the person.
9+
* The following two characters are used to indicate the age of the person.
10+
* The last two characters determine the seat allotted to that person.
11+
12+
Return_the number of passengers who are**strictly****more than 60 years old**._
13+
14+
**Example 1:**
15+
16+
**Input:** details =["7868190130M7522","5303914400F9211","9273338290F4010"]
17+
18+
**Output:** 2
19+
20+
**Explanation:** The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
21+
22+
**Example 2:**
23+
24+
**Input:** details =["1313579440F2036","2921522980M5644"]
25+
26+
**Output:** 0
27+
28+
**Explanation:** None of the passengers are older than 60.
29+
30+
**Constraints:**
31+
32+
*`1 <= details.length <= 100`
33+
*`details[i].length == 15`
34+
*`details[i] consists of digits from '0' to '9'.`
35+
*`details[i][10] is either 'M' or 'F' or 'O'.`
36+
* The phone numbers and seat numbers of the passengers are distinct.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
packageg2601_2700.s2679_sum_in_a_matrix;
2+
3+
// #Medium #Array #Sorting #Matrix #Heap_Priority_Queue #Simulation
4+
// #2023_09_11_Time_13_ms_(99.66%)_Space_56.9_MB_(31.71%)
5+
6+
importjava.util.Arrays;
7+
8+
publicclassSolution {
9+
publicintmatrixSum(int[][]nums) {
10+
intresult =0;
11+
12+
for (int[]row :nums) {
13+
Arrays.sort(row);
14+
reverseArray(row);
15+
}
16+
17+
for (inti =0;i <nums[0].length;i++) {
18+
intmax =0;
19+
for (int[]num :nums) {
20+
max =Math.max(max,num[i]);
21+
}
22+
result +=max;
23+
}
24+
25+
returnresult;
26+
}
27+
28+
privatevoidreverseArray(int[]arr) {
29+
intleft =0;
30+
intright =arr.length -1;
31+
while (left <right) {
32+
inttemp =arr[left];
33+
arr[left] =arr[right];
34+
arr[right] =temp;
35+
left++;
36+
right--;
37+
}
38+
}
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2679\. Sum in a Matrix
2+
3+
Medium
4+
5+
You are given a**0-indexed** 2D integer array`nums`. Initially, your score is`0`. Perform the following operations until the matrix becomes empty:
6+
7+
1. From each row in the matrix, select the largest number and remove it. In the case of a tie, it does not matter which number is chosen.
8+
2. Identify the highest number amongst all those removed in step 1. Add that number to your**score**.
9+
10+
Return_the final**score**._
11+
12+
**Example 1:**
13+
14+
**Input:** nums =[[7,2,1],[6,4,2],[6,5,3],[3,2,1]]
15+
16+
**Output:** 15
17+
18+
**Explanation:** In the first operation, we remove 7, 6, 6, and 3. We then add 7 to our score. Next, we remove 2, 4, 5, and 2. We add 5 to our score. Lastly, we remove 1, 2, 3, and 1. We add 3 to our score. Thus, our final score is 7 + 5 + 3 = 15.
19+
20+
**Example 2:**
21+
22+
**Input:** nums =[[1]]
23+
24+
**Output:** 1
25+
26+
**Explanation:** We remove 1 and add it to the answer. We return 1.
27+
28+
**Constraints:**
29+
30+
*`1 <= nums.length <= 300`
31+
*`1 <= nums[i].length <= 500`
32+
* <code>0 <= nums[i][j] <= 10<sup>3</sup></code>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
packageg2601_2700.s2680_maximum_or;
2+
3+
// #Medium #Array #Greedy #Bit_Manipulation #Prefix_Sum
4+
// #2023_09_11_Time_2_ms_(100.00%)_Space_55.2_MB_(56.19%)
5+
6+
publicclassSolution {
7+
publiclongmaximumOr(int[]nums,intk) {
8+
int[]suffix =newint[nums.length];
9+
for (inti =nums.length -2;i >=0;i--) {
10+
suffix[i] =suffix[i +1] |nums[i +1];
11+
}
12+
longprefix =0L;
13+
longmax =0L;
14+
for (inti =0;i <=nums.length -1;i++) {
15+
longnum =nums[i];
16+
max =Math.max(max,prefix | (num <<k) |suffix[i]);
17+
prefix |=num;
18+
}
19+
returnmax;
20+
}
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2680\. Maximum OR
2+
3+
Medium
4+
5+
You are given a**0-indexed** integer array`nums` of length`n` and an integer`k`. In an operation, you can choose an element and multiply it by`2`.
6+
7+
Return_the maximum possible value of_`nums[0] | nums[1] | ... | nums[n - 1]`_that can be obtained after applying the operation on nums at most_`k`_times_.
8+
9+
Note that`a | b` denotes the**bitwise or** between two integers`a` and`b`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums =[12,9], k = 1
14+
15+
**Output:** 30
16+
17+
**Explanation:** If we apply the operation to index 1, our new array nums will be equal to[12,18]. Thus, we return the bitwise or of 12 and 18, which is 30.
18+
19+
**Example 2:**
20+
21+
**Input:** nums =[8,1,2], k = 2
22+
23+
**Output:** 35
24+
25+
**Explanation:** If we apply the operation twice on index 0, we yield a new array of[32,1,2]. Thus, we return 32|1|2 = 35.
26+
27+
**Constraints:**
28+
29+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
30+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
31+
*`1 <= k <= 15`
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
packageg2601_2700.s2681_power_of_heroes;
2+
3+
// #Hard #Array #Math #Sorting #Prefix_Sum #2023_09_11_Time_13_ms_(88.24%)_Space_54.4_MB_(55.29%)
4+
5+
importjava.util.Arrays;
6+
7+
publicclassSolution {
8+
publicintsumOfPower(int[]nums) {
9+
Arrays.sort(nums);
10+
longsumOfMin =0L;
11+
longres =0L;
12+
for (intnum :nums) {
13+
longmod =1_000_000_007L;
14+
longmax = (long)num *num %mod;
15+
longmySumOfMin = (sumOfMin +num) %mod;
16+
res = (res +max *mySumOfMin) %mod;
17+
sumOfMin = (sumOfMin +mySumOfMin) %mod;
18+
}
19+
return (int)res;
20+
}
21+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2681\. Power of Heroes
2+
3+
Hard
4+
5+
You are given a**0-indexed** integer array`nums` representing the strength of some heroes. The**power** of a group of heroes is defined as follows:
6+
7+
* Let <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, ... ,<code>i<sub>k</sub></code> be the indices of the heroes in a group. Then, the power of this group is <code>max(nums[i<sub>0</sub>], nums[i<sub>1</sub>], ... ,nums[i<sub>k</sub>])<sup>2</sup> * min(nums[i<sub>0</sub>], nums[i<sub>1</sub>], ... ,nums[i<sub>k</sub>])</code>.
8+
9+
Return_the sum of the**power** of all**non-empty** groups of heroes possible._ Since the sum could be very large, return it**modulo** <code>10<sup>9</sup> + 7</code>.
10+
11+
**Example 1:**
12+
13+
**Input:** nums =[2,1,4]
14+
15+
**Output:** 141
16+
17+
**Explanation:**
18+
19+
1<sup>st</sup> group:[2] has power = 2<sup>2</sup>\* 2 = 8.
20+
21+
2<sup>nd</sup> group:[1] has power = 1<sup>2</sup>\* 1 = 1.
22+
23+
3<sup>rd</sup> group:[4] has power = 4<sup>2</sup>\* 4 = 64.
24+
25+
4<sup>th</sup> group:[2,1] has power = 2<sup>2</sup>\* 1 = 4.
26+
27+
5<sup>th</sup> group:[2,4] has power = 4<sup>2</sup>\* 2 = 32.
28+
29+
6<sup>th</sup> group:[1,4] has power = 4<sup>2</sup>\* 1 = 16.
30+
31+
7<sup>th</sup> group:[2,1,4] has power = 4<sup>2</sup>\* 1 = 16.
32+
33+
The sum of powers of all groups is 8 + 1 + 64 + 4 + 32 + 16 + 16 = 141.
34+
35+
**Example 2:**
36+
37+
**Input:** nums =[1,1,1]
38+
39+
**Output:** 7
40+
41+
**Explanation:** A total of 7 groups are possible, and the power of each group will be 1. Therefore, the sum of the powers of all groups is 7.
42+
43+
**Constraints:**
44+
45+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
46+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// tslint:disable:no-magic-numbers
2+
import{chunk}from'src/main/java/g2601_2700/s2677_chunk_array/solution'
3+
import{expect,test}from'vitest'
4+
5+
test('chunk',()=>{
6+
expect(chunk([1,2,3,4,5],1)).toEqual([[1],[2],[3],[4],[5]])
7+
})
8+
9+
test('chunk2',()=>{
10+
expect(chunk([1,9,6,3,2],3)).toEqual([
11+
[1,9,6],
12+
[3,2],
13+
])
14+
})
15+
16+
test('chunk3',()=>{
17+
expect(chunk([8,5,3,2,6],6)).toEqual([[8,5,3,2,6]])
18+
})
19+
20+
test('chunk4',()=>{
21+
expect(chunk([],1)).toEqual([])
22+
})
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
packageg2601_2700.s2678_number_of_senior_citizens;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importorg.junit.jupiter.api.Test;
7+
8+
classSolutionTest {
9+
@Test
10+
voidcountSeniors() {
11+
assertThat(
12+
newSolution()
13+
.countSeniors(
14+
newString[] {
15+
"7868190130M7522","5303914400F9211","9273338290F4010"
16+
}),
17+
equalTo(2));
18+
}
19+
20+
@Test
21+
voidcountSeniors2() {
22+
assertThat(
23+
newSolution().countSeniors(newString[] {"1313579440F2036","2921522980M5644"}),
24+
equalTo(0));
25+
}
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
packageg2601_2700.s2679_sum_in_a_matrix;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importcom_github_leetcode.CommonUtils;
7+
importorg.junit.jupiter.api.Test;
8+
9+
classSolutionTest {
10+
@Test
11+
voidmatrixSum() {
12+
assertThat(
13+
newSolution()
14+
.matrixSum(
15+
CommonUtils
16+
.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray(
17+
"[7,2,1],[6,4,2],[6,5,3],[3,2,1]")),
18+
equalTo(15));
19+
}
20+
21+
@Test
22+
voidmatrixSum2() {
23+
assertThat(newSolution().matrixSum(newint[][] {{1}}),equalTo(1));
24+
}
25+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp