Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@
| 0977 |[Squares of a Sorted Array](src/main/kotlin/g0901_1000/s0977_squares_of_a_sorted_array)| Easy | Array, Sorting, Two_Pointers | 271 | 77.17
| 0026 |[Remove Duplicates from Sorted Array](src/main/kotlin/g0001_0100/s0026_remove_duplicates_from_sorted_array)| Easy | Top_Interview_Questions, Array, Two_Pointers | 249 | 67.38
| 0042 |[Trapping Rain Water](src/main/kotlin/g0001_0100/s0042_trapping_rain_water)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(1) | 189 | 99.37
| 0015 |[3Sum](src/main/kotlin/g0001_0100/s0015_3sum)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n^2)_Space_O(1) | 493 | 93.45
| 0015 |[3Sum](src/main/kotlin/g0001_0100/s0015_3sum)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 493 | 93.45

#### Udemy Famous Algorithm

Expand Down Expand Up @@ -1406,7 +1406,7 @@
|-|-|-|-|-|-
| 0136 |[Single Number](src/main/kotlin/g0101_0200/s0136_single_number)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 344 | 83.63
| 0169 |[Majority Element](src/main/kotlin/g0101_0200/s0169_majority_element)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 460 | 51.25
| 0015 |[3Sum](src/main/kotlin/g0001_0100/s0015_3sum)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n^2)_Space_O(1) | 493 | 93.45
| 0015 |[3Sum](src/main/kotlin/g0001_0100/s0015_3sum)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 493 | 93.45

#### Day 2 Array

Expand Down Expand Up @@ -1680,7 +1680,7 @@
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0082 |[Remove Duplicates from Sorted List II](src/main/kotlin/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii)| Medium | Two_Pointers, Linked_List | 166 | 89.47
| 0015 |[3Sum](src/main/kotlin/g0001_0100/s0015_3sum)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n^2)_Space_O(1) | 493 | 93.45
| 0015 |[3Sum](src/main/kotlin/g0001_0100/s0015_3sum)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 493 | 93.45

#### Day 4 Two Pointers

Expand Down Expand Up @@ -1816,6 +1816,45 @@

