Skip to content

Commit e2a636c

Browse files
authored
Added tasks 3151-3162
1 parent ccb2708 commit e2a636c

File tree

14 files changed

+722
-13
lines changed
  • src/main/kotlin
    • g0101_0200/s0107_binary_tree_level_order_traversal_ii
    • g0301_0400/s0373_find_k_pairs_with_smallest_sums
    • g1701_1800/s1751_maximum_number_of_events_that_can_be_attended_ii
    • g2201_2300/s2286_booking_concert_tickets_in_groups
    • g3101_3200
      • s3151_special_array_i
      • s3152_special_array_ii
      • s3153_sum_of_digit_differences_of_all_pairs
      • s3154_find_number_of_ways_to_reach_the_k_th_stair
      • s3158_find_the_xor_of_numbers_which_appear_twice
      • s3159_find_occurrences_of_an_element_in_an_array
      • s3160_find_the_number_of_distinct_colors_among_the_balls
      • s3161_block_placement_queries
      • s3162_find_the_number_of_good_pairs_i

14 files changed

+722
-13
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,15 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 3162 |[Find the Number of Good Pairs I](src/main/kotlin/g3101_3200/s3162_find_the_number_of_good_pairs_i)| Easy | Array, Hash_Table | 182 | 54.41
1820+
| 3161 |[Block Placement Queries](src/main/kotlin/g3101_3200/s3161_block_placement_queries)| Hard | Array, Binary_Search, Segment_Tree, Binary_Indexed_Tree | 1701 | 100.00
1821+
| 3160 |[Find the Number of Distinct Colors Among the Balls](src/main/kotlin/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls)| Medium | Array, Hash_Table, Simulation | 1055 | 58.82
1822+
| 3159 |[Find Occurrences of an Element in an Array](src/main/kotlin/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array)| Medium | Array, Hash_Table | 810 | 98.28
1823+
| 3158 |[Find the XOR of Numbers Which Appear Twice](src/main/kotlin/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice)| Easy | Array, Hash_Table, Bit_Manipulation | 166 | 92.21
1824+
| 3154 |[Find Number of Ways to Reach the K-th Stair](src/main/kotlin/g3101_3200/s3154_find_number_of_ways_to_reach_the_k_th_stair)| Hard | Dynamic_Programming, Math, Bit_Manipulation, Memoization, Combinatorics | 122 | 100.00
1825+
| 3153 |[Sum of Digit Differences of All Pairs](src/main/kotlin/g3101_3200/s3153_sum_of_digit_differences_of_all_pairs)| Medium | Array, Hash_Table, Math, Counting | 491 | 95.74
1826+
| 3152 |[Special Array II](src/main/kotlin/g3101_3200/s3152_special_array_ii)| Medium | Array, Binary_Search, Prefix_Sum | 707 | 93.83
1827+
| 3151 |[Special Array I](src/main/kotlin/g3101_3200/s3151_special_array_i)| Easy | Array | 165 | 92.21
18191828
| 3149 |[Find the Minimum Cost Array Permutation](src/main/kotlin/g3101_3200/s3149_find_the_minimum_cost_array_permutation)| Hard | Array, Dynamic_Programming, Bit_Manipulation, Bitmask | 329 | 100.00
18201829
| 3148 |[Maximum Difference Score in a Grid](src/main/kotlin/g3101_3200/s3148_maximum_difference_score_in_a_grid)| Medium | Array, Dynamic_Programming, Matrix | 777 | 84.62
18211830
| 3147 |[Taking Maximum Energy From the Mystic Dungeon](src/main/kotlin/g3101_3200/s3147_taking_maximum_energy_from_the_mystic_dungeon)| Medium | Array, Prefix_Sum | 671 | 79.17

src/main/kotlin/g0101_0200/s0107_binary_tree_level_order_traversal_ii/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ Given the `root` of a binary tree, return _the bottom-up level order traversal o
3737
```kotlin
3838
import com_github_leetcode.TreeNode
3939
import java.util.Collections
40-
import kotlin.collections.ArrayList
4140

4241
/*
4342
* Example:

src/main/kotlin/g0301_0400/s0373_find_k_pairs_with_smallest_sums/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ Return _the_ `k` _pairs_ <code>(u<sub>1</sub>, v<sub>1</sub>), (u<sub>2</sub>, v
4646

4747
```kotlin
4848
import java.util.PriorityQueue
49-
import kotlin.collections.ArrayList
5049

