Skip to content

Commit e663617

Browse files
authored
Added tasks 2874-2894
1 parent 4fa4be1 commit e663617

File tree

44 files changed

+1177
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1177
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.20'
11861186
| 0977 |[Squares of a Sorted Array](src/main/kotlin/g0901_1000/s0977_squares_of_a_sorted_array/Solution.kt)| Easy | Array, Sorting, Two_Pointers | 271 | 77.17
11871187
| 0026 |[Remove Duplicates from Sorted Array](src/main/kotlin/g0001_0100/s0026_remove_duplicates_from_sorted_array/Solution.kt)| Easy | Top_Interview_Questions, Array, Two_Pointers | 249 | 67.38
11881188
| 0042 |[Trapping Rain Water](src/main/kotlin/g0001_0100/s0042_trapping_rain_water/Solution.kt)| 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
1189-
| 0015 |[3Sum](src/main/kotlin/g0001_0100/s0015_3sum/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n^2)_Space_O(1) | 493 | 93.45
1189+
| 0015 |[3Sum](src/main/kotlin/g0001_0100/s0015_3sum/Solution.kt)| 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
11901190

11911191
#### Udemy Famous Algorithm
11921192

@@ -1438,7 +1438,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.20'
14381438
|-|-|-|-|-|-
14391439
| 0136 |[Single Number](src/main/kotlin/g0101_0200/s0136_single_number/Solution.kt)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 344 | 83.63
14401440
| 0169 |[Majority Element](src/main/kotlin/g0101_0200/s0169_majority_element/Solution.kt)| 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
1441-
| 0015 |[3Sum](src/main/kotlin/g0001_0100/s0015_3sum/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n^2)_Space_O(1) | 493 | 93.45
1441+
| 0015 |[3Sum](src/main/kotlin/g0001_0100/s0015_3sum/Solution.kt)| 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
14421442

14431443
#### Day 2 Array
14441444

@@ -1712,7 +1712,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.20'
17121712
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
17131713
|-|-|-|-|-|-
17141714
| 0082 |[Remove Duplicates from Sorted List II](src/main/kotlin/g0001_0100/s0082_remove_duplicates_from_sorted_list_ii/Solution.kt)| Medium | Two_Pointers, Linked_List | 166 | 89.47
1715-
| 0015 |[3Sum](src/main/kotlin/g0001_0100/s0015_3sum/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n^2)_Space_O(1) | 493 | 93.45
1715+
| 0015 |[3Sum](src/main/kotlin/g0001_0100/s0015_3sum/Solution.kt)| 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
17161716

17171717
#### Day 4 Two Pointers
17181718

src/main/kotlin/g2801_2900/s2815_max_pair_sum_in_an_array/Solution.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import java.util.PriorityQueue
66
import kotlin.collections.HashMap
77
import kotlin.math.max
88

