Skip to content

Commit

Permalink
Added tasks 23-41
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Oct 30, 2023
1 parent 31adf14 commit 3889fc0
Show file tree
Hide file tree
Showing 31 changed files with 1,039 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Day 2 Two Pointers

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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
| 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

#### Day 2 Binary Search

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Day 11 Recursion Backtracking

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Day 3

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Day 6

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Day 12

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Day 9 Binary Search Tree

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

### Udemy

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Udemy Arrays

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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
| 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

#### Udemy Two Pointers

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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
| 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
| 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

#### Udemy Tree Stack Queue

Expand Down Expand Up @@ -1057,6 +1070,7 @@ implementation 'com.github.javadev:leetcode-in-scala:1.0'
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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
| 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
| 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

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

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Day 13 Linked List

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 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

#### Day 14 Stack Queue

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

| # | Title | Difficulty | Tag | Time, ms | Time, %
|------|----------------|-------------|-------------|----------|---------
| 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
| 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
| 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
| 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
| 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
| 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
| 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
| 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
| 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
| 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
| 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
| 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
| 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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package g0001_0100.s0023_merge_k_sorted_lists

// #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))
// #2023_10_30_Time_549_ms_(94.74%)_Space_59.6_MB_(89.47%)

import com_github_leetcode.ListNode

/*
* Definition for singly-linked list.
* class ListNode(_x: Int = 0, _next: ListNode = null) {
* var next: ListNode = _next
* var x: Int = _x
* }
*/
object Solution {
def mergeKLists(lists: Array[ListNode]): ListNode = {
if (lists.isEmpty) {
null
} else {
mergeKLists(lists, 0, lists.length)
}
}

def mergeKLists(lists: Array[ListNode], leftIndex: Int, rightIndex: Int): ListNode = {
if (rightIndex > leftIndex + 1) {
val mid = (leftIndex + rightIndex) / 2
val left = mergeKLists(lists, leftIndex, mid)
val right = mergeKLists(lists, mid, rightIndex)
mergeTwoLists(left, right)
} else {
lists(leftIndex)
}
}

@SuppressWarnings(Array("scala:S1871"))
def mergeTwoLists(left: ListNode, right: ListNode): ListNode = {
if (left == null) {
right
} else if (right == null) {
left
} else {
var res: ListNode = null
var (l, r) = (left, right)
if (l.x <= r.x) {
res = l
l = l.next
} else {
res = r
r = r.next
}
var node = res
while (l != null || r != null) {
if (l == null) {
node.next = r
r = r.next
} else if (r == null) {
node.next = l
l = l.next
} else if (l.x <= r.x) {
node.next = l
l = l.next
} else {
node.next = r
r = r.next
}
node = node.next
}
res
}
}
}
36 changes: 36 additions & 0 deletions src/main/scala/g0001_0100/s0023_merge_k_sorted_lists/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
23\. Merge k Sorted Lists

Hard

You are given an array of `k` linked-lists `lists`, each linked-list is sorted in ascending order.

_Merge all the linked-lists into one sorted linked-list and return it._

**Example 1:**

**Input:** lists = [[1,4,5],[1,3,4],[2,6]]

**Output:** [1,1,2,3,4,4,5,6]

**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

**Example 2:**

**Input:** lists = []

**Output:** []

**Example 3:**

**Input:** lists = [[]]

**Output:** []

**Constraints:**

* `k == lists.length`
* `0 <= k <= 10^4`
* `0 <= lists[i].length <= 500`
* `-10^4 <= lists[i][j] <= 10^4`
* `lists[i]` is sorted in **ascending order**.
* The sum of `lists[i].length` won't exceed `10^4`.
51 changes: 51 additions & 0 deletions src/main/scala/g0001_0100/s0024_swap_nodes_in_pairs/Solution.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package g0001_0100.s0024_swap_nodes_in_pairs

// #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)
// #2023_10_30_Time_441_ms_(95.65%)_Space_56.9_MB_(52.17%)

import com_github_leetcode.ListNode

/*
* Definition for singly-linked list.
* class ListNode(_x: Int = 0, _next: ListNode = null) {
* var next: ListNode = _next
* var x: Int = _x
* }
*/
object Solution {
def swapPairs(head: ListNode): ListNode = {
if (head == null) {
return null
}
val len = getLength(head)
reverse(head, len)
}

private def getLength(curr: ListNode): Int = {
var cnt = 0
var current = curr
while (current != null) {
cnt += 1
current = current.next
}
cnt
}

private def reverse(head: ListNode, len: Int): ListNode = {
if (len < 2) {
return head
}
var curr = head
var prev: ListNode = null
var next: ListNode = null
for (i <- 0 until 2) {
next = curr.next
curr.next = prev
prev = curr
curr = next
}
head.next = reverse(curr, len - 2)
prev
}
}
30 changes: 30 additions & 0 deletions src/main/scala/g0001_0100/s0024_swap_nodes_in_pairs/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
24\. Swap Nodes in Pairs

Medium

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.)

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg)

**Input:** head = [1,2,3,4]

**Output:** [2,1,4,3]

**Example 2:**

**Input:** head = []

**Output:** []

**Example 3:**

**Input:** head = [1]

**Output:** [1]

**Constraints:**

* The number of nodes in the list is in the range `[0, 100]`.
* `0 <= Node.val <= 100`
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package g0001_0100.s0025_reverse_nodes_in_k_group

// #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)
// #2023_10_30_Time_520_ms_(80.00%)_Space_58_MB_(50.00%)

import com_github_leetcode.ListNode

/*
* Definition for singly-linked list.
* class ListNode(_x: Int = 0, _next: ListNode = null) {
* var next: ListNode = _next
* var x: Int = _x
* }
*/
object Solution {
def reverseKGroup(head: ListNode, k: Int): ListNode = {
if (head == null || head.next == null || k == 1) {
return head
}
var j = 0
var len = head
// Loop for checking the length of the linked list; if the linked list is less than k, then return as it is.
while (j < k) {
if (len == null) {
return head
}
len = len.next
j += 1
}
// Reverse linked list logic applied here.
var c = head
var n: ListNode = null
var prev: ListNode = null
var i = 0
// Traverse the while loop for K times to reverse the nodes in K groups.
while (i != k) {
n = c.next
c.next = prev
prev = c
c = n
i += 1
}
// head.x is pointing to the next K group linked list; recursion for further remaining linked list.
head.next = reverseKGroup(n, k)
prev
}
}
Loading

0 comments on commit 3889fc0

Please sign in to comment.