-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace LeetCodeNet.G0101_0200.S0124_binary_tree_maximum_path_sum { | ||
|
||
using System; | ||
using Xunit; | ||
using Com_github_leetcode; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void MaxPathSum() { | ||
TreeNode treeNode = TreeNode.Create(new List<int?> {1, 2, 3}); | ||
Assert.Equal(6, new Solution().MaxPathSum(treeNode)); | ||
} | ||
|
||
[Fact] | ||
public void MaxPathSum2() { | ||
TreeNode treeNode = TreeNode.Create(new List<int?> {-10, 9, 20, null, null, 15, 7}); | ||
Assert.Equal(42, new Solution().MaxPathSum(treeNode)); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace LeetCodeNet.G0101_0200.S0128_longest_consecutive_sequence { | ||
|
||
using System; | ||
using Xunit; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void LongestConsecutive() { | ||
Assert.Equal(4, new Solution().LongestConsecutive(new int[] {100, 4, 200, 1, 3, 2})); | ||
} | ||
|
||
[Fact] | ||
public void LongestConsecutive2() { | ||
Assert.Equal(9, new Solution().LongestConsecutive(new int[] {0, 3, 7, 2, 5, 8, 4, 6, 0, 1})); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace LeetCodeNet.G0101_0200.S0131_palindrome_partitioning { | ||
|
||
using System; | ||
using Xunit; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void Partition() { | ||
Assert.Equal( | ||
new List<IList<string>> { | ||
new List<string> {"a", "a", "b"}, | ||
new List<string> {"aa", "b"} | ||
}, | ||
new Solution().Partition("aab")); | ||
} | ||
|
||
[Fact] | ||
public void Partition2() { | ||
Assert.Equal( | ||
new List<IList<string>> {new List<string> {"a"}}, | ||
new Solution().Partition("a")); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace LeetCodeNet.G0101_0200.S0136_single_number { | ||
|
||
using System; | ||
using Xunit; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void SingleNumber() { | ||
Assert.Equal(1, new Solution().SingleNumber(new int[] {2, 2, 1})); | ||
} | ||
|
||
[Fact] | ||
public void SingleNumber2() { | ||
Assert.Equal(4, new Solution().SingleNumber(new int[] {4, 1, 2, 1, 2})); | ||
} | ||
|
||
[Fact] | ||
public void SingleNumber3() { | ||
Assert.Equal(1, new Solution().SingleNumber(new int[] {1})); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
namespace LeetCodeNet.G0101_0200.S0138_copy_list_with_random_pointer { | ||
|
||
using System; | ||
using Xunit; | ||
using LeetCodeNet.Com_github_leetcode; | ||
|
||
public class SolutionTest { | ||
[Fact] | ||
public void CopyRandomList() { | ||
Node node7 = new Node(7); | ||
Node node13 = new Node(13); | ||
Node node11 = new Node(11); | ||
Node node10 = new Node(10); | ||
Node node1 = new Node(1); | ||
node7.next = node13; | ||
node13.next = node11; | ||
node11.next = node10; | ||
node10.next = node1; | ||
node1.next = null; | ||
node7.random = null; | ||
node13.random = node7; | ||
node11.random = node1; | ||
node10.random = node11; | ||
node1.random = node7; | ||
Assert.Equal( | ||
"[[7,null],[13,0],[11,4],[10,2],[1,0]]", | ||
new Solution().CopyRandomList(node7).ToString()); | ||
} | ||
|
||
[Fact] | ||
public void CopyRandomList2() { | ||
Node node1 = new Node(1); | ||
Node node2 = new Node(2); | ||
node1.next = node2; | ||
node1.random = node1; | ||
node2.next = null; | ||
node2.random = node2; | ||
Assert.Equal("[[1,1],[2,1]]", new Solution().CopyRandomList(node1).ToString()); | ||
} | ||
|
||
[Fact] | ||
public void CopyRandomList3() { | ||
Node node31 = new Node(3); | ||
Node node32 = new Node(3); | ||
Node node33 = new Node(3); | ||
node31.next = node32; | ||
node31.random = null; | ||
node32.next = node33; | ||
node32.random = node31; | ||
node33.next = null; | ||
node33.random = null; | ||
Assert.Equal("[[3,null],[3,0],[3,null]]", new Solution().CopyRandomList(node31).ToString()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
namespace LeetCodeNet.Com_github_leetcode { | ||
|
||
public class Node { | ||
public int val; | ||
public Node next; | ||
public Node random; | ||
|
||
public Node() { | ||
Check warning on line 8 in LeetCodeNet/Com_github_leetcode/Node.cs GitHub Actions / build-windows
Check warning on line 8 in LeetCodeNet/Com_github_leetcode/Node.cs GitHub Actions / build-windows
Check warning on line 8 in LeetCodeNet/Com_github_leetcode/Node.cs GitHub Actions / build-windows
|
||
this.val = 0; | ||
} | ||
|
||
public Node(int val) { | ||
Check warning on line 12 in LeetCodeNet/Com_github_leetcode/Node.cs GitHub Actions / build-windows
Check warning on line 12 in LeetCodeNet/Com_github_leetcode/Node.cs GitHub Actions / build-windows
Check warning on line 12 in LeetCodeNet/Com_github_leetcode/Node.cs GitHub Actions / build-windows
|
||
this.val = val; | ||
} | ||
|
||
public Node(int val, Node next, Node random) { | ||
this.val = val; | ||
this.next = next; | ||
this.random = random; | ||
} | ||
|
||
public override string ToString() { | ||
List<string> result = new List<string>{}; | ||
List<string> result2 = new List<string> { | ||
val.ToString() | ||
}; | ||
if (random == null) { | ||
result2.Add("null"); | ||
} else { | ||
result2.Add(random.val.ToString()); | ||
} | ||
string result2String = "[" + string.Join(",", result2) + "]"; | ||
result.Add(result2String); | ||
Node curr = next; | ||
while (curr != null) { | ||
List<string> result3 = new List<string> { | ||
curr.val.ToString() | ||
}; | ||
if (curr.random == null) { | ||
result3.Add("null"); | ||
} else { | ||
int randomIndex = 0; | ||
Node curr2 = this; | ||
while (curr2.next != null && curr2 != curr.random) { | ||
randomIndex += 1; | ||
curr2 = curr2.next; | ||
} | ||
result3.Add(randomIndex.ToString()); | ||
} | ||
string result3String = "[" + string.Join(",", result3) + "]"; | ||
result.Add(result3String); | ||
curr = curr.next; | ||
} | ||
return "[" + string.Join(",", result) + "]"; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
namespace LeetCodeNet.G0101_0200.S0124_binary_tree_maximum_path_sum { | ||
|
||
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Dynamic_Programming #Depth_First_Search | ||
// #Tree #Binary_Tree #Udemy_Tree_Stack_Queue #Big_O_Time_O(N)_Space_O(N) | ||
// #2024_01_09_Time_85_ms_(91.69%)_Space_47.6_MB_(23.52%) | ||
|
||
using LeetCodeNet.Com_github_leetcode; | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* public int val; | ||
* public TreeNode left; | ||
* public TreeNode right; | ||
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
public class Solution { | ||
private int max = int.MinValue; | ||
|
||
private int Helper(TreeNode root) { | ||
if (root == null) { | ||
return 0; | ||
} | ||
// to avoid the -ve values in left side we will compare them with 0 | ||
int left = Math.Max(0, Helper(root.left)); | ||
int right = Math.Max(0, Helper(root.right)); | ||
int current = (int)(root.val + left + right); | ||
if (current > max) { | ||
max = current; | ||
} | ||
return (int)(root.val + Math.Max(left, right)); | ||
} | ||
|
||
public int MaxPathSum(TreeNode root) { | ||
Helper(root); | ||
return max; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
124\. Binary Tree Maximum Path Sum | ||
|
||
Hard | ||
|
||
A **path** in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence **at most once**. Note that the path does not need to pass through the root. | ||
|
||
The **path sum** of a path is the sum of the node's values in the path. | ||
|
||
Given the `root` of a binary tree, return _the maximum **path sum** of any **non-empty** path_. | ||
|
||
**Example 1:** | ||
|
||
![](https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg) | ||
|
||
**Input:** root = [1,2,3] | ||
|
||
**Output:** 6 | ||
|
||
**Explanation:** The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6. | ||
|
||
**Example 2:** | ||
|
||
![](https://assets.leetcode.com/uploads/2020/10/13/exx2.jpg) | ||
|
||
**Input:** root = [-10,9,20,null,null,15,7] | ||
|
||
**Output:** 42 | ||
|
||
**Explanation:** The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42. | ||
|
||
**Constraints:** | ||
|
||
* The number of nodes in the tree is in the range <code>[1, 3 * 10<sup>4</sup>]</code>. | ||
* `-1000 <= Node.val <= 1000` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace LeetCodeNet.G0101_0200.S0128_longest_consecutive_sequence { | ||
|
||
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Union_Find | ||
// #Big_O_Time_O(N_log_N)_Space_O(1) #2024_01_09_Time_201_ms_(61.50%)_Space_61.2_MB_(52.89%) | ||
|
||
public class Solution { | ||
public int LongestConsecutive(int[] nums) { | ||
if (nums.Length == 0) { | ||
return 0; | ||
} | ||
Array.Sort(nums); | ||
int max = int.MinValue; | ||
int thsMax = 1; | ||
for (int i = 0; i < nums.Length - 1; i++) { | ||
if (nums[i + 1] == nums[i] + 1) { | ||
thsMax += 1; | ||
continue; | ||
} | ||
if (nums[i + 1] == nums[i]) { | ||
continue; | ||
} | ||
// Start of a new Sequene | ||
max = Math.Max(max, thsMax); | ||
thsMax = 1; | ||
} | ||
return Math.Max(max, thsMax); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
128\. Longest Consecutive Sequence | ||
|
||
Medium | ||
|
||
Given an unsorted array of integers `nums`, return _the length of the longest consecutive elements sequence._ | ||
|
||
You must write an algorithm that runs in `O(n)` time. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums = [100,4,200,1,3,2] | ||
|
||
**Output:** 4 | ||
|
||
**Explanation:** The longest consecutive elements sequence is `[1, 2, 3, 4]`. Therefore its length is 4. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [0,3,7,2,5,8,4,6,0,1] | ||
|
||
**Output:** 9 | ||
|
||
**Constraints:** | ||
|
||
* <code>0 <= nums.length <= 10<sup>5</sup></code> | ||
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
namespace LeetCodeNet.G0101_0200.S0131_palindrome_partitioning { | ||
|
||
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming | ||
// #Backtracking #Big_O_Time_O(N*2^N)_Space_O(2^N*N) | ||
// #2024_01_09_Time_677_ms_(38.30%)_Space_148_MB_(5.53%) | ||
|
||
public class Solution { | ||
public IList<IList<string>> Partition(string s) { | ||
IList<IList<string>> res = new List<IList<string>>(); | ||
Backtracking(res, new List<string>(), s, 0); | ||
return res; | ||
} | ||
|
||
private void Backtracking(IList<IList<string>> res, IList<string> currArr, string s, int start) { | ||
if (start == s.Length) { | ||
res.Add(new List<string>(currArr)); | ||
} | ||
for (int end = start; end < s.Length; end++) { | ||
if (!IsPanlindrome(s, start, end)) { | ||
continue; | ||
} | ||
currArr.Add(s.Substring(start, end - start + 1)); | ||
Backtracking(res, currArr, s, end + 1); | ||
currArr.RemoveAt(currArr.Count - 1); | ||
} | ||
} | ||
|
||
private bool IsPanlindrome(string s, int start, int end) { | ||
while (start < end && s[start] == s[end]) { | ||
start++; | ||
end--; | ||
} | ||
return start >= end; | ||
} | ||
} | ||
} |