Skip to content

Commit e2fbe72

Browse files
authored
Added tasks 3151-3154
1 parent f937a62 commit e2fbe72

File tree

12 files changed

+355
-0
lines changed

12 files changed

+355
-0
lines changed
Lines changed: 17 additions & 0 deletions
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_25_Time_165_ms_(92.21%)_Space_36.7_MB_(84.42%)
4+
5+
class Solution {
6+
fun isArraySpecial(nums: IntArray): Boolean {
7+
for (i in 1 until nums.size) {
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+
}
Lines changed: 42 additions & 0 deletions
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`
Lines changed: 24 additions & 0 deletions
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_25_Time_707_ms_(93.83%)_Space_93.9_MB_(59.26%)
4+
5+
class Solution {
6+
fun isArraySpecial(nums: IntArray, queries: Array<IntArray>): BooleanArray {
7+
val n = nums.size
8+
val bad = IntArray(n)
9+
for (i in 1 until n) {
10+
bad[i] = bad[i - 1] + (((nums[i - 1] xor nums[i]) and 1) xor 1)
11+
}
12+
val nq = queries.size
13+
val res = BooleanArray(nq)
14+
for (i in 0 until nq) {
15+
val q = queries[i]
16+
res[i] = calc(bad, q[0], q[1]) == 0
17+
}
18+
return res
19+
}
20+
21+
private fun calc(arr: IntArray, st: Int, end: Int): Int {
22+
return arr[end] - arr[st]
23+
}
24+
}
Lines changed: 38 additions & 0 deletions
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`
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package g3101_3200.s3153_sum_of_digit_differences_of_all_pairs
2+
3+
// #Medium #Array #Hash_Table #Math #Counting
4+
// #2024_05_25_Time_491_ms_(95.74%)_Space_61.5_MB_(48.94%)
5+
6+
class Solution {
7+
fun sumDigitDifferences(nums: IntArray): Long {
8+
var result: Long = 0
9+
while (nums[0] > 0) {
10+
val counts = IntArray(10)
11+
for (i in nums.indices) {
12+
val digit = nums[i] % 10
13+
nums[i] = nums[i] / 10
14+
result += (i - counts[digit]).toLong()
15+
counts[digit]++
16+
}
17+
}
18+
return result
19+
}
20+
}
Lines changed: 37 additions & 0 deletions
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.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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_25_Time_122_ms_(100.00%)_Space_33.6_MB_(96.55%)
5+
6+
@Suppress("NAME_SHADOWING")
7+
class Solution {
8+
fun waysToReachStair(k: Int): Int {
9+
var x = 1
10+
var y = 1
11+
var a = 0
12+
while (x > 0 && x - y <= k) {
13+
if (x >= k) {
14+
a += combi(y, x - k)
15+
}
16+
x = x shl 1
17+
y++
18+
}
19+
return a
20+
}
21+
22+
private fun combi(a: Int, b: Int): Int {
23+
var b = b
24+
if (b > a - b) {
25+
b = a - b
26+
}
27+
var r: Long = 1
28+
for (i in 0 until b) {
29+
r *= (a - i).toLong()
30+
r /= (i + 1).toLong()
31+
}
32+
return r.toInt()
33+
}
34+
}
Lines changed: 59 additions & 0 deletions
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>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g3101_3200.s3151_special_array_i
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun isArraySpecial() {
10+
assertThat(Solution().isArraySpecial(intArrayOf(1)), equalTo(true))
11+
}
12+
13+
@Test
14+
fun isArraySpecial2() {
15+
assertThat(Solution().isArraySpecial(intArrayOf(2, 1, 4)), equalTo(true))
16+
}
17+
18+
@Test
19+
fun isArraySpecial3() {
20+
assertThat(Solution().isArraySpecial(intArrayOf(4, 3, 1, 6)), equalTo(false))
21+
}
22+
23+
@Test
24+
fun isArraySpecial4() {
25+
assertThat(Solution().isArraySpecial(intArrayOf(2, 10)), equalTo(false))
26+
}
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g3101_3200.s3152_special_array_ii
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun isArraySpecial() {
10+
assertThat(
11+
Solution().isArraySpecial(intArrayOf(3, 4, 1, 2, 6), arrayOf(intArrayOf(0, 4))),
12+
equalTo(booleanArrayOf(false))
13+
)
14+
}
15+
16+
@Test
17+
fun isArraySpecial2() {
18+
assertThat(
19+
Solution().isArraySpecial(intArrayOf(4, 3, 1, 6), arrayOf(intArrayOf(0, 2), intArrayOf(2, 3))),
20+
equalTo(booleanArrayOf(false, true))
21+
)
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g3101_3200.s3153_sum_of_digit_differences_of_all_pairs
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun sumDigitDifferences() {
10+
assertThat(Solution().sumDigitDifferences(intArrayOf(13, 23, 12)), equalTo(4L))
11+
}
12+
13+
@Test
14+
fun sumDigitDifferences2() {
15+
assertThat(Solution().sumDigitDifferences(intArrayOf(10, 10, 10, 10)), equalTo(0L))
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package g3101_3200.s3154_find_number_of_ways_to_reach_the_k_th_stair
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun waysToReachStair() {
10+
assertThat(Solution().waysToReachStair(0), equalTo(2))
11+
}
12+
13+
@Test
14+
fun waysToReachStair2() {
15+
assertThat(Solution().waysToReachStair(1), equalTo(4))
16+
}
17+
}

0 commit comments

Comments
 (0)