| # | Title | Difficulty | Tag | Time, ms | Time, %
|------|----------------|-------------|-------------|----------|--------
| 2900 |[Longest Unequal Adjacent Groups Subsequence I](src/main/kotlin/g2801_2900/s2900_longest_unequal_adjacent_groups_subsequence_i)| Medium | Array, String, Dynamic_Programming, Greedy | 294 | 100.00
| 2899 |[Last Visited Integers](src/main/kotlin/g2801_2900/s2899_last_visited_integers)| Easy | Array, String, Simulation | 209 | 72.73
| 2897 |[Apply Operations on Array to Maximize Sum of Squares](src/main/kotlin/g2801_2900/s2897_apply_operations_on_array_to_maximize_sum_of_squares)| Hard | Array, Hash_Table, Greedy, Bit_Manipulation | 572 | 100.00
| 2896 |[Apply Operations to Make Two Strings Equal](src/main/kotlin/g2801_2900/s2896_apply_operations_to_make_two_strings_equal)| Medium | String, Dynamic_Programming | 181 | 66.67
| 2895 |[Minimum Processing Time](src/main/kotlin/g2801_2900/s2895_minimum_processing_time)| Medium | Array, Sorting, Greedy | 662 | 80.00
| 2894 |[Divisible and Non-divisible Sums Difference](src/main/kotlin/g2801_2900/s2894_divisible_and_non_divisible_sums_difference)| Easy | Math | 135 | 76.92
| 2891 |[Method Chaining](src/main/kotlin/g2801_2900/s2891_method_chaining)| Easy || 412 | 99.23
| 2890 |[Reshape Data: Melt](src/main/kotlin/g2801_2900/s2890_reshape_data_melt)| Easy || 446 | 97.89
| 2889 |[Reshape Data: Pivot](src/main/kotlin/g2801_2900/s2889_reshape_data_pivot)| Easy || 416 | 99.87
| 2888 |[Reshape Data: Concatenate](src/main/kotlin/g2801_2900/s2888_reshape_data_concatenate)| Easy || 441 | 96.26
| 2887 |[Fill Missing Data](src/main/kotlin/g2801_2900/s2887_fill_missing_data)| Easy || 404 | 97.11
| 2886 |[Change Data Type](src/main/kotlin/g2801_2900/s2886_change_data_type)| Easy || 421 | 94.57
| 2885 |[Rename Columns](src/main/kotlin/g2801_2900/s2885_rename_columns)| Easy || 467 | 68.13
| 2884 |[Modify Columns](src/main/kotlin/g2801_2900/s2884_modify_columns)| Easy || 401 | 96.35
| 2883 |[Drop Missing Data](src/main/kotlin/g2801_2900/s2883_drop_missing_data)| Easy || 429 | 94.97
| 2882 |[Drop Duplicate Rows](src/main/kotlin/g2801_2900/s2882_drop_duplicate_rows)| Easy || 405 | 97.36
| 2881 |[Create a New Column](src/main/kotlin/g2801_2900/s2881_create_a_new_column)| Easy || 437 | 81.98
| 2880 |[Select Data](src/main/kotlin/g2801_2900/s2880_select_data)| Easy || 428 | 94.99
| 2879 |[Display the First Three Rows](src/main/kotlin/g2801_2900/s2879_display_the_first_three_rows)| Easy || 406 | 96.44
| 2878 |[Get the Size of a DataFrame](src/main/kotlin/g2801_2900/s2878_get_the_size_of_a_dataframe)| Easy || 413 | 94.68
| 2877 |[Create a DataFrame from List](src/main/kotlin/g2801_2900/s2877_create_a_dataframe_from_list)| Easy || 406 | 82.57
| 2876 |[Count Visited Nodes in a Directed Graph](src/main/kotlin/g2801_2900/s2876_count_visited_nodes_in_a_directed_graph)| Hard | Dynamic_Programming, Graph, Memoization | 922 | 100.00
| 2875 |[Minimum Size Subarray in Infinite Array](src/main/kotlin/g2801_2900/s2875_minimum_size_subarray_in_infinite_array)| Medium | Array, Hash_Table, Prefix_Sum, Sliding_Window | 372 | 100.00
| 2874 |[Maximum Value of an Ordered Triplet II](src/main/kotlin/g2801_2900/s2874_maximum_value_of_an_ordered_triplet_ii)| Medium | Array | 508 | 100.00
| 2873 |[Maximum Value of an Ordered Triplet I](src/main/kotlin/g2801_2900/s2873_maximum_value_of_an_ordered_triplet_i)| Easy | Array | 157 | 94.12
| 2872 |[Maximum Number of K-Divisible Components](src/main/kotlin/g2801_2900/s2872_maximum_number_of_k_divisible_components)| Hard | Dynamic_Programming, Depth_First_Search, Tree | 780 | 100.00
| 2871 |[Split Array Into Maximum Number of Subarrays](src/main/kotlin/g2801_2900/s2871_split_array_into_maximum_number_of_subarrays)| Medium | Array, Greedy, Bit_Manipulation | 490 | 100.00
| 2870 |[Minimum Number of Operations to Make Array Empty](src/main/kotlin/g2801_2900/s2870_minimum_number_of_operations_to_make_array_empty)| Medium | Array, Hash_Table, Greedy, Counting | 503 | 80.00
| 2869 |[Minimum Operations to Collect Elements](src/main/kotlin/g2801_2900/s2869_minimum_operations_to_collect_elements)| Easy | Array, Hash_Table | 156 | 100.00
| 2867 |[Count Valid Paths in a Tree](src/main/kotlin/g2801_2900/s2867_count_valid_paths_in_a_tree)| Hard | Dynamic_Programming, Math, Depth_First_Search, Tree, Number_Theory | 793 | 100.00
| 2866 |[Beautiful Towers II](src/main/kotlin/g2801_2900/s2866_beautiful_towers_ii)| Medium | Array, Stack, Monotonic_Stack | 676 | 85.71
| 2865 |[Beautiful Towers I](src/main/kotlin/g2801_2900/s2865_beautiful_towers_i)| Medium | Array, Stack, Monotonic_Stack | 232 | 75.00
| 2864 |[Maximum Odd Binary Number](src/main/kotlin/g2801_2900/s2864_maximum_odd_binary_number)| Easy | String, Math, Greedy | 176 | 90.91
| 2862 |[Maximum Element-Sum of a Complete Subset of Indices](src/main/kotlin/g2801_2900/s2862_maximum_element_sum_of_a_complete_subset_of_indices)| Hard | Array, Math, Number_Theory | 259 | 100.00
| 2861 |[Maximum Number of Alloys](src/main/kotlin/g2801_2900/s2861_maximum_number_of_alloys)| Medium | Array, Binary_Search | 289 | 100.00
| 2860 |[Happy Students](src/main/kotlin/g2801_2900/s2860_happy_students)| Medium | Array, Sorting, Enumeration | 512 | 100.00
| 2859 |[Sum of Values at Indices With K Set Bits](src/main/kotlin/g2801_2900/s2859_sum_of_values_at_indices_with_k_set_bits)| Easy | Array, Bit_Manipulation | 177 | 100.00
| 2858 |[Minimum Edge Reversals So Every Node Is Reachable](src/main/kotlin/g2801_2900/s2858_minimum_edge_reversals_so_every_node_is_reachable)| Hard | Dynamic_Programming, Depth_First_Search, Breadth_First_Search, Graph | 1161 | 100.00
| 2857 |[Count Pairs of Points With Distance k](src/main/kotlin/g2801_2900/s2857_count_pairs_of_points_with_distance_k)| Medium | Array, Hash_Table, Bit_Manipulation | 1212 | 100.00
| 2856 |[Minimum Array Length After Pair Removals](src/main/kotlin/g2801_2900/s2856_minimum_array_length_after_pair_removals)| Medium | Array, Hash_Table, Greedy, Binary_Search, Two_Pointers, Counting | 647 | 100.00
| 2855 |[Minimum Right Shifts to Sort the Array](src/main/kotlin/g2801_2900/s2855_minimum_right_shifts_to_sort_the_array)| Easy | Array | 169 | 75.00
| 2851 |[String Transformation](src/main/kotlin/g2801_2900/s2851_string_transformation)| Hard | String, Dynamic_Programming, Math, String_Matching | 377 | 100.00
Expand Down Expand Up @@ -4085,7 +4124,7 @@
| 0018 |[4Sum](src/main/kotlin/g0001_0100/s0018_4sum)| Medium | Array, Sorting, Two_Pointers | 229 | 98.59
| 0017 |[Letter Combinations of a Phone Number](src/main/kotlin/g0001_0100/s0017_letter_combinations_of_a_phone_number)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking, Algorithm_II_Day_11_Recursion_Backtracking, Udemy_Backtracking/Recursion, Big_O_Time_O(4^n)_Space_O(n) | 155 | 95.24
| 0016 |[3Sum Closest](src/main/kotlin/g0001_0100/s0016_3sum_closest)| Medium | Array, Sorting, Two_Pointers, Level_2_Day_14_Sliding_Window/Two_Pointer | 163 | 100.00
| 0015 |[3Sum](src/main/kotlin/g0001_0100/s0015_3sum)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Data_Structure_II_Day_1_Array, Algorithm_II_Day_3_Two_Pointers, Udemy_Two_Pointers, Big_O_Time_O(n^2)_Space_O(1) | 493 | 93.45
| 0015 |[3Sum](src/main/kotlin/g0001_0100/s0015_3sum)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Data_Structure_II_Day_1_Array, Algorithm_II_Day_3_Two_Pointers, Udemy_Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 493 | 93.45
| 0014 |[Longest Common Prefix](src/main/kotlin/g0001_0100/s0014_longest_common_prefix)| Easy | Top_Interview_Questions, String, Level_2_Day_2_String, Udemy_Strings | 154 | 89.70
| 0013 |[Roman to Integer](src/main/kotlin/g0001_0100/s0013_roman_to_integer)| Easy | Top_Interview_Questions, String, Hash_Table, Math | 211 | 83.47
| 0012 |[Integer to Roman](src/main/kotlin/g0001_0100/s0012_integer_to_roman)| Medium | String, Hash_Table, Math | 180 | 98.25
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import java.util.PriorityQueue
import kotlin.collections.HashMap
import kotlin.math.max

