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

Commit2150e4f

Browse files
authored
Added tasks 3349-3352
1 parent0315871 commit2150e4f

File tree

12 files changed

+376
-0
lines changed

12 files changed

+376
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
packageg3301_3400.s3349_adjacent_increasing_subarrays_detection_i;
2+
3+
// #Easy #Array #2024_11_15_Time_1_ms_(100.00%)_Space_44.7_MB_(18.69%)
4+
5+
importjava.util.List;
6+
7+
publicclassSolution {
8+
publicbooleanhasIncreasingSubarrays(List<Integer>nums,intk) {
9+
intl =nums.size();
10+
if (l <k *2) {
11+
returnfalse;
12+
}
13+
for (inti =0;i <l -2 *k +1;i++) {
14+
if (check(i,k,nums) &&check(i +k,k,nums)) {
15+
returntrue;
16+
}
17+
}
18+
returnfalse;
19+
}
20+
21+
privatebooleancheck(intp,intk,List<Integer>nums) {
22+
for (inti =p;i <p +k -1;i++) {
23+
if (nums.get(i) >=nums.get(i +1)) {
24+
returnfalse;
25+
}
26+
}
27+
returntrue;
28+
}
29+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
3349\. Adjacent Increasing Subarrays Detection I
2+
3+
Easy
4+
5+
Given an array`nums` of`n` integers and an integer`k`, determine whether there exist**two****adjacent** subarrays of length`k` such that both subarrays are**strictly****increasing**. Specifically, check if there are**two** subarrays starting at indices`a` and`b` (`a < b`), where:
6+
7+
* Both subarrays`nums[a..a + k - 1]` and`nums[b..b + k - 1]` are**strictly increasing**.
8+
* The subarrays must be**adjacent**, meaning`b = a + k`.
9+
10+
Return`true` if it is_possible_ to find**two** such subarrays, and`false` otherwise.
11+
12+
**Example 1:**
13+
14+
**Input:** nums =[2,5,7,8,9,2,3,4,3,1], k = 3
15+
16+
**Output:** true
17+
18+
**Explanation:**
19+
20+
* The subarray starting at index`2` is`[7, 8, 9]`, which is strictly increasing.
21+
* The subarray starting at index`5` is`[2, 3, 4]`, which is also strictly increasing.
22+
* These two subarrays are adjacent, so the result is`true`.
23+
24+
**Example 2:**
25+
26+
**Input:** nums =[1,2,3,4,4,4,4,5,6,7], k = 5
27+
28+
**Output:** false
29+
30+
**Constraints:**
31+
32+
*`2 <= nums.length <= 100`
33+
*`1 < 2 * k <= nums.length`
34+
*`-1000 <= nums[i] <= 1000`
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
packageg3301_3400.s3350_adjacent_increasing_subarrays_detection_ii;
2+
3+
// #Medium #Array #Binary_Search #2024_11_15_Time_10_ms_(99.76%)_Space_90.6_MB_(30.61%)
4+
5+
importjava.util.List;
6+
7+
publicclassSolution {
8+
publicintmaxIncreasingSubarrays(List<Integer>nums) {
9+
intn =nums.size();
10+
int[]a =newint[n];
11+
for (inti =0;i <n; ++i) {
12+
a[i] =nums.get(i);
13+
}
14+
intans =1;
15+
intpreviousLen =Integer.MAX_VALUE;
16+
inti =0;
17+
while (i <n) {
18+
intj =i +1;
19+
while (j <n &&a[j -1] <a[j]) {
20+
++j;
21+
}
22+
intlen =j -i;
23+
ans =Math.max(ans,len /2);
24+
if (previousLen !=Integer.MAX_VALUE) {
25+
ans =Math.max(ans,Math.min(previousLen,len));
26+
}
27+
previousLen =len;
28+
i =j;
29+
}
30+
returnans;
31+
}
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
3350\. Adjacent Increasing Subarrays Detection II
2+
3+
Medium
4+
5+
Given an array`nums` of`n` integers, your task is to find the**maximum** value of`k` for which there exist**two** adjacent subarrays of length`k` each, such that both subarrays are**strictly****increasing**. Specifically, check if there are**two** subarrays of length`k` starting at indices`a` and`b` (`a < b`), where:
6+
7+
* Both subarrays`nums[a..a + k - 1]` and`nums[b..b + k - 1]` are**strictly increasing**.
8+
* The subarrays must be**adjacent**, meaning`b = a + k`.
9+
10+
Return the**maximum**_possible_ value of`k`.
11+
12+
A**subarray** is a contiguous**non-empty** sequence of elements within an array.
13+
14+
**Example 1:**
15+
16+
**Input:** nums =[2,5,7,8,9,2,3,4,3,1]
17+
18+
**Output:** 3
19+
20+
**Explanation:**
21+
22+
* The subarray starting at index 2 is`[7, 8, 9]`, which is strictly increasing.
23+
* The subarray starting at index 5 is`[2, 3, 4]`, which is also strictly increasing.
24+
* These two subarrays are adjacent, and 3 is the**maximum** possible value of`k` for which two such adjacent strictly increasing subarrays exist.
25+
26+
**Example 2:**
27+
28+
**Input:** nums =[1,2,3,4,4,4,4,5,6,7]
29+
30+
**Output:** 2
31+
32+
**Explanation:**
33+
34+
* The subarray starting at index 0 is`[1, 2]`, which is strictly increasing.
35+
* The subarray starting at index 2 is`[3, 4]`, which is also strictly increasing.
36+
* These two subarrays are adjacent, and 2 is the**maximum** possible value of`k` for which two such adjacent strictly increasing subarrays exist.
37+
38+
**Constraints:**
39+
40+
* <code>2 <= nums.length <= 2 * 10<sup>5</sup></code>
41+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
packageg3301_3400.s3351_sum_of_good_subsequences;
2+
3+
// #Hard #Array #Hash_Table #Dynamic_Programming
4+
// #2024_11_15_Time_13_ms_(99.09%)_Space_55.8_MB_(68.79%)
5+
6+
publicclassSolution {
7+
publicintsumOfGoodSubsequences(int[]nums) {
8+
intmax =0;
9+
for (intx :nums) {
10+
max =Math.max(x,max);
11+
}
12+
long[]count =newlong[max +3];
13+
long[]total =newlong[max +3];
14+
longmod = (int) (1e9 +7);
15+
longres =0;
16+
for (inta :nums) {
17+
count[a +1] = (count[a] +count[a +1] +count[a +2] +1) %mod;
18+
longcur =total[a] +total[a +2] +a * (count[a] +count[a +2] +1);
19+
total[a +1] = (total[a +1] +cur) %mod;
20+
res = (res +cur) %mod;
21+
}
22+
return (int)res;
23+
}
24+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
3351\. Sum of Good Subsequences
2+
3+
Hard
4+
5+
You are given an integer array`nums`. A**good** subsequence is defined as a subsequence of`nums` where the absolute difference between any**two** consecutive elements in the subsequence is**exactly** 1.
6+
7+
Return the**sum** of all_possible_**good subsequences** of`nums`.
8+
9+
Since the answer may be very large, return it**modulo** <code>10<sup>9</sup> + 7</code>.
10+
11+
**Note** that a subsequence of size 1 is considered good by definition.
12+
13+
**Example 1:**
14+
15+
**Input:** nums =[1,2,1]
16+
17+
**Output:** 14
18+
19+
**Explanation:**
20+
21+
* Good subsequences are:`[1]`,`[2]`,`[1]`,`[1,2]`,`[2,1]`,`[1,2,1]`.
22+
* The sum of elements in these subsequences is 14.
23+
24+
**Example 2:**
25+
26+
**Input:** nums =[3,4,5]
27+
28+
**Output:** 40
29+
30+
**Explanation:**
31+
32+
* Good subsequences are:`[3]`,`[4]`,`[5]`,`[3,4]`,`[4,5]`,`[3,4,5]`.
33+
* The sum of elements in these subsequences is 40.
34+
35+
**Constraints:**
36+
37+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
38+
* <code>0 <= nums[i] <= 10<sup>5</sup></code>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
packageg3301_3400.s3352_count_k_reducible_numbers_less_than_n;
2+
3+
// #Hard #String #Dynamic_Programming #Math #Combinatorics
4+
// #2024_11_15_Time_11_ms_(99.58%)_Space_42.6_MB_(95.85%)
5+
6+
publicclassSolution {
7+
privatestaticfinalintMOD = (int) (1e9 +7);
8+
9+
publicintcountKReducibleNumbers(Strings,intk) {
10+
intn =s.length();
11+
int[]reducible =newint[n +1];
12+
for (inti =2;i <reducible.length;i++) {
13+
reducible[i] =1 +reducible[Integer.bitCount(i)];
14+
}
15+
long[]dp =newlong[n +1];
16+
intcurr =0;
17+
for (inti =0;i <n;i++) {
18+
for (intj =i -1;j >=0;j--) {
19+
dp[j +1] +=dp[j];
20+
dp[j +1] %=MOD;
21+
}
22+
if (s.charAt(i) =='1') {
23+
dp[curr]++;
24+
dp[curr] %=MOD;
25+
curr++;
26+
}
27+
}
28+
longresult =0;
29+
for (inti =1;i <=s.length();i++) {
30+
if (reducible[i] <k) {
31+
result +=dp[i];
32+
result %=MOD;
33+
}
34+
}
35+
return (int) (result %MOD);
36+
}
37+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
3352\. Count K-Reducible Numbers Less Than N
2+
3+
Hard
4+
5+
You are given a**binary** string`s` representing a number`n` in its binary form.
6+
7+
You are also given an integer`k`.
8+
9+
An integer`x` is called**k-reducible** if performing the following operation**at most**`k` times reduces it to 1:
10+
11+
* Replace`x` with the**count** of set bits in its binary representation.
12+
13+
For example, the binary representation of 6 is`"110"`. Applying the operation once reduces it to 2 (since`"110"` has two set bits). Applying the operation again to 2 (binary`"10"`) reduces it to 1 (since`"10"` has one set bit).
14+
15+
Return an integer denoting the number of positive integers**less** than`n` that are**k-reducible**.
16+
17+
Since the answer may be too large, return it**modulo** <code>10<sup>9</sup> + 7</code>.
18+
19+
**Example 1:**
20+
21+
**Input:** s = "111", k = 1
22+
23+
**Output:** 3
24+
25+
**Explanation:**
26+
27+
`n = 7`. The 1-reducible integers less than 7 are 1, 2, and 4.
28+
29+
**Example 2:**
30+
31+
**Input:** s = "1000", k = 2
32+
33+
**Output:** 6
34+
35+
**Explanation:**
36+
37+
`n = 8`. The 2-reducible integers less than 8 are 1, 2, 3, 4, 5, and 6.
38+
39+
**Example 3:**
40+
41+
**Input:** s = "1", k = 3
42+
43+
**Output:** 0
44+
45+
**Explanation:**
46+
47+
There are no positive integers less than`n = 1`, so the answer is 0.
48+
49+
**Constraints:**
50+
51+
*`1 <= s.length <= 800`
52+
*`s` has no leading zeros.
53+
*`s` consists only of the characters`'0'` and`'1'`.
54+
*`1 <= k <= 5`
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
packageg3301_3400.s3349_adjacent_increasing_subarrays_detection_i;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importjava.util.List;
7+
importorg.junit.jupiter.api.Test;
8+
9+
classSolutionTest {
10+
@Test
11+
voidhasIncreasingSubarrays() {
12+
assertThat(
13+
newSolution().hasIncreasingSubarrays(List.of(2,5,7,8,9,2,3,4,3,1),3),
14+
equalTo(true));
15+
}
16+
17+
@Test
18+
voidhasIncreasingSubarrays2() {
19+
assertThat(
20+
newSolution().hasIncreasingSubarrays(List.of(1,2,3,4,4,4,4,5,6,7),5),
21+
equalTo(false));
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
packageg3301_3400.s3350_adjacent_increasing_subarrays_detection_ii;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importjava.util.List;
7+
importorg.junit.jupiter.api.Test;
8+
9+
classSolutionTest {
10+
@Test
11+
voidmaxIncreasingSubarrays() {
12+
assertThat(
13+
newSolution().maxIncreasingSubarrays(List.of(2,5,7,8,9,2,3,4,3,1)),
14+
equalTo(3));
15+
}
16+
17+
@Test
18+
voidmaxIncreasingSubarrays2() {
19+
assertThat(
20+
newSolution().maxIncreasingSubarrays(List.of(1,2,3,4,4,4,4,5,6,7)),
21+
equalTo(2));
22+
}
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
packageg3301_3400.s3351_sum_of_good_subsequences;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importorg.junit.jupiter.api.Test;
7+
8+
classSolutionTest {
9+
@Test
10+
voidsumOfGoodSubsequences() {
11+
assertThat(newSolution().sumOfGoodSubsequences(newint[] {1,2,1}),equalTo(14));
12+
}
13+
14+
@Test
15+
voidsumOfGoodSubsequences2() {
16+
assertThat(newSolution().sumOfGoodSubsequences(newint[] {3,4,5}),equalTo(40));
17+
}
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
packageg3301_3400.s3352_count_k_reducible_numbers_less_than_n;
2+
3+
importstaticorg.hamcrest.CoreMatchers.equalTo;
4+
importstaticorg.hamcrest.MatcherAssert.assertThat;
5+
6+
importorg.junit.jupiter.api.Test;
7+
8+
classSolutionTest {
9+
@Test
10+
voidcountKReducibleNumbers() {
11+
assertThat(newSolution().countKReducibleNumbers("111",1),equalTo(3));
12+
}
13+
14+
@Test
15+
voidcountKReducibleNumbers2() {
16+
assertThat(newSolution().countKReducibleNumbers("1000",2),equalTo(6));
17+
}
18+
19+
@Test
20+
voidcountKReducibleNumbers3() {
21+
assertThat(newSolution().countKReducibleNumbers("1",3),equalTo(0));
22+
}
23+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp