From 77b42b2a11293fa642c2d6b4a54e2f06db6f4d85 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Thu, 30 May 2024 05:29:45 +0300 Subject: [PATCH] Added tasks 3151-3162 --- README.md | 9 ++ .../readme.md | 1 - .../readme.md | 1 - .../readme.md | 10 +- .../readme.md | 5 +- .../s3151_special_array_i/readme.md | 63 ++++++++ .../s3152_special_array_ii/readme.md | 66 ++++++++ .../readme.md | 60 +++++++ .../readme.md | 96 +++++++++++ .../readme.md | 64 ++++++++ .../readme.md | 67 ++++++++ .../readme.md | 84 ++++++++++ .../s3161_block_placement_queries/readme.md | 153 ++++++++++++++++++ .../readme.md | 56 +++++++ 14 files changed, 722 insertions(+), 13 deletions(-) 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/readme.md 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/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3161_block_placement_queries/readme.md create mode 100644 src/main/kotlin/g3101_3200/s3162_find_the_number_of_good_pairs_i/readme.md diff --git a/README.md b/README.md index 0326b234..9c878d82 100644 --- a/README.md +++ b/README.md @@ -1816,6 +1816,15 @@ | # | Title | Difficulty | Tag | Time, ms | Time, % |------|----------------|-------------|-------------|----------|-------- +| 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 +| 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 +| 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 +| 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 +| 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 +| 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 +| 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 +| 3152 |[Special Array II](src/main/kotlin/g3101_3200/s3152_special_array_ii)| Medium | Array, Binary_Search, Prefix_Sum | 707 | 93.83 +| 3151 |[Special Array I](src/main/kotlin/g3101_3200/s3151_special_array_i)| Easy | Array | 165 | 92.21 | 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 | 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 | 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 diff --git a/src/main/kotlin/g0101_0200/s0107_binary_tree_level_order_traversal_ii/readme.md b/src/main/kotlin/g0101_0200/s0107_binary_tree_level_order_traversal_ii/readme.md index aea18640..2472d957 100644 --- a/src/main/kotlin/g0101_0200/s0107_binary_tree_level_order_traversal_ii/readme.md +++ b/src/main/kotlin/g0101_0200/s0107_binary_tree_level_order_traversal_ii/readme.md @@ -37,7 +37,6 @@ Given the `root` of a binary tree, return _the bottom-up level order traversal o ```kotlin import com_github_leetcode.TreeNode import java.util.Collections -import kotlin.collections.ArrayList /* * Example: diff --git a/src/main/kotlin/g0301_0400/s0373_find_k_pairs_with_smallest_sums/readme.md b/src/main/kotlin/g0301_0400/s0373_find_k_pairs_with_smallest_sums/readme.md index 711d2ab1..bd54e7c7 100644 --- a/src/main/kotlin/g0301_0400/s0373_find_k_pairs_with_smallest_sums/readme.md +++ b/src/main/kotlin/g0301_0400/s0373_find_k_pairs_with_smallest_sums/readme.md @@ -46,7 +46,6 @@ Return _the_ `k` _pairs_ (u1, v1), (u2, v ```kotlin import java.util.PriorityQueue -import kotlin.collections.ArrayList class Solution { private class Node(index: Int, num1: Int, num2: Int) { diff --git a/src/main/kotlin/g1701_1800/s1751_maximum_number_of_events_that_can_be_attended_ii/readme.md b/src/main/kotlin/g1701_1800/s1751_maximum_number_of_events_that_can_be_attended_ii/readme.md index d82a7ef6..46711522 100644 --- a/src/main/kotlin/g1701_1800/s1751_maximum_number_of_events_that_can_be_attended_ii/readme.md +++ b/src/main/kotlin/g1701_1800/s1751_maximum_number_of_events_that_can_be_attended_ii/readme.md @@ -51,18 +51,12 @@ Return _the **maximum sum** of values that you can receive by attending events._ ## Solution ```kotlin -import java.util.Arrays - @Suppress("NAME_SHADOWING") class Solution { fun maxValue(events: Array, k: Int): Int { if (k == 1) { - val value = Arrays.stream(events).max({ a: IntArray, b: IntArray -> a[2].compareTo(b[2]) }) - return if (value.isPresent) { - value.get()[2] - } else { - throw NullPointerException() - } + val value = events.maxByOrNull { it[2] } + return value?.get(2) ?: throw NullPointerException() } val n = events.size events.sortWith { a: IntArray, b: IntArray -> a[0].compareTo(b[0]) } diff --git a/src/main/kotlin/g2201_2300/s2286_booking_concert_tickets_in_groups/readme.md b/src/main/kotlin/g2201_2300/s2286_booking_concert_tickets_in_groups/readme.md index 9e08ef1a..56b53725 100644 --- a/src/main/kotlin/g2201_2300/s2286_booking_concert_tickets_in_groups/readme.md +++ b/src/main/kotlin/g2201_2300/s2286_booking_concert_tickets_in_groups/readme.md @@ -55,7 +55,6 @@ Implement the `BookMyShow` class: ```kotlin import java.util.ArrayDeque -import java.util.Arrays import java.util.Deque @Suppress("NAME_SHADOWING") @@ -86,8 +85,8 @@ class BookMyShow(n: Int, private val m: Int) { numZerosLeft = IntArray(this.n + 2) // initialize max and total, for max we firstly set values to m // segments of size 1 are placed starting from this.n - 1 - Arrays.fill(max, this.n - 1, this.n + n - 1, m) - Arrays.fill(total, this.n - 1, this.n + n - 1, m.toLong()) + max.fill(m, this.n - 1, this.n + n - 1) + total.fill(m.toLong(), this.n - 1, this.n + n - 1) // calculate values of max and total for segments based on values of their children var i = this.n - 2 var i1 = i * 2 + 1 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 00000000..a5113d5d --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3151_special_array_i/readme.md @@ -0,0 +1,63 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](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) + +## 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` + +## Solution + +```kotlin +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 + } +} +``` \ No newline at end of file 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 00000000..0c8234ed --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3152_special_array_ii/readme.md @@ -0,0 +1,66 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](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) + +## 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` + +## Solution + +```kotlin +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] + } +} +``` \ No newline at end of file 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 00000000..0042a687 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3153_sum_of_digit_differences_of_all_pairs/readme.md @@ -0,0 +1,60 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](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) + +## 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. + +## Solution + +```kotlin +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 + } +} +``` \ 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/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 00000000..27da50a8 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3154_find_number_of_ways_to_reach_the_k_th_stair/readme.md @@ -0,0 +1,96 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](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) + +## 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 + +## Solution + +```kotlin +@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() + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice/readme.md b/src/main/kotlin/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice/readme.md new file mode 100644 index 00000000..1880a0ba --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice/readme.md @@ -0,0 +1,64 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](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) + +## 3158\. Find the XOR of Numbers Which Appear Twice + +Easy + +You are given an array `nums`, where each number in the array appears **either** once or twice. + +Return the bitwise `XOR` of all the numbers that appear twice in the array, or 0 if no number appears twice. + +**Example 1:** + +**Input:** nums = [1,2,1,3] + +**Output:** 1 + +**Explanation:** + +The only number that appears twice in `nums` is 1. + +**Example 2:** + +**Input:** nums = [1,2,3] + +**Output:** 0 + +**Explanation:** + +No number appears twice in `nums`. + +**Example 3:** + +**Input:** nums = [1,2,2,1] + +**Output:** 3 + +**Explanation:** + +Numbers 1 and 2 appeared twice. `1 XOR 2 == 3`. + +**Constraints:** + +* `1 <= nums.length <= 50` +* `1 <= nums[i] <= 50` +* Each number in `nums` appears either once or twice. + +## Solution + +```kotlin +class Solution { + fun duplicateNumbersXOR(nums: IntArray): Int { + val appeared = BooleanArray(51) + var res = 0 + for (num in nums) { + if (appeared[num]) { + res = res xor num + } + appeared[num] = true + } + return res + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array/readme.md b/src/main/kotlin/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array/readme.md new file mode 100644 index 00000000..3bd9793c --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array/readme.md @@ -0,0 +1,67 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](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) + +## 3159\. Find Occurrences of an Element in an Array + +Medium + +You are given an integer array `nums`, an integer array `queries`, and an integer `x`. + +For each `queries[i]`, you need to find the index of the queries[i]th occurrence of `x` in the `nums` array. If there are fewer than `queries[i]` occurrences of `x`, the answer should be -1 for that query. + +Return an integer array `answer` containing the answers to all queries. + +**Example 1:** + +**Input:** nums = [1,3,1,7], queries = [1,3,2,4], x = 1 + +**Output:** [0,-1,2,-1] + +**Explanation:** + +* For the 1st query, the first occurrence of 1 is at index 0. +* For the 2nd query, there are only two occurrences of 1 in `nums`, so the answer is -1. +* For the 3rd query, the second occurrence of 1 is at index 2. +* For the 4th query, there are only two occurrences of 1 in `nums`, so the answer is -1. + +**Example 2:** + +**Input:** nums = [1,2,3], queries = [10], x = 5 + +**Output:** [-1] + +**Explanation:** + +* For the 1st query, 5 doesn't exist in `nums`, so the answer is -1. + +**Constraints:** + +* 1 <= nums.length, queries.length <= 105 +* 1 <= queries[i] <= 105 +* 1 <= nums[i], x <= 104 + +## Solution + +```kotlin +class Solution { + fun occurrencesOfElement(nums: IntArray, queries: IntArray, x: Int): IntArray { + val a = ArrayList() + run { + var i = 0 + val l = nums.size + while (i < l) { + if (nums[i] == x) { + a.add(i) + } + i++ + } + } + val l = queries.size + val r = IntArray(l) + for (i in 0 until l) { + r[i] = if (queries[i] > a.size) -1 else a[queries[i] - 1] + } + return r + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls/readme.md b/src/main/kotlin/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls/readme.md new file mode 100644 index 00000000..153243b3 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls/readme.md @@ -0,0 +1,84 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](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) + +## 3160\. Find the Number of Distinct Colors Among the Balls + +Medium + +You are given an integer `limit` and a 2D array `queries` of size `n x 2`. + +There are `limit + 1` balls with **distinct** labels in the range `[0, limit]`. Initially, all balls are uncolored. For every query in `queries` that is of the form `[x, y]`, you mark ball `x` with the color `y`. After each query, you need to find the number of **distinct** colors among the balls. + +Return an array `result` of length `n`, where `result[i]` denotes the number of distinct colors _after_ ith query. + +**Note** that when answering a query, lack of a color _will not_ be considered as a color. + +**Example 1:** + +**Input:** limit = 4, queries = \[\[1,4],[2,5],[1,3],[3,4]] + +**Output:** [1,2,2,3] + +**Explanation:** + +![](https://assets.leetcode.com/uploads/2024/04/17/ezgifcom-crop.gif) + +* After query 0, ball 1 has color 4. +* After query 1, ball 1 has color 4, and ball 2 has color 5. +* After query 2, ball 1 has color 3, and ball 2 has color 5. +* After query 3, ball 1 has color 3, ball 2 has color 5, and ball 3 has color 4. + +**Example 2:** + +**Input:** limit = 4, queries = \[\[0,1],[1,2],[2,2],[3,4],[4,5]] + +**Output:** [1,2,2,3,4] + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2024/04/17/ezgifcom-crop2.gif)** + +* After query 0, ball 0 has color 1. +* After query 1, ball 0 has color 1, and ball 1 has color 2. +* After query 2, ball 0 has color 1, and balls 1 and 2 have color 2. +* After query 3, ball 0 has color 1, balls 1 and 2 have color 2, and ball 3 has color 4. +* After query 4, ball 0 has color 1, balls 1 and 2 have color 2, ball 3 has color 4, and ball 4 has color 5. + +**Constraints:** + +* 1 <= limit <= 109 +* 1 <= n == queries.length <= 105 +* `queries[i].length == 2` +* `0 <= queries[i][0] <= limit` +* 1 <= queries[i][1] <= 109 + +## Solution + +```kotlin +class Solution { + fun queryResults(ignoredLimit: Int, queries: Array): IntArray { + val ballToColor: MutableMap = HashMap() + val colorToCnt: MutableMap = HashMap() + val ret = IntArray(queries.size) + var i = 0 + while (i < queries.size) { + val ball = queries[i][0] + val color = queries[i][1] + if (ballToColor.containsKey(ball)) { + val oldColor = ballToColor[ball]!! + val oldColorCnt = colorToCnt[oldColor]!! + if (oldColorCnt >= 2) { + colorToCnt[oldColor] = oldColorCnt - 1 + } else { + colorToCnt.remove(oldColor) + } + } + ballToColor[ball] = color + colorToCnt[color] = colorToCnt.getOrDefault(color, 0) + 1 + ret[i] = colorToCnt.size + i += 1 + } + return ret + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3161_block_placement_queries/readme.md b/src/main/kotlin/g3101_3200/s3161_block_placement_queries/readme.md new file mode 100644 index 00000000..d01360f5 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3161_block_placement_queries/readme.md @@ -0,0 +1,153 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](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) + +## 3161\. Block Placement Queries + +Hard + +There exists an infinite number line, with its origin at 0 and extending towards the **positive** x-axis. + +You are given a 2D array `queries`, which contains two types of queries: + +1. For a query of type 1, `queries[i] = [1, x]`. Build an obstacle at distance `x` from the origin. It is guaranteed that there is **no** obstacle at distance `x` when the query is asked. +2. For a query of type 2, `queries[i] = [2, x, sz]`. Check if it is possible to place a block of size `sz` _anywhere_ in the range `[0, x]` on the line, such that the block **entirely** lies in the range `[0, x]`. A block **cannot** be placed if it intersects with any obstacle, but it may touch it. Note that you do **not** actually place the block. Queries are separate. + +Return a boolean array `results`, where `results[i]` is `true` if you can place the block specified in the ith query of type 2, and `false` otherwise. + +**Example 1:** + +**Input:** queries = \[\[1,2],[2,3,3],[2,3,1],[2,2,2]] + +**Output:** [false,true,true] + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2024/04/22/example0block.png)** + +For query 0, place an obstacle at `x = 2`. A block of size at most 2 can be placed before `x = 3`. + +**Example 2:** + +**Input:** queries = \[\[1,7],[2,7,6],[1,2],[2,7,5],[2,7,6]] + +**Output:** [true,true,false] + +**Explanation:** + +**![](https://assets.leetcode.com/uploads/2024/04/22/example1block.png)** + +* Place an obstacle at `x = 7` for query 0. A block of size at most 7 can be placed before `x = 7`. +* Place an obstacle at `x = 2` for query 2. Now, a block of size at most 5 can be placed before `x = 7`, and a block of size at most 2 before `x = 2`. + +**Constraints:** + +* 1 <= queries.length <= 15 * 104 +* `2 <= queries[i].length <= 3` +* `1 <= queries[i][0] <= 2` +* 1 <= x, sz <= min(5 * 104, 3 * queries.length) +* The input is generated such that for queries of type 1, no obstacle exists at distance `x` when the query is asked. +* The input is generated such that there is at least one query of type 2. + +## Solution + +```kotlin +import kotlin.math.max + +class Solution { + private class Seg private constructor(private val start: Int, private val end: Int) { + private var min = 0 + private var max = 0 + private var len = 0 + private var obstacle = false + private lateinit var left: Seg + private lateinit var right: Seg + + init { + if (start < end) { + val mid = start + ((end - start) shr 1) + left = Seg(start, mid) + right = Seg(mid + 1, end) + refresh() + } + } + + fun set(i: Int) { + if (i < start || i > end) { + return + } else if (i == start && i == end) { + obstacle = true + max = start + min = max + return + } + left.set(i) + right.set(i) + refresh() + } + + private fun refresh() { + if (left.obstacle) { + min = left.min + if (right.obstacle) { + max = right.max + len = max((right.min - left.max), max(left.len, right.len)) + } else { + max = left.max + len = max(left.len, (right.end - left.max)) + } + obstacle = true + } else if (right.obstacle) { + min = right.min + max = right.max + len = max(right.len, (right.min - left.start)) + obstacle = true + } else { + len = end - start + } + } + + fun max(n: Int, t: IntArray) { + if (end <= n) { + t[0] = max(t[0], len) + if (obstacle) { + t[1] = max + } + return + } + left.max(n, t) + if (!right.obstacle || right.min >= n) { + return + } + t[0] = max(t[0], (right.min - t[1])) + right.max(n, t) + } + + companion object { + fun init(n: Int): Seg { + return Seg(0, n) + } + } + } + + fun getResults(queries: Array): List { + var max = 0 + for (i in queries) { + max = max(max, i[1]) + } + val root = Seg.init(max) + root.set(0) + + val res: MutableList = ArrayList(queries.size) + for (i in queries) { + if (i[0] == 1) { + root.set(i[1]) + } else { + val t = IntArray(2) + root.max(i[1], t) + res.add(max(t[0], (i[1] - t[1])) >= i[2]) + } + } + return res + } +} +``` \ No newline at end of file diff --git a/src/main/kotlin/g3101_3200/s3162_find_the_number_of_good_pairs_i/readme.md b/src/main/kotlin/g3101_3200/s3162_find_the_number_of_good_pairs_i/readme.md new file mode 100644 index 00000000..84e6cd46 --- /dev/null +++ b/src/main/kotlin/g3101_3200/s3162_find_the_number_of_good_pairs_i/readme.md @@ -0,0 +1,56 @@ +[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin) +[![](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) + +## 3162\. Find the Number of Good Pairs I + +Easy + +You are given 2 integer arrays `nums1` and `nums2` of lengths `n` and `m` respectively. You are also given a **positive** integer `k`. + +A pair `(i, j)` is called **good** if `nums1[i]` is divisible by `nums2[j] * k` (`0 <= i <= n - 1`, `0 <= j <= m - 1`). + +Return the total number of **good** pairs. + +**Example 1:** + +**Input:** nums1 = [1,3,4], nums2 = [1,3,4], k = 1 + +**Output:** 5 + +**Explanation:** + +The 5 good pairs are `(0, 0)`, `(1, 0)`, `(1, 1)`, `(2, 0)`, and `(2, 2)`. + +**Example 2:** + +**Input:** nums1 = [1,2,4,12], nums2 = [2,4], k = 3 + +**Output:** 2 + +**Explanation:** + +The 2 good pairs are `(3, 0)` and `(3, 1)`. + +**Constraints:** + +* `1 <= n, m <= 50` +* `1 <= nums1[i], nums2[j] <= 50` +* `1 <= k <= 50` + +## Solution + +```kotlin +class Solution { + fun numberOfPairs(nums1: IntArray, nums2: IntArray, k: Int): Int { + var count = 0 + for (j in nums1) { + for (value in nums2) { + if (j % (value * k) == 0) { + count++ + } + } + } + return count + } +} +``` \ No newline at end of file