Skip to content

Commit ac9c01a

Browse files
authored
Added tasks 2091-2100
1 parent ac602ca commit ac9c01a

File tree

24 files changed

+1070
-0
lines changed

24 files changed

+1070
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package g2001_2100.s2091_removing_minimum_and_maximum_from_array
2+
3+
// #Medium #Array #Greedy #2023_06_28_Time_607_ms_(100.00%)_Space_57.3_MB_(100.00%)
4+
5+
class Solution {
6+
fun minimumDeletions(nums: IntArray): Int {
7+
val n = nums.size
8+
var min = Int.MAX_VALUE
9+
var max = Int.MIN_VALUE
10+
var minIndex = 0
11+
var maxIndex = 0
12+
for (i in nums.indices) {
13+
if (nums[i] < min) {
14+
min = nums[i]
15+
minIndex = i
16+
}
17+
if (nums[i] > max) {
18+
max = nums[i]
19+
maxIndex = i
20+
}
21+
}
22+
val firstCase: Int
23+
val secondCase: Int
24+
val thirdCase: Int
25+
if (minIndex > maxIndex) {
26+
firstCase = minIndex + 1
27+
secondCase = n - maxIndex
28+
thirdCase = maxIndex + 1 + (n - minIndex)
29+
} else {
30+
firstCase = maxIndex + 1
31+
secondCase = n - minIndex
32+
thirdCase = minIndex + 1 + (n - maxIndex)
33+
}
34+
return firstCase.coerceAtMost(secondCase.coerceAtMost(thirdCase))
35+
}
36+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2091\. Removing Minimum and Maximum From Array
2+
3+
Medium
4+
5+
You are given a **0-indexed** array of **distinct** integers `nums`.
6+
7+
There is an element in `nums` that has the **lowest** value and an element that has the **highest** value. We call them the **minimum** and **maximum** respectively. Your goal is to remove **both** these elements from the array.
8+
9+
A **deletion** is defined as either removing an element from the **front** of the array or removing an element from the **back** of the array.
10+
11+
Return _the **minimum** number of deletions it would take to remove **both** the minimum and maximum element from the array._
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [2,**10**,7,5,4,**1**,8,6]
16+
17+
**Output:** 5
18+
19+
**Explanation:**
20+
21+
The minimum element in the array is nums[5], which is 1.
22+
23+
The maximum element in the array is nums[1], which is 10.
24+
25+
We can remove both the minimum and maximum by removing 2 elements from the front and 3 elements from the back.
26+
27+
This results in 2 + 3 = 5 deletions, which is the minimum number possible.
28+
29+
**Example 2:**
30+
31+
**Input:** nums = [0,**\-4**,**19**,1,8,-2,-3,5]
32+
33+
**Output:** 3
34+
35+
**Explanation:**
36+
37+
The minimum element in the array is nums[1], which is -4.
38+
39+
The maximum element in the array is nums[2], which is 19.
40+
41+
We can remove both the minimum and maximum by removing 3 elements from the front.
42+
43+
This results in only 3 deletions, which is the minimum number possible.
44+
45+
**Example 3:**
46+
47+
**Input:** nums = [**101**]
48+
49+
**Output:** 1
50+
51+
**Explanation:**
52+
53+
There is only one element in the array, which makes it both the minimum and maximum element.
54+
55+
We can remove it with 1 deletion.
56+
57+
**Constraints:**
58+
59+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
60+
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
61+
* The integers in `nums` are **distinct**.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package g2001_2100.s2092_find_all_people_with_secret
2+
3+
// #Hard #Sorting #Depth_First_Search #Breadth_First_Search #Graph #Union_Find
4+
// #2023_06_28_Time_1086_ms_(100.00%)_Space_104.2_MB_(100.00%)
5+
6+
import java.util.Arrays
7+
8+
@Suppress("NAME_SHADOWING")
9+
class Solution {
10+
fun findAllPeople(n: Int, meetings: Array<IntArray>, firstPerson: Int): List<Int> {
11+
Arrays.sort(meetings) { a: IntArray, b: IntArray -> a[2] - b[2] }
12+
val uf = UF(n)
13+
// base
14+
uf.union(0, firstPerson)
15+
// for every time we have a pool of people that talk to each other
16+
// if someone knows a secret proir to this meeting - all pool will too
17+
// if not - reset unions from this pool
18+
var i = 0
19+
while (i < meetings.size) {
20+
val curTime = meetings[i][2]
21+
val pool: MutableSet<Int> = HashSet()
22+
while (i < meetings.size && curTime == meetings[i][2]) {
23+
val currentMeeting = meetings[i]
24+
uf.union(currentMeeting[0], currentMeeting[1])
25+
pool.add(currentMeeting[0])
26+
pool.add(currentMeeting[1])
27+
i++
28+
}
29+
// meeting that took place now should't affect future
30+
// meetings if people don't know the secret
31+
for (j in pool) {
32+
if (!uf.connected(0, j)) {
33+
uf.reset(j)
34+
}
35+
}
36+
}
37+
// if the person is conneted to 0 - they know a secret
38+
val ans: MutableList<Int> = ArrayList()
39+
for (j in 0 until n) {
40+
if (uf.connected(j, 0)) {
41+
ans.add(j)
42+
}
43+
}
44+
return ans
45+
}
46+
47+
// regular union find
48+
private class UF(n: Int) {
49+
private val parent: IntArray
50+
private val rank: IntArray
51+
52+
init {
53+
parent = IntArray(n)
54+
rank = IntArray(n)
55+
for (i in 0 until n) {
56+
parent[i] = i
57+
}
58+
}
59+
60+
fun union(p: Int, q: Int) {
61+
val rootP = find(p)
62+
val rootQ = find(q)
63+
if (rootP == rootQ) {
64+
return
65+
}
66+
if (rank[rootP] < rank[rootQ]) {
67+
parent[rootP] = rootQ
68+
} else {
69+
parent[rootQ] = rootP
70+
rank[rootP]++
71+
}
72+
}
73+
74+
fun find(p: Int): Int {
75+
var p = p
76+
while (parent[p] != p) {
77+
p = parent[parent[p]]
78+
}
79+
return p
80+
}
81+
82+
fun connected(p: Int, q: Int): Boolean {
83+
return find(p) == find(q)
84+
}
85+
86+
fun reset(p: Int) {
87+
parent[p] = p
88+
rank[p] = 0
89+
}
90+
}
91+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2092\. Find All People With Secret
2+
3+
Hard
4+
5+
You are given an integer `n` indicating there are `n` people numbered from `0` to `n - 1`. You are also given a **0-indexed** 2D integer array `meetings` where <code>meetings[i] = [x<sub>i</sub>, y<sub>i</sub>, time<sub>i</sub>]</code> indicates that person <code>x<sub>i</sub></code> and person <code>y<sub>i</sub></code> have a meeting at <code>time<sub>i</sub></code>. A person may attend **multiple meetings** at the same time. Finally, you are given an integer `firstPerson`.
6+
7+
Person `0` has a **secret** and initially shares the secret with a person `firstPerson` at time `0`. This secret is then shared every time a meeting takes place with a person that has the secret. More formally, for every meeting, if a person <code>x<sub>i</sub></code> has the secret at <code>time<sub>i</sub></code>, then they will share the secret with person <code>y<sub>i</sub></code>, and vice versa.
8+
9+
The secrets are shared **instantaneously**. That is, a person may receive the secret and share it with people in other meetings within the same time frame.
10+
11+
Return _a list of all the people that have the secret after all the meetings have taken place._ You may return the answer in **any order**.
12+
13+
**Example 1:**
14+
15+
**Input:** n = 6, meetings = [[1,2,5],[2,3,8],[1,5,10]], firstPerson = 1
16+
17+
**Output:** [0,1,2,3,5]
18+
19+
**Explanation:**
20+
21+
At time 0, person 0 shares the secret with person 1.
22+
23+
At time 5, person 1 shares the secret with person 2.
24+
25+
At time 8, person 2 shares the secret with person 3.
26+
27+
At time 10, person 1 shares the secret with person 5.
28+
29+
Thus, people 0, 1, 2, 3, and 5 know the secret after all the meetings.
30+
31+
**Example 2:**
32+
33+
**Input:** n = 4, meetings = [[3,1,3],[1,2,2],[0,3,3]], firstPerson = 3
34+
35+
**Output:** [0,1,3]
36+
37+
**Explanation:**
38+
39+
At time 0, person 0 shares the secret with person 3.
40+
41+
At time 2, neither person 1 nor person 2 know the secret.
42+
43+
At time 3, person 3 shares the secret with person 0 and person 1.
44+
45+
Thus, people 0, 1, and 3 know the secret after all the meetings.
46+
47+
**Example 3:**
48+
49+
**Input:** n = 5, meetings = [[3,4,2],[1,2,1],[2,3,1]], firstPerson = 1
50+
51+
**Output:** [0,1,2,3,4]
52+
53+
**Explanation:**
54+
55+
At time 0, person 0 shares the secret with person 1.
56+
57+
At time 1, person 1 shares the secret with person 2, and person 2 shares the secret with person 3.
58+
59+
Note that person 2 can share the secret at the same time as receiving it.
60+
61+
At time 2, person 3 shares the secret with person 4.
62+
63+
Thus, people 0, 1, 2, 3, and 4 know the secret after all the meetings.
64+
65+
**Constraints:**
66+
67+
* <code>2 <= n <= 10<sup>5</sup></code>
68+
* <code>1 <= meetings.length <= 10<sup>5</sup></code>
69+
* `meetings[i].length == 3`
70+
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> <= n - 1</code>
71+
* <code>x<sub>i</sub> != y<sub>i</sub></code>
72+
* <code>1 <= time<sub>i</sub> <= 10<sup>5</sup></code>
73+
* `1 <= firstPerson <= n - 1`
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g2001_2100.s2094_finding_3_digit_even_numbers
2+
3+
// #Easy #Array #Hash_Table #Sorting #Enumeration
4+
// #2023_06_28_Time_181_ms_(100.00%)_Space_37.4_MB_(100.00%)
5+
6+
@Suppress("NAME_SHADOWING")
7+
class Solution {
8+
fun findEvenNumbers(digits: IntArray): IntArray {
9+
val idx = IntArray(1)
10+
val result = IntArray(9 * 10 * 5)
11+
val digitMap = IntArray(10)
12+
for (digit in digits) {
13+
digitMap[digit]++
14+
}
15+
dfs(result, digitMap, idx, 0)
16+
return result.copyOfRange(0, idx[0])
17+
}
18+
19+
private fun dfs(result: IntArray, digitMap: IntArray, idx: IntArray, `val`: Int) {
20+
var `val` = `val`
21+
if (`val` > 99) {
22+
result[idx[0]++] = `val`
23+
return
24+
}
25+
`val` *= 10
26+
for (i in 0..9) {
27+
if (digitMap[i] == 0 || `val` == 0 && i == 0 || `val` > 99 && i and 1 == 1) {
28+
continue
29+
}
30+
digitMap[i]--
31+
`val` += i
32+
dfs(result, digitMap, idx, `val`)
33+
`val` -= i
34+
digitMap[i]++
35+
}
36+
}
37+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2094\. Finding 3-Digit Even Numbers
2+
3+
Easy
4+
5+
You are given an integer array `digits`, where each element is a digit. The array may contain duplicates.
6+
7+
You need to find **all** the **unique** integers that follow the given requirements:
8+
9+
* The integer consists of the **concatenation** of **three** elements from `digits` in **any** arbitrary order.
10+
* The integer does not have **leading zeros**.
11+
* The integer is **even**.
12+
13+
For example, if the given `digits` were `[1, 2, 3]`, integers `132` and `312` follow the requirements.
14+
15+
Return _a **sorted** array of the unique integers._
16+
17+
**Example 1:**
18+
19+
**Input:** digits = [2,1,3,0]
20+
21+
**Output:** [102,120,130,132,210,230,302,310,312,320]
22+
23+
**Explanation:** All the possible integers that follow the requirements are in the output array.
24+
25+
Notice that there are no **odd** integers or integers with **leading zeros**.
26+
27+
**Example 2:**
28+
29+
**Input:** digits = [2,2,8,8,2]
30+
31+
**Output:** [222,228,282,288,822,828,882]
32+
33+
**Explanation:** The same digit can be used as many times as it appears in digits.
34+
35+
In this example, the digit 8 is used twice each time in 288, 828, and 882.
36+
37+
**Example 3:**
38+
39+
**Input:** digits = [3,7,5]
40+
41+
**Output:** []
42+
43+
**Explanation:** No **even** integers can be formed using the given digits.
44+
45+
**Constraints:**
46+
47+
* `3 <= digits.length <= 100`
48+
* `0 <= digits[i] <= 9`
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package g2001_2100.s2095_delete_the_middle_node_of_a_linked_list
2+
3+
// #Medium #Two_Pointers #Linked_List #2023_06_28_Time_1115_ms_(50.00%)_Space_61.4_MB_(81.25%)
4+
5+
import com_github_leetcode.ListNode
6+
7+
/*
8+
* Example:
9+
* var li = ListNode(5)
10+
* var v = li.`val`
11+
* Definition for singly-linked list.
12+
* class ListNode(var `val`: Int) {
13+
* var next: ListNode? = null
14+
* }
15+
*/
16+
class Solution {
17+
fun deleteMiddle(head: ListNode?): ListNode? {
18+
var slow = head
19+
var fast = head
20+
var prev: ListNode? = null
21+
while (fast?.next != null) {
22+
prev = slow
23+
24+
slow = slow?.next
25+
fast = fast.next?.next
26+
}
27+
if (slow == head) {
28+
return null
29+
}
30+
prev?.next = slow?.next
31+
return head
32+
}
33+
}

0 commit comments

Comments
 (0)