Skip to content

Commit aadbdac

Browse files
authored
Added tasks 3151-3154
1 parent fe14067 commit aadbdac

File tree

12 files changed

+354
-0
lines changed

12 files changed

+354
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g3101_3200.s3151_special_array_i;
2+
3+
// #Easy #Array #2024_05_22_Time_0_ms_(100.00%)_Space_43.2_MB_(51.16%)
4+
5+
public class Solution {
6+
public boolean isArraySpecial(int[] nums) {
7+
for (int i = 1; i < nums.length; i++) {
8+
if (nums[i - 1] % 2 == 1 && nums[i] % 2 == 1) {
9+
return false;
10+
}
11+
if (nums[i - 1] % 2 == 0 && nums[i] % 2 == 0) {
12+
return false;
13+
}
14+
}
15+
return true;
16+
}
17+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
3151\. Special Array I
2+
3+
Easy
4+
5+
An array is considered **special** if every pair of its adjacent elements contains two numbers with different parity.
6+
7+
You are given an array of integers `nums`. Return `true` if `nums` is a **special** array, otherwise, return `false`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1]
12+
13+
**Output:** true
14+
15+
**Explanation:**
16+
17+
There is only one element. So the answer is `true`.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [2,1,4]
22+
23+
**Output:** true
24+
25+
**Explanation:**
26+
27+
There is only two pairs: `(2,1)` and `(1,4)`, and both of them contain numbers with different parity. So the answer is `true`.
28+
29+
**Example 3:**
30+
31+
**Input:** nums = [4,3,1,6]
32+
33+
**Output:** false
34+
35+
**Explanation:**
36+
37+
`nums[1]` and `nums[2]` are both odd. So the answer is `false`.
38+
39+
**Constraints:**
40+
41+
* `1 <= nums.length <= 100`
42+
* `1 <= nums[i] <= 100`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package g3101_3200.s3152_special_array_ii;
2+
3+
// #Medium #Array #Binary_Search #Prefix_Sum #2024_05_22_Time_2_ms_(99.93%)_Space_97.9_MB_(79.71%)
4+
5+
public class Solution {
6+
public boolean[] isArraySpecial(int[] nums, int[][] queries) {
7+
int n = nums.length;
8+
int[] bad = new int[n];
9+
for (int i = 1; i < n; i++) {
10+
bad[i] = bad[i - 1] + (((nums[i - 1] ^ nums[i]) & 1) ^ 1);
11+
}
12+
int nq = queries.length;
13+
boolean[] res = new boolean[nq];
14+
for (int i = 0; i < nq; i++) {
15+
int[] q = queries[i];
16+
res[i] = calc(bad, q[0], q[1]) == 0;
17+
}
18+
return res;
19+
}
20+
21+
private int calc(int[] arr, int st, int end) {
22+
return arr[end] - arr[st];
23+
}
24+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
3152\. Special Array II
2+
3+
Medium
4+
5+
An array is considered **special** if every pair of its adjacent elements contains two numbers with different parity.
6+
7+
You are given an array of integer `nums` and a 2D integer matrix `queries`, where for <code>queries[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> your task is to check that subarray <code>nums[from<sub>i</sub>..to<sub>i</sub>]</code> is **special** or not.
8+
9+
Return an array of booleans `answer` such that `answer[i]` is `true` if <code>nums[from<sub>i</sub>..to<sub>i</sub>]</code> is special.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [3,4,1,2,6], queries = [[0,4]]
14+
15+
**Output:** [false]
16+
17+
**Explanation:**
18+
19+
The subarray is `[3,4,1,2,6]`. 2 and 6 are both even.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [4,3,1,6], queries = [[0,2],[2,3]]
24+
25+
**Output:** [false,true]
26+
27+
**Explanation:**
28+
29+
1. The subarray is `[4,3,1]`. 3 and 1 are both odd. So the answer to this query is `false`.
30+
2. The subarray is `[1,6]`. There is only one pair: `(1,6)` and it contains numbers with different parity. So the answer to this query is `true`.
31+
32+
**Constraints:**
33+
34+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
35+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
36+
* <code>1 <= queries.length <= 10<sup>5</sup></code>
37+
* `queries[i].length == 2`
38+
* `0 <= queries[i][0] <= queries[i][1] <= nums.length - 1`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g3101_3200.s3153_sum_of_digit_differences_of_all_pairs;
2+
3+
// #Medium #Array #Hash_Table #Math #Counting #2024_05_22_Time_12_ms_(100.00%)_Space_62.8_MB_(6.25%)
4+
5+
public class Solution {
6+
public long sumDigitDifferences(int[] nums) {
7+
long result = 0;
8+
while (nums[0] > 0) {
9+
int[] counts = new int[10];
10+
for (int i = 0; i < nums.length; i++) {
11+
int digit = nums[i] % 10;
12+
nums[i] = nums[i] / 10;
13+
result += i - counts[digit];
14+
counts[digit]++;
15+
}
16+
}
17+
return result;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
3153\. Sum of Digit Differences of All Pairs
2+
3+
Medium
4+
5+
You are given an array `nums` consisting of **positive** integers where all integers have the **same** number of digits.
6+
7+
The **digit difference** between two integers is the _count_ of different digits that are in the **same** position in the two integers.
8+
9+
Return the **sum** of the **digit differences** between **all** pairs of integers in `nums`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [13,23,12]
14+
15+
**Output:** 4
16+
17+
**Explanation:**
18+
We have the following:
19+
- The digit difference between **1**3 and **2**3 is 1.
20+
- The digit difference between 1**3** and 1**2** is 1.
21+
- The digit difference between **23** and **12** is 2.
22+
So the total sum of digit differences between all pairs of integers is `1 + 1 + 2 = 4`.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [10,10,10,10]
27+
28+
**Output:** 0
29+
30+
**Explanation:**
31+
All the integers in the array are the same. So the total sum of digit differences between all pairs of integers will be 0.
32+
33+
**Constraints:**
34+
35+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
36+
* <code>1 <= nums[i] < 10<sup>9</sup></code>
37+
* All integers in `nums` have the same number of digits.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package g3101_3200.s3154_find_number_of_ways_to_reach_the_k_th_stair;
2+
3+
// #Hard #Dynamic_Programming #Math #Bit_Manipulation #Memoization #Combinatorics
4+
// #2024_05_22_Time_0_ms_(100.00%)_Space_40.3_MB_(98.50%)
5+
6+
public class Solution {
7+
public int waysToReachStair(int k) {
8+
int x = 1;
9+
int y = 1;
10+
int a = 0;
11+
while (x > 0 && x - y <= k) {
12+
if (x >= k) {
13+
a += combi(y, x - k);
14+
}
15+
x <<= 1;
16+
y++;
17+
}
18+
return a;
19+
}
20+
21+
private int combi(int a, int b) {
22+
if (b > a - b) {
23+
b = a - b;
24+
}
25+
long r = 1;
26+
for (int i = 0; i < b; i++) {
27+
r *= (a - i);
28+
r /= (i + 1);
29+
}
30+
return (int) r;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
3154\. Find Number of Ways to Reach the K-th Stair
2+
3+
Hard
4+
5+
You are given a **non-negative** integer `k`. There exists a staircase with an infinite number of stairs, with the **lowest** stair numbered 0.
6+
7+
Alice has an integer `jump`, with an initial value of 0. She starts on stair 1 and wants to reach stair `k` using **any** number of **operations**. If she is on stair `i`, in one **operation** she can:
8+
9+
* Go down to stair `i - 1`. This operation **cannot** be used consecutively or on stair 0.
10+
* Go up to stair <code>i + 2<sup>jump</sup></code>. And then, `jump` becomes `jump + 1`.
11+
12+
Return the _total_ number of ways Alice can reach stair `k`.
13+
14+
**Note** that it is possible that Alice reaches the stair `k`, and performs some operations to reach the stair `k` again.
15+
16+
**Example 1:**
17+
18+
**Input:** k = 0
19+
20+
**Output:** 2
21+
22+
**Explanation:**
23+
24+
The 2 possible ways of reaching stair 0 are:
25+
26+
* Alice starts at stair 1.
27+
* Using an operation of the first type, she goes down 1 stair to reach stair 0.
28+
* Alice starts at stair 1.
29+
* Using an operation of the first type, she goes down 1 stair to reach stair 0.
30+
* Using an operation of the second type, she goes up 2<sup>0</sup> stairs to reach stair 1.
31+
* Using an operation of the first type, she goes down 1 stair to reach stair 0.
32+
33+
**Example 2:**
34+
35+
**Input:** k = 1
36+
37+
**Output:** 4
38+
39+
**Explanation:**
40+
41+
The 4 possible ways of reaching stair 1 are:
42+
43+
* Alice starts at stair 1. Alice is at stair 1.
44+
* Alice starts at stair 1.
45+
* Using an operation of the first type, she goes down 1 stair to reach stair 0.
46+
* Using an operation of the second type, she goes up 2<sup>0</sup> stairs to reach stair 1.
47+
* Alice starts at stair 1.
48+
* Using an operation of the second type, she goes up 2<sup>0</sup> stairs to reach stair 2.
49+
* Using an operation of the first type, she goes down 1 stair to reach stair 1.
50+
* Alice starts at stair 1.
51+
* Using an operation of the first type, she goes down 1 stair to reach stair 0.
52+
* Using an operation of the second type, she goes up 2<sup>0</sup> stairs to reach stair 1.
53+
* Using an operation of the first type, she goes down 1 stair to reach stair 0.
54+
* Using an operation of the second type, she goes up 2<sup>1</sup> stairs to reach stair 2.
55+
* Using an operation of the first type, she goes down 1 stair to reach stair 1.
56+
57+
**Constraints:**
58+
59+
* <code>0 <= k <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g3101_3200.s3151_special_array_i;
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 isArraySpecial() {
11+
assertThat(new Solution().isArraySpecial(new int[] {1}), equalTo(true));
12+
}
13+
14+
@Test
15+
void isArraySpecial2() {
16+
assertThat(new Solution().isArraySpecial(new int[] {2, 1, 4}), equalTo(true));
17+
}
18+
19+
@Test
20+
void isArraySpecial3() {
21+
assertThat(new Solution().isArraySpecial(new int[] {4, 3, 1, 6}), equalTo(false));
22+
}
23+
24+
@Test
25+
void isArraySpecial4() {
26+
assertThat(new Solution().isArraySpecial(new int[] {2, 10}), equalTo(false));
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package g3101_3200.s3152_special_array_ii;
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 isArraySpecial() {
11+
assertThat(
12+
new Solution().isArraySpecial(new int[] {3, 4, 1, 2, 6}, new int[][] {{0, 4}}),
13+
equalTo(new boolean[] {false}));
14+
}
15+
16+
@Test
17+
void isArraySpecial2() {
18+
assertThat(
19+
new Solution().isArraySpecial(new int[] {4, 3, 1, 6}, new int[][] {{0, 2}, {2, 3}}),
20+
equalTo(new boolean[] {false, true}));
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3101_3200.s3153_sum_of_digit_differences_of_all_pairs;
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 sumDigitDifferences() {
11+
assertThat(new Solution().sumDigitDifferences(new int[] {13, 23, 12}), equalTo(4L));
12+
}
13+
14+
@Test
15+
void sumDigitDifferences2() {
16+
assertThat(new Solution().sumDigitDifferences(new int[] {10, 10, 10, 10}), equalTo(0L));
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g3101_3200.s3154_find_number_of_ways_to_reach_the_k_th_stair;
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 waysToReachStair() {
11+
assertThat(new Solution().waysToReachStair(0), equalTo(2));
12+
}
13+
14+
@Test
15+
void waysToReachStair2() {
16+
assertThat(new Solution().waysToReachStair(1), equalTo(4));
17+
}
18+
}

0 commit comments

Comments
 (0)