From a627602e805f03b395eff0d7ae7a4405b76811ac Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sat, 25 May 2024 03:37:33 +0300 Subject: [PATCH 1/2] Added tasks 3151-3154 --- .../s3151_special_array_i/Solution.kt | 17 ++++++ .../s3151_special_array_i/readme.md | 42 +++++++++++++ .../s3152_special_array_ii/Solution.kt | 24 ++++++++ .../s3152_special_array_ii/readme.md | 38 ++++++++++++ .../Solution.kt | 19 ++++++ .../readme.md | 37 ++++++++++++ .../Solution.kt | 34 +++++++++++ .../readme.md | 59 +++++++++++++++++++ .../s3151_special_array_i/SolutionTest.kt | 27 +++++++++ .../s3152_special_array_ii/SolutionTest.kt | 23 ++++++++ .../SolutionTest.kt | 17 ++++++ .../SolutionTest.kt | 17 ++++++ 12 files changed, 354 insertions(+) create mode 100644 src/main/kotlin/g3101_3200/s3151_special_array_i/Solution.kt create mode 100644 src/main/kotlin/g3101_3200/s3151_special_array_i/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3152_special_array_ii/Solution.kt create mode 100644 src/main/kotlin/g3101_3200/s3152_special_array_ii/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3153_sum_of_digit_differences_of_all_pairs/Solution.kt create mode 100644 src/main/kotlin/g3101_3200/s3153_sum_of_digit_differences_of_all_pairs/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3154_find_number_of_ways_to_reach_the_k_th_stair/Solution.kt create mode 100644 src/main/kotlin/g3101_3200/s3154_find_number_of_ways_to_reach_the_k_th_stair/readme.md create mode 100644 src/test/kotlin/g3101_3200/s3151_special_array_i/SolutionTest.kt create mode 100644 src/test/kotlin/g3101_3200/s3152_special_array_ii/SolutionTest.kt create mode 100644 src/test/kotlin/g3101_3200/s3153_sum_of_digit_differences_of_all_pairs/SolutionTest.kt create mode 100644 src/test/kotlin/g3101_3200/s3154_find_number_of_ways_to_reach_the_k_th_stair/SolutionTest.kt diff --git a/src/main/kotlin/g3101_3200/s3151_special_array_i/Solution.kt b/src/main/kotlin/g3101_3200/s3151_special_array_i/Solution.kt new file mode 100644 index 000000000..197244084 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3151_special_array_i/Solution.kt @@ -0,0 +1,17 @@ +package g3101_3200.s3151_special_array_i + +// #Easy #Array #2024_05_22_Time_0_ms_(100.00%)_Space_43.2_MB_(51.16%) + +class Solution { + fun isArraySpecial(nums: IntArray): Boolean { + for (i in 1 until nums.size) { + if (nums[i - 1] % 2 == 1 && nums[i] % 2 == 1) { + return false + } + if (nums[i - 1] % 2 == 0 && nums[i] % 2 == 0) { + return false + } + } + return true + } +} diff --git a/src/main/kotlin/g3101_3200/s3151_special_array_i/readme.md b/src/main/kotlin/g3101_3200/s3151_special_array_i/readme.md new file mode 100644 index 000000000..01b5486c5 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3151_special_array_i/readme.md @@ -0,0 +1,42 @@ +3151\. Special Array I + +Easy + +An array is considered **special** if every pair of its adjacent elements contains two numbers with different parity. + +You are given an array of integers `nums`. Return `true` if `nums` is a **special** array, otherwise, return `false`. + +**Example 1:** + +**Input:** nums = [1] + +**Output:** true + +**Explanation:** + +There is only one element. So the answer is `true`. + +**Example 2:** + +**Input:** nums = [2,1,4] + +**Output:** true + +**Explanation:** + +There is only two pairs: `(2,1)` and `(1,4)`, and both of them contain numbers with different parity. So the answer is `true`. + +**Example 3:** + +**Input:** nums = [4,3,1,6] + +**Output:** false + +**Explanation:** + +`nums[1]` and `nums[2]` are both odd. So the answer is `false`. + +**Constraints:** + +* `1 <= nums.length <= 100` +* `1 <= nums[i] <= 100` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3152_special_array_ii/Solution.kt b/src/main/kotlin/g3101_3200/s3152_special_array_ii/Solution.kt new file mode 100644 index 000000000..fae738a0d --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3152_special_array_ii/Solution.kt @@ -0,0 +1,24 @@ +package g3101_3200.s3152_special_array_ii + +// #Medium #Array #Binary_Search #Prefix_Sum #2024_05_22_Time_2_ms_(99.93%)_Space_97.9_MB_(79.71%) + +class Solution { + fun isArraySpecial(nums: IntArray, queries: Array): BooleanArray { + val n = nums.size + val bad = IntArray(n) + for (i in 1 until n) { + bad[i] = bad[i - 1] + (((nums[i - 1] xor nums[i]) and 1) xor 1) + } + val nq = queries.size + val res = BooleanArray(nq) + for (i in 0 until nq) { + val q = queries[i] + res[i] = calc(bad, q[0], q[1]) == 0 + } + return res + } + + private fun calc(arr: IntArray, st: Int, end: Int): Int { + return arr[end] - arr[st] + } +} diff --git a/src/main/kotlin/g3101_3200/s3152_special_array_ii/readme.md b/src/main/kotlin/g3101_3200/s3152_special_array_ii/readme.md new file mode 100644 index 000000000..d83a32dd9 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3152_special_array_ii/readme.md @@ -0,0 +1,38 @@ +3152\. Special Array II + +Medium + +An array is considered **special** if every pair of its adjacent elements contains two numbers with different parity. + +You are given an array of integer `nums` and a 2D integer matrix `queries`, where for queries[i] = [fromi, toi] your task is to check that subarray nums[fromi..toi] is **special** or not. + +Return an array of booleans `answer` such that `answer[i]` is `true` if nums[fromi..toi] is special. + +**Example 1:** + +**Input:** nums = [3,4,1,2,6], queries = [[0,4]] + +**Output:** [false] + +**Explanation:** + +The subarray is `[3,4,1,2,6]`. 2 and 6 are both even. + +**Example 2:** + +**Input:** nums = [4,3,1,6], queries = [[0,2],[2,3]] + +**Output:** [false,true] + +**Explanation:** + +1. The subarray is `[4,3,1]`. 3 and 1 are both odd. So the answer to this query is `false`. +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`. + +**Constraints:** + +* 1 <= nums.length <= 105 +* 1 <= nums[i] <= 105 +* 1 <= queries.length <= 105 +* `queries[i].length == 2` +* `0 <= queries[i][0] <= queries[i][1] <= nums.length - 1` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3153_sum_of_digit_differences_of_all_pairs/Solution.kt b/src/main/kotlin/g3101_3200/s3153_sum_of_digit_differences_of_all_pairs/Solution.kt new file mode 100644 index 000000000..2abd21bf1 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3153_sum_of_digit_differences_of_all_pairs/Solution.kt @@ -0,0 +1,19 @@ +package g3101_3200.s3153_sum_of_digit_differences_of_all_pairs + +// #Medium #Array #Hash_Table #Math #Counting #2024_05_22_Time_12_ms_(100.00%)_Space_62.8_MB_(6.25%) + +class Solution { + fun sumDigitDifferences(nums: IntArray): Long { + var result: Long = 0 + while (nums[0] > 0) { + val counts = IntArray(10) + for (i in nums.indices) { + val digit = nums[i] % 10 + nums[i] = nums[i] / 10 + result += (i - counts[digit]).toLong() + counts[digit]++ + } + } + return result + } +} diff --git a/src/main/kotlin/g3101_3200/s3153_sum_of_digit_differences_of_all_pairs/readme.md b/src/main/kotlin/g3101_3200/s3153_sum_of_digit_differences_of_all_pairs/readme.md new file mode 100644 index 000000000..63a7535a2 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3153_sum_of_digit_differences_of_all_pairs/readme.md @@ -0,0 +1,37 @@ +3153\. Sum of Digit Differences of All Pairs + +Medium + +You are given an array `nums` consisting of **positive** integers where all integers have the **same** number of digits. + +The **digit difference** between two integers is the _count_ of different digits that are in the **same** position in the two integers. + +Return the **sum** of the **digit differences** between **all** pairs of integers in `nums`. + +**Example 1:** + +**Input:** nums = [13,23,12] + +**Output:** 4 + +**Explanation:** + We have the following: + - The digit difference between **1**3 and **2**3 is 1. + - The digit difference between 1**3** and 1**2** is 1. + - The digit difference between **23** and **12** is 2. + So the total sum of digit differences between all pairs of integers is `1 + 1 + 2 = 4`. + +**Example 2:** + +**Input:** nums = [10,10,10,10] + +**Output:** 0 + +**Explanation:** + All the integers in the array are the same. So the total sum of digit differences between all pairs of integers will be 0. + +**Constraints:** + +* 2 <= nums.length <= 105 +* 1 <= nums[i] < 109 +* All integers in `nums` have the same number of digits. \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3154_find_number_of_ways_to_reach_the_k_th_stair/Solution.kt b/src/main/kotlin/g3101_3200/s3154_find_number_of_ways_to_reach_the_k_th_stair/Solution.kt new file mode 100644 index 000000000..ae4df6681 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3154_find_number_of_ways_to_reach_the_k_th_stair/Solution.kt @@ -0,0 +1,34 @@ +package g3101_3200.s3154_find_number_of_ways_to_reach_the_k_th_stair + +// #Hard #Dynamic_Programming #Math #Bit_Manipulation #Memoization #Combinatorics +// #2024_05_22_Time_0_ms_(100.00%)_Space_40.3_MB_(98.50%) + +@Suppress("NAME_SHADOWING") +class Solution { + fun waysToReachStair(k: Int): Int { + var x = 1 + var y = 1 + var a = 0 + while (x > 0 && x - y <= k) { + if (x >= k) { + a += combi(y, x - k) + } + x = x shl 1 + y++ + } + return a + } + + private fun combi(a: Int, b: Int): Int { + var b = b + if (b > a - b) { + b = a - b + } + var r: Long = 1 + for (i in 0 until b) { + r *= (a - i).toLong() + r /= (i + 1).toLong() + } + return r.toInt() + } +} diff --git a/src/main/kotlin/g3101_3200/s3154_find_number_of_ways_to_reach_the_k_th_stair/readme.md b/src/main/kotlin/g3101_3200/s3154_find_number_of_ways_to_reach_the_k_th_stair/readme.md new file mode 100644 index 000000000..9b9e3187b --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3154_find_number_of_ways_to_reach_the_k_th_stair/readme.md @@ -0,0 +1,59 @@ +3154\. Find Number of Ways to Reach the K-th Stair + +Hard + +You are given a **non-negative** integer `k`. There exists a staircase with an infinite number of stairs, with the **lowest** stair numbered 0. + +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: + +* Go down to stair `i - 1`. This operation **cannot** be used consecutively or on stair 0. +* Go up to stair i + 2jump. And then, `jump` becomes `jump + 1`. + +Return the _total_ number of ways Alice can reach stair `k`. + +**Note** that it is possible that Alice reaches the stair `k`, and performs some operations to reach the stair `k` again. + +**Example 1:** + +**Input:** k = 0 + +**Output:** 2 + +**Explanation:** + +The 2 possible ways of reaching stair 0 are: + +* Alice starts at stair 1. + * Using an operation of the first type, she goes down 1 stair to reach stair 0. +* Alice starts at stair 1. + * Using an operation of the first type, she goes down 1 stair to reach stair 0. + * Using an operation of the second type, she goes up 20 stairs to reach stair 1. + * Using an operation of the first type, she goes down 1 stair to reach stair 0. + +**Example 2:** + +**Input:** k = 1 + +**Output:** 4 + +**Explanation:** + +The 4 possible ways of reaching stair 1 are: + +* Alice starts at stair 1. Alice is at stair 1. +* Alice starts at stair 1. + * Using an operation of the first type, she goes down 1 stair to reach stair 0. + * Using an operation of the second type, she goes up 20 stairs to reach stair 1. +* Alice starts at stair 1. + * Using an operation of the second type, she goes up 20 stairs to reach stair 2. + * Using an operation of the first type, she goes down 1 stair to reach stair 1. +* Alice starts at stair 1. + * Using an operation of the first type, she goes down 1 stair to reach stair 0. + * Using an operation of the second type, she goes up 20 stairs to reach stair 1. + * Using an operation of the first type, she goes down 1 stair to reach stair 0. + * Using an operation of the second type, she goes up 21 stairs to reach stair 2. + * Using an operation of the first type, she goes down 1 stair to reach stair 1. + +**Constraints:** + +* 0 <= k <= 109 \ No newline at end of file diff --git a/src/test/kotlin/g3101_3200/s3151_special_array_i/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3151_special_array_i/SolutionTest.kt new file mode 100644 index 000000000..198e87e86 --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3151_special_array_i/SolutionTest.kt @@ -0,0 +1,27 @@ +package g3101_3200.s3151_special_array_i + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun isArraySpecial() { + assertThat(Solution().isArraySpecial(intArrayOf(1)), equalTo(true)) + } + + @Test + fun isArraySpecial2() { + assertThat(Solution().isArraySpecial(intArrayOf(2, 1, 4)), equalTo(true)) + } + + @Test + fun isArraySpecial3() { + assertThat(Solution().isArraySpecial(intArrayOf(4, 3, 1, 6)), equalTo(false)) + } + + @Test + fun isArraySpecial4() { + assertThat(Solution().isArraySpecial(intArrayOf(2, 10)), equalTo(false)) + } +} diff --git a/src/test/kotlin/g3101_3200/s3152_special_array_ii/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3152_special_array_ii/SolutionTest.kt new file mode 100644 index 000000000..a6e28208b --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3152_special_array_ii/SolutionTest.kt @@ -0,0 +1,23 @@ +package g3101_3200.s3152_special_array_ii + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun isArraySpecial() { + assertThat( + Solution().isArraySpecial(intArrayOf(3, 4, 1, 2, 6), arrayOf(intArrayOf(0, 4))), + equalTo(booleanArrayOf(false)) + ) + } + + @Test + fun isArraySpecial2() { + assertThat( + Solution().isArraySpecial(intArrayOf(4, 3, 1, 6), arrayOf(intArrayOf(0, 2), intArrayOf(2, 3))), + equalTo(booleanArrayOf(false, true)) + ) + } +} diff --git a/src/test/kotlin/g3101_3200/s3153_sum_of_digit_differences_of_all_pairs/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3153_sum_of_digit_differences_of_all_pairs/SolutionTest.kt new file mode 100644 index 000000000..0e38d9944 --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3153_sum_of_digit_differences_of_all_pairs/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3101_3200.s3153_sum_of_digit_differences_of_all_pairs + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun sumDigitDifferences() { + assertThat(Solution().sumDigitDifferences(intArrayOf(13, 23, 12)), equalTo(4L)) + } + + @Test + fun sumDigitDifferences2() { + assertThat(Solution().sumDigitDifferences(intArrayOf(10, 10, 10, 10)), equalTo(0L)) + } +} diff --git a/src/test/kotlin/g3101_3200/s3154_find_number_of_ways_to_reach_the_k_th_stair/SolutionTest.kt b/src/test/kotlin/g3101_3200/s3154_find_number_of_ways_to_reach_the_k_th_stair/SolutionTest.kt new file mode 100644 index 000000000..95a51cfa5 --- /dev/null +++ b/src/test/kotlin/g3101_3200/s3154_find_number_of_ways_to_reach_the_k_th_stair/SolutionTest.kt @@ -0,0 +1,17 @@ +package g3101_3200.s3154_find_number_of_ways_to_reach_the_k_th_stair + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun waysToReachStair() { + assertThat(Solution().waysToReachStair(0), equalTo(2)) + } + + @Test + fun waysToReachStair2() { + assertThat(Solution().waysToReachStair(1), equalTo(4)) + } +} From e3364a5335afcc550fcbba1530e66886092cbaa5 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sat, 25 May 2024 03:41:24 +0300 Subject: [PATCH 2/2] Updated tags --- src/main/kotlin/g3101_3200/s3151_special_array_i/Solution.kt | 2 +- src/main/kotlin/g3101_3200/s3152_special_array_ii/Solution.kt | 2 +- .../s3153_sum_of_digit_differences_of_all_pairs/Solution.kt | 3 ++- .../Solution.kt | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/g3101_3200/s3151_special_array_i/Solution.kt b/src/main/kotlin/g3101_3200/s3151_special_array_i/Solution.kt index 197244084..daceef1d0 100644 --- a/src/main/kotlin/g3101_3200/s3151_special_array_i/Solution.kt +++ b/src/main/kotlin/g3101_3200/s3151_special_array_i/Solution.kt @@ -1,6 +1,6 @@ package g3101_3200.s3151_special_array_i -// #Easy #Array #2024_05_22_Time_0_ms_(100.00%)_Space_43.2_MB_(51.16%) +// #Easy #Array #2024_05_25_Time_165_ms_(92.21%)_Space_36.7_MB_(84.42%) class Solution { fun isArraySpecial(nums: IntArray): Boolean { diff --git a/src/main/kotlin/g3101_3200/s3152_special_array_ii/Solution.kt b/src/main/kotlin/g3101_3200/s3152_special_array_ii/Solution.kt index fae738a0d..922051db2 100644 --- a/src/main/kotlin/g3101_3200/s3152_special_array_ii/Solution.kt +++ b/src/main/kotlin/g3101_3200/s3152_special_array_ii/Solution.kt @@ -1,6 +1,6 @@ package g3101_3200.s3152_special_array_ii -// #Medium #Array #Binary_Search #Prefix_Sum #2024_05_22_Time_2_ms_(99.93%)_Space_97.9_MB_(79.71%) +// #Medium #Array #Binary_Search #Prefix_Sum #2024_05_25_Time_707_ms_(93.83%)_Space_93.9_MB_(59.26%) class Solution { fun isArraySpecial(nums: IntArray, queries: Array): BooleanArray { diff --git a/src/main/kotlin/g3101_3200/s3153_sum_of_digit_differences_of_all_pairs/Solution.kt b/src/main/kotlin/g3101_3200/s3153_sum_of_digit_differences_of_all_pairs/Solution.kt index 2abd21bf1..cc746f432 100644 --- a/src/main/kotlin/g3101_3200/s3153_sum_of_digit_differences_of_all_pairs/Solution.kt +++ b/src/main/kotlin/g3101_3200/s3153_sum_of_digit_differences_of_all_pairs/Solution.kt @@ -1,6 +1,7 @@ package g3101_3200.s3153_sum_of_digit_differences_of_all_pairs -// #Medium #Array #Hash_Table #Math #Counting #2024_05_22_Time_12_ms_(100.00%)_Space_62.8_MB_(6.25%) +// #Medium #Array #Hash_Table #Math #Counting +// #2024_05_25_Time_491_ms_(95.74%)_Space_61.5_MB_(48.94%) class Solution { fun sumDigitDifferences(nums: IntArray): Long { diff --git a/src/main/kotlin/g3101_3200/s3154_find_number_of_ways_to_reach_the_k_th_stair/Solution.kt b/src/main/kotlin/g3101_3200/s3154_find_number_of_ways_to_reach_the_k_th_stair/Solution.kt index ae4df6681..5a58b9e9a 100644 --- a/src/main/kotlin/g3101_3200/s3154_find_number_of_ways_to_reach_the_k_th_stair/Solution.kt +++ b/src/main/kotlin/g3101_3200/s3154_find_number_of_ways_to_reach_the_k_th_stair/Solution.kt @@ -1,7 +1,7 @@ package g3101_3200.s3154_find_number_of_ways_to_reach_the_k_th_stair // #Hard #Dynamic_Programming #Math #Bit_Manipulation #Memoization #Combinatorics -// #2024_05_22_Time_0_ms_(100.00%)_Space_40.3_MB_(98.50%) +// #2024_05_25_Time_122_ms_(100.00%)_Space_33.6_MB_(96.55%) @Suppress("NAME_SHADOWING") class Solution {