Skip to content

Commit 29b6335

Browse files
committed
https://leetcode.com/problems/merge-k-sorted-lists/
1 parent 633908e commit 29b6335

File tree

6 files changed

+87
-21
lines changed

6 files changed

+87
-21
lines changed

LeetCodeSolutions/BitManipulation/AddBinary.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
using NUnit.Framework;
2-
using NUnit.Framework.Legacy;
3-
41
namespace LeetCodeSolutions.BitManipulation;
52

63
public class Add_Binary
@@ -11,15 +8,15 @@ public string AddBinary(string a, string b)
118
int ib = b.Length - 1;
129
string returnValue = "";
1310
char carry = '0';
14-
11+
1512
while (ia >= 0 || ib >= 0)
1613
{
1714
var tempA = '0';
1815
if (ia >= 0)
1916
{
2017
tempA = a[ia];
2118
}
22-
19+
2320
var tempB = '0';
2421
if (ib >= 0)
2522
{
@@ -30,15 +27,16 @@ public string AddBinary(string a, string b)
3027

3128
carry = result.carry;
3229
returnValue = $"{result.sum}{returnValue}";
33-
30+
3431
ia--;
3532
ib--;
3633
}
37-
34+
3835
if (carry != '0')
3936
{
4037
returnValue = $"{carry}{returnValue}";
4138
}
39+
4240
return returnValue;
4341
}
4442

LeetCodeSolutions/BitManipulation/Single_Number.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
using NUnit.Framework;
2-
3-
namespace LeetCode
1+
namespace LeetCodeSolutions.BitManipulation
42
{
5-
/// <summary>
6-
/// https://leetcode.com/problems/single-number/
7-
/// </summary>
8-
public class Single_Number
3+
public class SingleNumberSolution
94
{
105
public int SingleNumber(int[] nums)
116
{
12-
137
if (nums == null || nums.Length % 2 == 0)
148
{
159
return 0;
@@ -44,4 +38,4 @@ public void Test1((int Output, int[] Input) item)
4438
(4, [4, 1, 2, 1, 2]),
4539
};
4640
}
47-
}
41+
}

LeetCodeSolutions/DivideAndConquer/ConvertSortedArrayToBinarySearchTree.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11

22
namespace LeetCodeSolutions.DivideAndConquer;
33

4-
/// <summary>
5-
/// https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
6-
/// </summary>
74
public class ConvertSortedArrayToBinarySearchTree
85
{
96
public TreeNode SortedArrayToBST(int[] nums)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
namespace LeetCodeSolutions.DivideAndConquer
2+
{
3+
class MergeKSortedLists
4+
{
5+
public ListNode MergeKLists(ListNode[] lists)
6+
{
7+
// Handle edge cases
8+
if (lists == null || lists.Length == 0) return null;
9+
10+
// Start the divide-and-conquer process
11+
return MergeDivideAndConquer(lists, 0, lists.Length - 1);
12+
}
13+
14+
private ListNode MergeDivideAndConquer(ListNode[] lists, int left, int right)
15+
{
16+
// Base case: only one list in the current range
17+
if (left == right) return lists[left];
18+
19+
// Divide the problem into two halves
20+
int mid = left + (right - left) / 2;
21+
ListNode leftList = MergeDivideAndConquer(lists, left, mid);
22+
ListNode rightList = MergeDivideAndConquer(lists, mid + 1, right);
23+
24+
// Merge the two halves
25+
return MergeTwoLists(leftList, rightList);
26+
}
27+
28+
private ListNode MergeTwoLists(ListNode l1, ListNode l2)
29+
{
30+
// Create a dummy head to simplify edge cases
31+
ListNode dummy = new ListNode();
32+
ListNode current = dummy;
33+
34+
// Compare and link nodes in sorted order
35+
while (l1 != null && l2 != null)
36+
{
37+
if (l1.val <= l2.val)
38+
{
39+
current.next = l1;
40+
l1 = l1.next;
41+
}
42+
else
43+
{
44+
current.next = l2;
45+
l2 = l2.next;
46+
}
47+
48+
current = current.next;
49+
}
50+
51+
// Attach remaining nodes (if any)
52+
current.next = (l1 != null) ? l1 : l2;
53+
54+
return dummy.next;
55+
}
56+
57+
58+
[Test(Description = "https://leetcode.com/problems/merge-k-sorted-lists/")]
59+
[Category("Hard")]
60+
[Category("LeetCode")]
61+
[Category("Merge K Sorted Lists")]
62+
[TestCaseSource(nameof(Input))]
63+
[Category("DivideAndConquer")]
64+
[Category("TopInterview")]
65+
public void Test1((int[] Output, int[][] Input) item)
66+
{
67+
var response = MergeKLists(item.Input.Select(x => x.ToListNode()).ToArray());
68+
Assert.That(response.ToArray(), Is.EqualTo(item.Output));
69+
}
70+
71+
public static IEnumerable<(int[] Output, int[][] Input)> Input =>
72+
new List<(int[] Output, int[][] Input)>()
73+
{
74+
([1, 1, 2, 3, 4, 4, 5, 6], ( [[1, 4, 5], [1, 3, 4], [2, 6]])),
75+
};
76+
}
77+
}

LeetCodeSolutions/DivideAndConquer/Sort List.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private ListNode Merge(ListNode left, ListNode right)
7474
[TestCaseSource(nameof(Input))]
7575
[Category("DivideAndConquer")]
7676
[Category("TopInterview")]
77-
public void Test1((int?[] Output, int[] Input) item)
77+
public void Test1((int[] Output, int[] Input) item)
7878
{
7979
var response = SortList(item.Input.ToListNode());
8080
Assert.That(response.ToArray(), Is.EqualTo(item.Output));

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ The Top Interview 150 collection on Leetcode is a curated set of 150 interview q
169169
| 108 | Convert Sorted Array to Binary Search Tree | Easy ||
170170
| 109 | Sort List | Medium ||
171171
| 110 | Construct Quad Tree | Medium | |
172-
| 111 | Merge k Sorted Lists | Hard | |
172+
| 111 | Merge k Sorted Lists | Hard | |
173173
| <br> Kadane&#39;s Algorithm<br> | | | |
174174
| 112 | Maximum Subarray | Medium | |
175175
| 113 | Maximum Sum Circular Subarray | Medium | |

0 commit comments

Comments
 (0)