5150
class Solution {
5251
private class Node(index: Int, num1: Int, num2: Int) {

src/main/kotlin/g1701_1800/s1751_maximum_number_of_events_that_can_be_attended_ii/readme.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,12 @@ Return _the **maximum sum** of values that you can receive by attending events._
5151
## Solution
5252

5353
```kotlin
54-
import java.util.Arrays
55-
5654
@Suppress("NAME_SHADOWING")
5755
class Solution {
5856
fun maxValue(events: Array<IntArray>, k: Int): Int {
5957
if (k == 1) {
60-
val value = Arrays.stream(events).max({ a: IntArray, b: IntArray -> a[2].compareTo(b[2]) })
61-
return if (value.isPresent) {
62-
value.get()[2]
63-
} else {
64-
throw NullPointerException()
65-
}
58+
val value = events.maxByOrNull { it[2] }
59+
return value?.get(2) ?: throw NullPointerException()
6660
}
6761
val n = events.size
6862
events.sortWith { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) }

src/main/kotlin/g2201_2300/s2286_booking_concert_tickets_in_groups/readme.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ Implement the `BookMyShow` class:
5555

5656
```kotlin
5757
import java.util.ArrayDeque
58-
import java.util.Arrays
5958
import java.util.Deque
6059

6160
@Suppress("NAME_SHADOWING")
@@ -86,8 +85,8 @@ class BookMyShow(n: Int, private val m: Int) {
8685
numZerosLeft = IntArray(this.n + 2)
8786
// initialize max and total, for max we firstly set values to m
8887
// segments of size 1 are placed starting from this.n - 1
89-
Arrays.fill(max, this.n - 1, this.n + n - 1, m)
90-
Arrays.fill(total, this.n - 1, this.n + n - 1, m.toLong())
88+
max.fill(m, this.n - 1, this.n + n - 1)
89+
total.fill(m.toLong(), this.n - 1, this.n + n - 1)
9190
// calculate values of max and total for segments based on values of their children
9291
var i = this.n - 2
9392
var i1 = i * 2 + 1
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3151\. Special Array I
5+
6+
Easy
7+
8+
An array is considered **special** if every pair of its adjacent elements contains two numbers with different parity.
9+
10+
You are given an array of integers `nums`. Return `true` if `nums` is a **special** array, otherwise, return `false`.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [1]
15+
16+
**Output:** true
17+
18+
**Explanation:**
19+
20+
There is only one element. So the answer is `true`.
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [2,1,4]
25+
26+
**Output:** true
27+
28+
**Explanation:**
29+
30+
There is only two pairs: `(2,1)` and `(1,4)`, and both of them contain numbers with different parity. So the answer is `true`.
31+
32+
**Example 3:**
33+
34+
**Input:** nums = [4,3,1,6]
35+
36+
**Output:** false
37+
38+
**Explanation:**
39+
40+
`nums[1]` and `nums[2]` are both odd. So the answer is `false`.
41+
42+
**Constraints:**
43+
44+
* `1 <= nums.length <= 100`
45+
* `1 <= nums[i] <= 100`
46+
47+
## Solution
48+
49+
```kotlin
50+
class Solution {
51+
fun isArraySpecial(nums: IntArray): Boolean {
52+
for (i in 1 until nums.size) {
53+
if (nums[i - 1] % 2 == 1 && nums[i] % 2 == 1) {
54+
return false
55+
}
56+
if (nums[i - 1] % 2 == 0 && nums[i] % 2 == 0) {
57+
return false
58+
}
59+
}
60+
return true
61+
}
62+
}
63+
```
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3152\. Special Array II
5+
6+
Medium
7+
8+
An array is considered **special** if every pair of its adjacent elements contains two numbers with different parity.
9+
10+
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.
11+
12+
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.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [3,4,1,2,6], queries = \[\[0,4]]
17+
18+
**Output:** [false]
19+
20+
**Explanation:**
21+
22+
The subarray is `[3,4,1,2,6]`. 2 and 6 are both even.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [4,3,1,6], queries = \[\[0,2],[2,3]]
27+
28+
**Output:** [false,true]
29+
30+
**Explanation:**
31+
32+
1. The subarray is `[4,3,1]`. 3 and 1 are both odd. So the answer to this query is `false`.
33+
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`.
34+
35+
**Constraints:**
36+
37+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
38+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
39+
* <code>1 <= queries.length <= 10<sup>5</sup></code>
40+
* `queries[i].length == 2`
41+
* `0 <= queries[i][0] <= queries[i][1] <= nums.length - 1`
42+
43+
## Solution
44+
45+
```kotlin
46+
class Solution {
47+
fun isArraySpecial(nums: IntArray, queries: Array<IntArray>): BooleanArray {
48+
val n = nums.size
49+
val bad = IntArray(n)
50+
for (i in 1 until n) {
51+
bad[i] = bad[i - 1] + (((nums[i - 1] xor nums[i]) and 1) xor 1)
52+
}
53+
val nq = queries.size
54+
val res = BooleanArray(nq)
55+
for (i in 0 until nq) {
56+
val q = queries[i]
57+
res[i] = calc(bad, q[0], q[1]) == 0
58+
}
59+
return res
60+
}
61+
62+
private fun calc(arr: IntArray, st: Int, end: Int): Int {
63+
return arr[end] - arr[st]
64+
}
65+
}
66+
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3153\. Sum of Digit Differences of All Pairs
5+
6+
Medium
7+
8+
You are given an array `nums` consisting of **positive** integers where all integers have the **same** number of digits.
9+
10+
The **digit difference** between two integers is the _count_ of different digits that are in the **same** position in the two integers.
11+
12+
Return the **sum** of the **digit differences** between **all** pairs of integers in `nums`.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [13,23,12]
17+
18+
**Output:** 4
19+
20+
**Explanation:**
21+
We have the following:
22+
- The digit difference between **1**3 and **2**3 is 1.
23+
- The digit difference between 1**3** and 1**2** is 1.
24+
- The digit difference between **23** and **12** is 2.
25+
So the total sum of digit differences between all pairs of integers is `1 + 1 + 2 = 4`.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [10,10,10,10]
30+
31+
**Output:** 0
32+
33+
**Explanation:**
34+
All the integers in the array are the same. So the total sum of digit differences between all pairs of integers will be 0.
35+
36+
**Constraints:**
37+
38+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
39+
* <code>1 <= nums[i] < 10<sup>9</sup></code>
40+
* All integers in `nums` have the same number of digits.
41+
42+
## Solution
43+
44+
```kotlin
45+
class Solution {
46+
fun sumDigitDifferences(nums: IntArray): Long {
47+
var result: Long = 0
48+
while (nums[0] > 0) {
49+
val counts = IntArray(10)
50+
for (i in nums.indices) {
51+
val digit = nums[i] % 10
52+
nums[i] = nums[i] / 10
53+
result += (i - counts[digit]).toLong()
54+
counts[digit]++
55+
}
56+
}
57+
return result
58+
}
59+
}
60+
```
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3154\. Find Number of Ways to Reach the K-th Stair
5+
6+
Hard
7+
8+
You are given a **non-negative** integer `k`. There exists a staircase with an infinite number of stairs, with the **lowest** stair numbered 0.
9+
10+
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:
11+
12+
* Go down to stair `i - 1`. This operation **cannot** be used consecutively or on stair 0.
13+
* Go up to stair <code>i + 2<sup>jump</sup></code>. And then, `jump` becomes `jump + 1`.
14+
15+
Return the _total_ number of ways Alice can reach stair `k`.
16+
17+
**Note** that it is possible that Alice reaches the stair `k`, and performs some operations to reach the stair `k` again.
18+
19+
**Example 1:**
20+
21+
**Input:** k = 0
22+
23+
**Output:** 2
24+
25+
**Explanation:**
26+
27+
The 2 possible ways of reaching stair 0 are:
28+
29+
* Alice starts at stair 1.
30+
* Using an operation of the first type, she goes down 1 stair to reach stair 0.
31+
* Alice starts at stair 1.
32+
* Using an operation of the first type, she goes down 1 stair to reach stair 0.
33+
* Using an operation of the second type, she goes up 2<sup>0</sup> stairs to reach stair 1.
34+
* Using an operation of the first type, she goes down 1 stair to reach stair 0.
35+
36+
**Example 2:**
37+
38+
**Input:** k = 1
39+
40+
**Output:** 4
41+
42+
**Explanation:**
43+
44+
The 4 possible ways of reaching stair 1 are:
45+
46+
* Alice starts at stair 1. Alice is at stair 1.
47+
* Alice starts at stair 1.
48+
* Using an operation of the first type, she goes down 1 stair to reach stair 0.
49+
* Using an operation of the second type, she goes up 2<sup>0</sup> stairs to reach stair 1.
50+
* Alice starts at stair 1.
51+
* Using an operation of the second type, she goes up 2<sup>0</sup> stairs to reach stair 2.
52+
* Using an operation of the first type, she goes down 1 stair to reach stair 1.
53+
* Alice starts at stair 1.
54+
* Using an operation of the first type, she goes down 1 stair to reach stair 0.
55+
* Using an operation of the second type, she goes up 2<sup>0</sup> stairs to reach stair 1.
56+
* Using an operation of the first type, she goes down 1 stair to reach stair 0.
57+
* Using an operation of the second type, she goes up 2<sup>1</sup> stairs to reach stair 2.
58+
* Using an operation of the first type, she goes down 1 stair to reach stair 1.
59+
60+
**Constraints:**
61+
62+
* <code>0 <= k <= 10<sup>9</sup></code>
63+
64+
## Solution
65+
66+
```kotlin
67+
@Suppress("NAME_SHADOWING")
68+
class Solution {
69+
fun waysToReachStair(k: Int): Int {
70+
var x = 1
71+
var y = 1
72+
var a = 0
73+
while (x > 0 && x - y <= k) {
74+
if (x >= k) {
75+
a += combi(y, x - k)
76+
}
77+
x = x shl 1
78+
y++
79+
}
80+
return a
81+
}
82+
83+
private fun combi(a: Int, b: Int): Int {
84+
var b = b
85+
if (b > a - b) {
86+
b = a - b
87+
}
88+
var r: Long = 1
89+
for (i in 0 until b) {
90+
r *= (a - i).toLong()
91+
r /= (i + 1).toLong()
92+
}
93+
return r.toInt()
94+
}
95+
}
96+
```

0 commit comments

Comments
 (0)