@Suppress("NAME_SHADOWING")
class Solution {
fun maxSum(nums: IntArray): Int {
// what we'll return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Return _the number of beautiful integers in the range_ `[low, high]`.
```kotlin
import kotlin.math.max

@Suppress("kotlin:S107")
class Solution {
private lateinit var dp: Array<Array<Array<Array<IntArray>>>>
private var maxLength = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Return _the **minimum** possible sum of a k-avoiding array of length_ `n`.
## Solution

```kotlin
@Suppress("NAME_SHADOWING")
class Solution {
fun minimumSum(n: Int, k: Int): Int {
var k = k
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Solution {
if (sum < target) {
return -1
}
while (!pq.isEmpty()) {
while (pq.isNotEmpty()) {
val `val` = pq.poll()
sum -= `val`.toLong()
if (`val` <= target) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ There are 2 k-subsequences that have the maximum beauty, 5. Hence, the answer is
## Solution

```kotlin
@Suppress("NAME_SHADOWING")
class Solution {
fun countKSubsequencesWithMaxBeauty(s: String, k: Int): Int {
var k = k
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ In the fourth query, we change the weights of edges [0,7], [0,3] and [1,3] to 6.
import kotlin.math.ln
import kotlin.math.max

@Suppress("kotlin:S107")
class Solution {
private class Node(var v: Int, var w: Int)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Now nums is sorted; therefore the answer is 2.
## Solution

```kotlin
@Suppress("kotlin:S6510")
class Solution {
fun minimumRightShifts(nums: List<Int>): Int {
var i = 1
Expand Down
Original file line number Diff line number Diff line change
@@ -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)

## 2857\. Count Pairs of Points With Distance k

Medium

You are given a **2D** integer array `coordinates` and an integer `k`, where <code>coordinates[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> are the coordinates of the <code>i<sup>th</sup></code> point in a 2D plane.

We define the **distance** between two points <code>(x<sub>1</sub>, y<sub>1</sub>)</code> and <code>(x<sub>2</sub>, y<sub>2</sub>)</code> as `(x1 XOR x2) + (y1 XOR y2)` where `XOR` is the bitwise `XOR` operation.

Return _the number of pairs_ `(i, j)` _such that_ `i < j` _and the distance between points_ `i` _and_ `j` _is equal to_ `k`.

**Example 1:**

**Input:** coordinates = \[\[1,2],[4,2],[1,3],[5,2]], k = 5

**Output:** 2

**Explanation:** We can choose the following pairs:
- (0,1): Because we have (1 XOR 4) + (2 XOR 2) = 5.
- (2,3): Because we have (1 XOR 5) + (3 XOR 2) = 5.

**Example 2:**

**Input:** coordinates = \[\[1,3],[1,3],[1,3],[1,3],[1,3]], k = 0

**Output:** 10

**Explanation:** Any two chosen pairs will have a distance of 0. There are 10 ways to choose two pairs.

**Constraints:**

* `2 <= coordinates.length <= 50000`
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>6</sup></code>
* `0 <= k <= 100`

## Solution

```kotlin
class Solution {
fun countPairs(coordinates: List<List<Int>>, k: Int): Int {
var ans = 0
val map: MutableMap<Long, Int> = HashMap()
for (p in coordinates) {
val p0 = p[0]
val p1 = p[1]
for (i in 0..k) {
val x1 = i xor p0
val y1 = (k - i) xor p1
val key2 = hash(x1, y1)
if (map.containsKey(key2)) {
ans += map[key2]!!
}
}
val key = hash(p0, p1)
map[key] = map.getOrDefault(key, 0) + 1
}
return ans
}

private fun hash(x1: Int, y1: Int): Long {
val r = 1e8.toLong()
return x1 * r + y1
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
[![](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)

## 2858\. Minimum Edge Reversals So Every Node Is Reachable

Hard

There is a **simple directed graph** with `n` nodes labeled from `0` to `n - 1`. The graph would form a **tree** if its edges were bi-directional.

You are given an integer `n` and a **2D** integer array `edges`, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> represents a **directed edge** going from node <code>u<sub>i</sub></code> to node <code>v<sub>i</sub></code>.

An **edge reversal** changes the direction of an edge, i.e., a directed edge going from node <code>u<sub>i</sub></code> to node <code>v<sub>i</sub></code> becomes a directed edge going from node <code>v<sub>i</sub></code> to node <code>u<sub>i</sub></code>.

For every node `i` in the range `[0, n - 1]`, your task is to **independently** calculate the **minimum** number of **edge reversals** required so it is possible to reach any other node starting from node `i` through a **sequence** of **directed edges**.

Return _an integer array_ `answer`_, where_ `answer[i]` _is the_ _**minimum** number of **edge reversals** required so it is possible to reach any other node starting from node_ `i` _through a **sequence** of **directed edges**._

**Example 1:**

![](https://assets.leetcode.com/uploads/2023/08/26/image-20230826221104-3.png)

**Input:** n = 4, edges = \[\[2,0],[2,1],[1,3]]

**Output:** [1,1,0,2]

**Explanation:** The image above shows the graph formed by the edges.

For node 0: after reversing the edge [2,0], it is possible to reach any other node starting from node 0.

So, answer[0] = 1.

For node 1: after reversing the edge [2,1], it is possible to reach any other node starting from node 1.

So, answer[1] = 1.

For node 2: it is already possible to reach any other node starting from node 2.

So, answer[2] = 0.

For node 3: after reversing the edges [1,3] and [2,1], it is possible to reach any other node starting from node 3.

So, answer[3] = 2.

**Example 2:**

![](https://assets.leetcode.com/uploads/2023/08/26/image-20230826225541-2.png)

**Input:** n = 3, edges = \[\[1,2],[2,0]]

**Output:** [2,0,1]

**Explanation:** The image above shows the graph formed by the edges.

For node 0: after reversing the edges [2,0] and [1,2], it is possible to reach any other node starting from node 0.

So, answer[0] = 2.

For node 1: it is already possible to reach any other node starting from node 1.

So, answer[1] = 0.

For node 2: after reversing the edge [1, 2], it is possible to reach any other node starting from node 2.

So, answer[2] = 1.

**Constraints:**

* <code>2 <= n <= 10<sup>5</sup></code>
* `edges.length == n - 1`
* `edges[i].length == 2`
* <code>0 <= u<sub>i</sub> == edges[i][0] < n</code>
* <code>0 <= v<sub>i</sub> == edges[i][1] < n</code>
* <code>u<sub>i</sub> != v<sub>i</sub></code>
* The input is generated such that if the edges were bi-directional, the graph would be a tree.

## Solution

```kotlin
import java.util.LinkedList
import java.util.Queue

class Solution {
fun minEdgeReversals(n: Int, edges: Array<IntArray>): IntArray {
val nexts: Array<MutableList<IntArray>> = Array(n) { ArrayList() }
for (edge in edges) {
val u = edge[0]
val v = edge[1]
nexts[u].add(intArrayOf(1, v))
nexts[v].add(intArrayOf(-1, u))
}
val res = IntArray(n)
for (i in 0 until n) {
res[i] = -1
}
res[0] = dfs(nexts, 0, -1)
val queue: Queue<Int> = LinkedList()
queue.add(0)
while (queue.isNotEmpty()) {
val index = queue.remove()
val `val` = res[index]
val next: List<IntArray> = nexts[index]
for (node in next) {
if (res[node[1]] == -1) {
if (node[0] == 1) {
res[node[1]] = `val` + 1
} else {
res[node[1]] = `val` - 1
}
queue.add(node[1])
}
}
}
return res
}

private fun dfs(nexts: Array<MutableList<IntArray>>, index: Int, pre: Int): Int {
var res = 0
val next: List<IntArray> = nexts[index]
for (node in next) {
if (node[1] != pre) {
if (node[0] == -1) {
res++
}
res += dfs(nexts, node[1], index)
}
}
return res
}
}
```
Loading