forked from javadev/LeetCode-in-Kotlin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
570 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice/Solution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package g3101_3200.s3158_find_the_xor_of_numbers_which_appear_twice | ||
|
||
// #Easy #Array #Hash_Table #Bit_Manipulation | ||
// #2024_05_30_Time_166_ms_(92.21%)_Space_36.5_MB_(76.62%) | ||
|
||
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 | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...in/kotlin/g3101_3200/s3158_find_the_xor_of_numbers_which_appear_twice/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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. |
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array/Solution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package g3101_3200.s3159_find_occurrences_of_an_element_in_an_array | ||
|
||
// #Medium #Array #Hash_Table #2024_05_30_Time_810_ms_(98.28%)_Space_66.3_MB_(81.03%) | ||
|
||
class Solution { | ||
fun occurrencesOfElement(nums: IntArray, queries: IntArray, x: Int): IntArray { | ||
val a = ArrayList<Int>() | ||
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 | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...in/kotlin/g3101_3200/s3159_find_occurrences_of_an_element_in_an_array/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
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 <code>queries[i]<sup>th</sup></code> 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 1<sup>st</sup> query, the first occurrence of 1 is at index 0. | ||
* For the 2<sup>nd</sup> query, there are only two occurrences of 1 in `nums`, so the answer is -1. | ||
* For the 3<sup>rd</sup> query, the second occurrence of 1 is at index 2. | ||
* For the 4<sup>th</sup> 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 1<sup>st</sup> query, 5 doesn't exist in `nums`, so the answer is -1. | ||
|
||
**Constraints:** | ||
|
||
* <code>1 <= nums.length, queries.length <= 10<sup>5</sup></code> | ||
* <code>1 <= queries[i] <= 10<sup>5</sup></code> | ||
* <code>1 <= nums[i], x <= 10<sup>4</sup></code> |
30 changes: 30 additions & 0 deletions
30
...in/kotlin/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls/Solution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package g3101_3200.s3160_find_the_number_of_distinct_colors_among_the_balls | ||
|
||
// #Medium #Array #Hash_Table #Simulation #2024_05_30_Time_1055_ms_(58.82%)_Space_101.1_MB_(86.27%) | ||
|
||
class Solution { | ||
fun queryResults(ignoredLimit: Int, queries: Array<IntArray>): IntArray { | ||
val ballToColor: MutableMap<Int, Int> = HashMap() | ||
val colorToCnt: MutableMap<Int, Int> = 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 | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...n/g3101_3200/s3160_find_the_number_of_distinct_colors_among_the_balls/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
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_ <code>i<sup>th</sup></code> 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:** | ||
|
||
* <code>1 <= limit <= 10<sup>9</sup></code> | ||
* <code>1 <= n == queries.length <= 10<sup>5</sup></code> | ||
* `queries[i].length == 2` | ||
* `0 <= queries[i][0] <= limit` | ||
* <code>1 <= queries[i][1] <= 10<sup>9</sup></code> |
104 changes: 104 additions & 0 deletions
104
src/main/kotlin/g3101_3200/s3161_block_placement_queries/Solution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package g3101_3200.s3161_block_placement_queries | ||
|
||
// #Hard #Array #Binary_Search #Segment_Tree #Binary_Indexed_Tree | ||
// #2024_05_30_Time_1701_ms_(100.00%)_Space_174.7_MB_(33.33%) | ||
|
||
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<IntArray>): List<Boolean> { | ||
var max = 0 | ||
for (i in queries) { | ||
max = max(max, i[1]) | ||
} | ||
val root = Seg.init(max) | ||
root.set(0) | ||
|
||
val res: MutableList<Boolean> = 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 | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/kotlin/g3101_3200/s3161_block_placement_queries/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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 <code>i<sup>th</sup></code> 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:** | ||
|
||
* <code>1 <= queries.length <= 15 * 10<sup>4</sup></code> | ||
* `2 <= queries[i].length <= 3` | ||
* `1 <= queries[i][0] <= 2` | ||
* <code>1 <= x, sz <= min(5 * 10<sup>4</sup>, 3 * queries.length)</code> | ||
* 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. |
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/g3101_3200/s3162_find_the_number_of_good_pairs_i/Solution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package g3101_3200.s3162_find_the_number_of_good_pairs_i | ||
|
||
// #Easy #Array #Hash_Table #2024_05_30_Time_182_ms_(54.41%)_Space_36.1_MB_(94.12%) | ||
|
||
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 | ||
} | ||
} |
Oops, something went wrong.