9+
@Suppress("NAME_SHADOWING")
910
class Solution {
1011
fun maxSum(nums: IntArray): Int {
1112
// what we'll return
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g2801_2900.s2874_maximum_value_of_an_ordered_triplet_ii
2+
3+
// #Medium #Array #2023_12_25_Time_508_ms_(100.00%)_Space_63.7_MB_(50.00%)
4+
5+
import kotlin.math.max
6+
7+
class Solution {
8+
fun maximumTripletValue(nums: IntArray): Long {
9+
val diff = IntArray(nums.size)
10+
var tempMax = nums[0]
11+
for (i in 1 until diff.size - 1) {
12+
diff[i] = tempMax - nums[i]
13+
tempMax = max(tempMax.toDouble(), nums[i].toDouble()).toInt()
14+
}
15+
var max = Long.MIN_VALUE
16+
tempMax = nums[nums.size - 1]
17+
for (i in nums.size - 2 downTo 1) {
18+
max = max(max.toDouble(), (tempMax.toLong() * diff[i]).toDouble()).toLong()
19+
tempMax = max(tempMax.toDouble(), nums[i].toDouble()).toInt()
20+
}
21+
return max(max.toDouble(), 0.0).toLong()
22+
}
23+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2874\. Maximum Value of an Ordered Triplet II
2+
3+
Medium
4+
5+
You are given a **0-indexed** integer array `nums`.
6+
7+
Return _**the maximum value over all triplets of indices**_ `(i, j, k)` _such that_ `i < j < k`_._ If all such triplets have a negative value, return `0`.
8+
9+
The **value of a triplet of indices** `(i, j, k)` is equal to `(nums[i] - nums[j]) * nums[k]`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [12,6,1,2,7]
14+
15+
**Output:** 77
16+
17+
**Explanation:** The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) \* nums[4] = 77. It can be shown that there are no ordered triplets of indices with a value greater than 77.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [1,10,3,4,19]
22+
23+
**Output:** 133
24+
25+
**Explanation:** The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) \* nums[4] = 133. It can be shown that there are no ordered triplets of indices with a value greater than 133.
26+
27+
**Example 3:**
28+
29+
**Input:** nums = [1,2,3]
30+
31+
**Output:** 0
32+
33+
**Explanation:** The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) \* nums[2] = -3. Hence, the answer would be 0.
34+
35+
**Constraints:**
36+
37+
* <code>3 <= nums.length <= 10<sup>5</sup></code>
38+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g2801_2900.s2875_minimum_size_subarray_in_infinite_array
2+
3+
// #Medium #Array #Hash_Table #Prefix_Sum #Sliding_Window
4+
// #2023_12_25_Time_372_ms_(100.00%)_Space_52.6_MB_(100.00%)
5+
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun minSizeSubarray(nums: IntArray, target: Int): Int {
10+
var sum = 0
11+
for (num in nums) {
12+
sum += num
13+
}
14+
if (sum == 0) {
15+
return -1
16+
}
17+
val result = (target / sum) * nums.size
18+
sum = target % sum
19+
var currentSum = 0
20+
var min = nums.size
21+
var start = 0
22+
for (i in 0 until nums.size * 2) {
23+
currentSum += nums[i % nums.size]
24+
while (currentSum > sum) {
25+
currentSum -= nums[start % nums.size]
26+
start++
27+
}
28+
if (currentSum == sum) {
29+
min = min(min, i - start + 1)
30+
}
31+
}
32+
if (min == nums.size) {
33+
return -1
34+
}
35+
return result + min
36+
}
37+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2875\. Minimum Size Subarray in Infinite Array
2+
3+
Medium
4+
5+
You are given a **0-indexed** array `nums` and an integer `target`.
6+
7+
A **0-indexed** array `infinite_nums` is generated by infinitely appending the elements of `nums` to itself.
8+
9+
Return _the length of the **shortest** subarray of the array_ `infinite_nums` _with a sum equal to_ `target`_._ If there is no such subarray return `-1`.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,3], target = 5
14+
15+
**Output:** 2
16+
17+
**Explanation:** In this example infinite\_nums = [1,2,3,1,2,3,1,2,...]. The subarray in the range [1,2], has the sum equal to target = 5 and length = 2. It can be proven that 2 is the shortest length of a subarray with sum equal to target = 5.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [1,1,1,2,3], target = 4
22+
23+
**Output:** 2
24+
25+
**Explanation:** In this example infinite\_nums = [1,1,1,2,3,1,1,1,2,3,1,1,...]. The subarray in the range [4,5], has the sum equal to target = 4 and length = 2. It can be proven that 2 is the shortest length of a subarray with sum equal to target = 4.
26+
27+
**Example 3:**
28+
29+
**Input:** nums = [2,4,6,8], target = 3
30+
31+
**Output:** -1
32+
33+
**Explanation:** In this example infinite\_nums = [2,4,6,8,2,4,6,8,...]. It can be proven that there is no subarray with sum equal to target = 3.
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 <= target <= 10<sup>9</sup></code>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g2801_2900.s2876_count_visited_nodes_in_a_directed_graph
2+
3+
// #Hard #Dynamic_Programming #Graph #Memoization
4+
// #2023_12_25_Time_922_ms_(100.00%)_Space_76.6_MB_(100.00%)
5+
6+
class Solution {
7+
fun countVisitedNodes(edges: List<Int>): IntArray {
8+
val n = edges.size
9+
val visited = BooleanArray(n)
10+
val ans = IntArray(n)
11+
val level = IntArray(n)
12+
for (i in 0 until n) {
13+
if (!visited[i]) {
14+
visit(edges, 0, i, ans, visited, level)
15+
}
16+
}
17+
return ans
18+
}
19+
20+
private fun visit(
21+
edges: List<Int>,
22+
count: Int,
23+
curr: Int,
24+
ans: IntArray,
25+
visited: BooleanArray,
26+
level: IntArray
27+
): IntArray {
28+
if (ans[curr] != 0) {
29+
return intArrayOf(-1, ans[curr])
30+
}
31+
if (visited[curr]) {
32+
return intArrayOf(level[curr], count - level[curr])
33+
}
34+
level[curr] = count
35+
visited[curr] = true
36+
val ret = visit(edges, count + 1, edges[curr], ans, visited, level)
37+
if (ret[0] == -1 || count < ret[0]) {
38+
ret[1]++
39+
}
40+
ans[curr] = ret[1]
41+
return ret
42+
}
43+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2876\. Count Visited Nodes in a Directed Graph
2+
3+
Hard
4+
5+
There is a **directed** graph consisting of `n` nodes numbered from `0` to `n - 1` and `n` directed edges.
6+
7+
You are given a **0-indexed** array `edges` where `edges[i]` indicates that there is an edge from node `i` to node `edges[i]`.
8+
9+
Consider the following process on the graph:
10+
11+
* You start from a node `x` and keep visiting other nodes through edges until you reach a node that you have already visited before on this **same** process.
12+
13+
Return _an array_ `answer` _where_ `answer[i]` _is the number of **different** nodes that you will visit if you perform the process starting from node_ `i`.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2023/08/31/graaphdrawio-1.png)
18+
19+
**Input:** edges = [1,2,0,0]
20+
21+
**Output:** [3,3,3,4]
22+
23+
**Explanation:** We perform the process starting from each node in the following way:
24+
- Starting from node 0, we visit the nodes 0 -> 1 -> 2 -> 0. The number of different nodes we visit is 3.
25+
- Starting from node 1, we visit the nodes 1 -> 2 -> 0 -> 1. The number of different nodes we visit is 3.
26+
- Starting from node 2, we visit the nodes 2 -> 0 -> 1 -> 2. The number of different nodes we visit is 3.
27+
- Starting from node 3, we visit the nodes 3 -> 0 -> 1 -> 2 -> 0. The number of different nodes we visit is 4.
28+
29+
**Example 2:**
30+
31+
![](https://assets.leetcode.com/uploads/2023/08/31/graaph2drawio.png)
32+
33+
**Input:** edges = [1,2,3,4,0]
34+
35+
**Output:** [5,5,5,5,5]
36+
37+
**Explanation:** Starting from any node we can visit every node in the graph in the process.
38+
39+
**Constraints:**
40+
41+
* `n == edges.length`
42+
* <code>2 <= n <= 10<sup>5</sup></code>
43+
* `0 <= edges[i] <= n - 1`
44+
* `edges[i] != i`
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2877\. Create a DataFrame from List
2+
3+
Easy
4+
5+
Write a solution to **create** a DataFrame from a 2D list called `student_data`. This 2D list contains the IDs and ages of some students.
6+
7+
The DataFrame should have two columns, `student_id` and `age`, and be in the same order as the original 2D list.
8+
9+
The result format is in the following example.
10+
11+
**Example 1:**
12+
13+
**Input:** student\_data: `[ [1, 15], [2, 11], [3, 11], [4, 20] ]`
14+
15+
**Output:**
16+
17+
+------------+-----+
18+
| student_id | age |
19+
+------------+-----+
20+
| 1 | 15 |
21+
| 2 | 11 |
22+
| 3 | 11 |
23+
| 4 | 20 |
24+
+------------+-----+
25+
26+
**Explanation:** A DataFrame was created on top of student\_data, with two columns named `student_id` and `age`.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# #Easy #2023_12_22_Time_406_ms_(82.57%)_Space_59.2_MB_(81.15%)
2+
3+
import pandas as pd
4+
5+
def createDataframe(student_data: List[List[int]]) -> pd.DataFrame:
6+
column_name = ['student_id','age']
7+
result = pd.DataFrame(student_data, columns=column_name)
8+
return result

0 commit comments

Comments
 (0)