Skip to content

Commit 3889fc0

Browse files
authored
Added tasks 23-41
1 parent 31adf14 commit 3889fc0

File tree

31 files changed

+1039
-0
lines changed

31 files changed

+1039
-0
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
5353

5454
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
5555
|-|-|-|-|-|-
56+
| 0035 |[Search Insert Position](src/main/scala/g0001_0100/s0035_search_insert_position/Solution.scala)| Easy | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 466 | 87.04
5657

5758
#### Day 2 Two Pointers
5859

@@ -128,6 +129,8 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
128129

129130
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
130131
|-|-|-|-|-|-
132+
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/scala/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 478 | 97.85
133+
| 0033 |[Search in Rotated Sorted Array](src/main/scala/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 453 | 91.53
131134

132135
#### Day 2 Binary Search
133136

@@ -175,6 +178,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
175178

176179
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
177180
|-|-|-|-|-|-
181+
| 0039 |[Combination Sum](src/main/scala/g0001_0100/s0039_combination_sum/Solution.scala)| Medium | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(2^n)_Space_O(n+2^n) | 488 | 97.50
178182

179183
#### Day 11 Recursion Backtracking
180184

@@ -245,6 +249,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
245249

246250
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
247251
|-|-|-|-|-|-
252+
| 0035 |[Search Insert Position](src/main/scala/g0001_0100/s0035_search_insert_position/Solution.scala)| Easy | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 466 | 87.04
248253

249254
#### Day 3
250255

@@ -260,6 +265,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
260265

261266
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
262267
|-|-|-|-|-|-
268+
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/scala/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 478 | 97.85
263269

264270
#### Day 6
265271

@@ -290,6 +296,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
290296

291297
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
292298
|-|-|-|-|-|-
299+
| 0033 |[Search in Rotated Sorted Array](src/main/scala/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 453 | 91.53
293300

294301
#### Day 12
295302

@@ -916,6 +923,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
916923

917924
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
918925
|-|-|-|-|-|-
926+
| 0033 |[Search in Rotated Sorted Array](src/main/scala/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 453 | 91.53
919927

920928
#### Day 9 Binary Search Tree
921929

@@ -977,6 +985,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
977985

978986
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
979987
|-|-|-|-|-|-
988+
| 0039 |[Combination Sum](src/main/scala/g0001_0100/s0039_combination_sum/Solution.scala)| Medium | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(2^n)_Space_O(n+2^n) | 488 | 97.50
980989

981990
### Udemy
982991

@@ -997,12 +1006,14 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
9971006

9981007
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
9991008
|-|-|-|-|-|-
1009+
| 0033 |[Search in Rotated Sorted Array](src/main/scala/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 453 | 91.53
10001010

10011011
#### Udemy Arrays
10021012

10031013
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10041014
|-|-|-|-|-|-
10051015
| 0001 |[Two Sum](src/main/scala/g0001_0100/s0001_two_sum/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 517 | 86.66
1016+
| 0041 |[First Missing Positive](src/main/scala/g0001_0100/s0041_first_missing_positive/Solution.scala)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 650 | 100.00
10061017

10071018
#### Udemy Two Pointers
10081019

@@ -1029,7 +1040,9 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
10291040

10301041
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10311042
|-|-|-|-|-|-
1043+
| 0024 |[Swap Nodes in Pairs](src/main/scala/g0001_0100/s0024_swap_nodes_in_pairs/Solution.scala)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 441 | 95.65
10321044
| 0021 |[Merge Two Sorted Lists](src/main/scala/g0001_0100/s0021_merge_two_sorted_lists/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 480 | 89.72
1045+
| 0025 |[Reverse Nodes in k-Group](src/main/scala/g0001_0100/s0025_reverse_nodes_in_k_group/Solution.scala)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 520 | 80.00
10331046

10341047
#### Udemy Tree Stack Queue
10351048

@@ -1057,6 +1070,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
10571070
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10581071
|-|-|-|-|-|-
10591072
| 0022 |[Generate Parentheses](src/main/scala/g0001_0100/s0022_generate_parentheses/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(2^n)_Space_O(n) | 380 | 100.00
1073+
| 0039 |[Combination Sum](src/main/scala/g0001_0100/s0039_combination_sum/Solution.scala)| Medium | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(2^n)_Space_O(n+2^n) | 488 | 97.50
10601074
| 0017 |[Letter Combinations of a Phone Number](src/main/scala/g0001_0100/s0017_letter_combinations_of_a_phone_number/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking, Big_O_Time_O(4^n)_Space_O(n) | 459 | 58.54
10611075

10621076
#### Udemy Bit Manipulation
@@ -1208,11 +1222,13 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
12081222

12091223
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12101224
|-|-|-|-|-|-
1225+
| 0024 |[Swap Nodes in Pairs](src/main/scala/g0001_0100/s0024_swap_nodes_in_pairs/Solution.scala)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 441 | 95.65
12111226

12121227
#### Day 13 Linked List
12131228

12141229
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12151230
|-|-|-|-|-|-
1231+
| 0025 |[Reverse Nodes in k-Group](src/main/scala/g0001_0100/s0025_reverse_nodes_in_k_group/Solution.scala)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 520 | 80.00
12161232

12171233
#### Day 14 Stack Queue
12181234

@@ -1258,6 +1274,16 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
12581274

12591275
| # | Title | Difficulty | Tag | Time, ms | Time, %
12601276
|------|----------------|-------------|-------------|----------|---------
1277+
| 0041 |[First Missing Positive](src/main/scala/g0001_0100/s0041_first_missing_positive/Solution.scala)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Udemy_Arrays, Big_O_Time_O(n)_Space_O(n) | 650 | 100.00
1278+
| 0039 |[Combination Sum](src/main/scala/g0001_0100/s0039_combination_sum/Solution.scala)| Medium | Top_100_Liked_Questions, Array, Backtracking, Algorithm_II_Day_10_Recursion_Backtracking, Level_2_Day_20_Brute_Force/Backtracking, Udemy_Backtracking/Recursion, Big_O_Time_O(2^n)_Space_O(n+2^n) | 488 | 97.50
1279+
| 0035 |[Search Insert Position](src/main/scala/g0001_0100/s0035_search_insert_position/Solution.scala)| Easy | Top_100_Liked_Questions, Array, Binary_Search, Algorithm_I_Day_1_Binary_Search, Binary_Search_I_Day_2, Big_O_Time_O(log_n)_Space_O(1) | 466 | 87.04
1280+
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/scala/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_5, Big_O_Time_O(log_n)_Space_O(1) | 478 | 97.85
1281+
| 0033 |[Search in Rotated Sorted Array](src/main/scala/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_11, Level_2_Day_8_Binary_Search, Udemy_Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 453 | 91.53
1282+
| 0032 |[Longest Valid Parentheses](src/main/scala/g0001_0100/s0032_longest_valid_parentheses/Solution.scala)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Stack, Big_O_Time_O(n)_Space_O(1) | 444 | 87.50
1283+
| 0031 |[Next Permutation](src/main/scala/g0001_0100/s0031_next_permutation/Solution.scala)| Medium | Top_100_Liked_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 462 | 100.00
1284+
| 0025 |[Reverse Nodes in k-Group](src/main/scala/g0001_0100/s0025_reverse_nodes_in_k_group/Solution.scala)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Data_Structure_II_Day_13_Linked_List, Udemy_Linked_List, Big_O_Time_O(n)_Space_O(k) | 520 | 80.00
1285+
| 0024 |[Swap Nodes in Pairs](src/main/scala/g0001_0100/s0024_swap_nodes_in_pairs/Solution.scala)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Data_Structure_II_Day_12_Linked_List, Udemy_Linked_List, Big_O_Time_O(n)_Space_O(1) | 441 | 95.65
1286+
| 0023 |[Merge k Sorted Lists](src/main/scala/g0001_0100/s0023_merge_k_sorted_lists/Solution.scala)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Heap_Priority_Queue, Linked_List, Divide_and_Conquer, Merge_Sort, Big_O_Time_O(k\*n\*log(k))_Space_O(log(k)) | 549 | 94.74
12611287
| 0022 |[Generate Parentheses](src/main/scala/g0001_0100/s0022_generate_parentheses/Solution.scala)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Algorithm_II_Day_11_Recursion_Backtracking, Udemy_Backtracking/Recursion, Big_O_Time_O(2^n)_Space_O(n) | 380 | 100.00
12621288
| 0021 |[Merge Two Sorted Lists](src/main/scala/g0001_0100/s0021_merge_two_sorted_lists/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Data_Structure_I_Day_7_Linked_List, Algorithm_I_Day_10_Recursion_Backtracking, Level_1_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(m+n)_Space_O(m+n) | 480 | 89.72
12631289
| 0020 |[Valid Parentheses](src/main/scala/g0001_0100/s0020_valid_parentheses/Solution.scala)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack, Data_Structure_I_Day_9_Stack_Queue, Udemy_Strings, Big_O_Time_O(n)_Space_O(n) | 449 | 65.58
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package g0001_0100.s0023_merge_k_sorted_lists
2+
3+
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Heap_Priority_Queue #Linked_List
4+
// #Divide_and_Conquer #Merge_Sort #Big_O_Time_O(k*n*log(k))_Space_O(log(k))
5+
// #2023_10_30_Time_549_ms_(94.74%)_Space_59.6_MB_(89.47%)
6+
7+
import com_github_leetcode.ListNode
8+
9+
/*
10+
* Definition for singly-linked list.
11+
* class ListNode(_x: Int = 0, _next: ListNode = null) {
12+
* var next: ListNode = _next
13+
* var x: Int = _x
14+
* }
15+
*/
16+
object Solution {
17+
def mergeKLists(lists: Array[ListNode]): ListNode = {
18+
if (lists.isEmpty) {
19+
null
20+
} else {
21+
mergeKLists(lists, 0, lists.length)
22+
}
23+
}
24+
25+
def mergeKLists(lists: Array[ListNode], leftIndex: Int, rightIndex: Int): ListNode = {
26+
if (rightIndex > leftIndex + 1) {
27+
val mid = (leftIndex + rightIndex) / 2
28+
val left = mergeKLists(lists, leftIndex, mid)
29+
val right = mergeKLists(lists, mid, rightIndex)
30+
mergeTwoLists(left, right)
31+
} else {
32+
lists(leftIndex)
33+
}
34+
}
35+
36+
@SuppressWarnings(Array("scala:S1871"))
37+
def mergeTwoLists(left: ListNode, right: ListNode): ListNode = {
38+
if (left == null) {
39+
right
40+
} else if (right == null) {
41+
left
42+
} else {
43+
var res: ListNode = null
44+
var (l, r) = (left, right)
45+
if (l.x <= r.x) {
46+
res = l
47+
l = l.next
48+
} else {
49+
res = r
50+
r = r.next
51+
}
52+
var node = res
53+
while (l != null || r != null) {
54+
if (l == null) {
55+
node.next = r
56+
r = r.next
57+
} else if (r == null) {
58+
node.next = l
59+
l = l.next
60+
} else if (l.x <= r.x) {
61+
node.next = l
62+
l = l.next
63+
} else {
64+
node.next = r
65+
r = r.next
66+
}
67+
node = node.next
68+
}
69+
res
70+
}
71+
}
72+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
23\. Merge k Sorted Lists
2+
3+
Hard
4+
5+
You are given an array of `k` linked-lists `lists`, each linked-list is sorted in ascending order.
6+
7+
_Merge all the linked-lists into one sorted linked-list and return it._
8+
9+
**Example 1:**
10+
11+
**Input:** lists = [[1,4,5],[1,3,4],[2,6]]
12+
13+
**Output:** [1,1,2,3,4,4,5,6]
14+
15+
**Explanation:** The linked-lists are: [ 1->4->5, 1->3->4, 2->6 ] merging them into one sorted list: 1->1->2->3->4->4->5->6
16+
17+
**Example 2:**
18+
19+
**Input:** lists = []
20+
21+
**Output:** []
22+
23+
**Example 3:**
24+
25+
**Input:** lists = [[]]
26+
27+
**Output:** []
28+
29+
**Constraints:**
30+
31+
* `k == lists.length`
32+
* `0 <= k <= 10^4`
33+
* `0 <= lists[i].length <= 500`
34+
* `-10^4 <= lists[i][j] <= 10^4`
35+
* `lists[i]` is sorted in **ascending order**.
36+
* The sum of `lists[i].length` won't exceed `10^4`.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package g0001_0100.s0024_swap_nodes_in_pairs
2+
3+
// #Medium #Top_100_Liked_Questions #Linked_List #Recursion #Data_Structure_II_Day_12_Linked_List
4+
// #Udemy_Linked_List #Big_O_Time_O(n)_Space_O(1)
5+
// #2023_10_30_Time_441_ms_(95.65%)_Space_56.9_MB_(52.17%)
6+
7+
import com_github_leetcode.ListNode
8+
9+
/*
10+
* Definition for singly-linked list.
11+
* class ListNode(_x: Int = 0, _next: ListNode = null) {
12+
* var next: ListNode = _next
13+
* var x: Int = _x
14+
* }
15+
*/
16+
object Solution {
17+
def swapPairs(head: ListNode): ListNode = {
18+
if (head == null) {
19+
return null
20+
}
21+
val len = getLength(head)
22+
reverse(head, len)
23+
}
24+
25+
private def getLength(curr: ListNode): Int = {
26+
var cnt = 0
27+
var current = curr
28+
while (current != null) {
29+
cnt += 1
30+
current = current.next
31+
}
32+
cnt
33+
}
34+
35+
private def reverse(head: ListNode, len: Int): ListNode = {
36+
if (len < 2) {
37+
return head
38+
}
39+
var curr = head
40+
var prev: ListNode = null
41+
var next: ListNode = null
42+
for (i <- 0 until 2) {
43+
next = curr.next
44+
curr.next = prev
45+
prev = curr
46+
curr = next
47+
}
48+
head.next = reverse(curr, len - 2)
49+
prev
50+
}
51+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
24\. Swap Nodes in Pairs
2+
3+
Medium
4+
5+
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg)
10+
11+
**Input:** head = [1,2,3,4]
12+
13+
**Output:** [2,1,4,3]
14+
15+
**Example 2:**
16+
17+
**Input:** head = []
18+
19+
**Output:** []
20+
21+
**Example 3:**
22+
23+
**Input:** head = [1]
24+
25+
**Output:** [1]
26+
27+
**Constraints:**
28+
29+
* The number of nodes in the list is in the range `[0, 100]`.
30+
* `0 <= Node.val <= 100`
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package g0001_0100.s0025_reverse_nodes_in_k_group
2+
3+
// #Hard #Top_100_Liked_Questions #Linked_List #Recursion #Data_Structure_II_Day_13_Linked_List
4+
// #Udemy_Linked_List #Big_O_Time_O(n)_Space_O(k)
5+
// #2023_10_30_Time_520_ms_(80.00%)_Space_58_MB_(50.00%)
6+
7+
import com_github_leetcode.ListNode
8+
9+
/*
10+
* Definition for singly-linked list.
11+
* class ListNode(_x: Int = 0, _next: ListNode = null) {
12+
* var next: ListNode = _next
13+
* var x: Int = _x
14+
* }
15+
*/
16+
object Solution {
17+
def reverseKGroup(head: ListNode, k: Int): ListNode = {
18+
if (head == null || head.next == null || k == 1) {
19+
return head
20+
}
21+
var j = 0
22+
var len = head
23+
// Loop for checking the length of the linked list; if the linked list is less than k, then return as it is.
24+
while (j < k) {
25+
if (len == null) {
26+
return head
27+
}
28+
len = len.next
29+
j += 1
30+
}
31+
// Reverse linked list logic applied here.
32+
var c = head
33+
var n: ListNode = null
34+
var prev: ListNode = null
35+
var i = 0
36+
// Traverse the while loop for K times to reverse the nodes in K groups.
37+
while (i != k) {
38+
n = c.next
39+
c.next = prev
40+
prev = c
41+
c = n
42+
i += 1
43+
}
44+
// head.x is pointing to the next K group linked list; recursion for further remaining linked list.
45+
head.next = reverseKGroup(n, k)
46+
prev
47+
}
48+
}

0 commit comments

Comments
 (0)