Skip to content

Commit 2cd7e63

Browse files
committed
Updated tasks 15-128
1 parent 77d40db commit 2cd7e63

File tree

9 files changed

+434
-446
lines changed

9 files changed

+434
-446
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023-2024 Valentyn Kolesnikov
3+
Copyright (c) 2023-2025 Valentyn Kolesnikov
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

LeetCodeNet/G0001_0100/S0015_3sum/readme.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,40 +40,32 @@ public class Solution {
4040
Array.Sort(nums);
4141
int len = nums.Length;
4242
IList<IList<int>> result = new List<IList<int>>();
43-
4443
for (int i = 0; i < len - 2; i++) {
4544
int l = i + 1;
4645
int r = len - 1;
47-
4846
while (r > l) {
4947
int sum = nums[i] + nums[l] + nums[r];
50-
5148
if (sum < 0) {
5249
l++;
5350
} else if (sum > 0) {
5451
r--;
5552
} else {
5653
IList<int> list = new List<int> { nums[i], nums[l], nums[r] };
5754
result.Add(list);
58-
5955
while (l < r && nums[l + 1] == nums[l]) {
6056
l++;
6157
}
62-
6358
while (r > l && nums[r - 1] == nums[r]) {
6459
r--;
6560
}
66-
6761
l++;
6862
r--;
6963
}
7064
}
71-
7265
while (i < len - 1 && nums[i + 1] == nums[i]) {
7366
i++;
7467
}
7568
}
76-
7769
return result;
7870
}
7971
}

LeetCodeNet/G0001_0100/S0017_letter_combinations_of_a_phone_number/readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,25 @@ public class Solution {
4545
return new List<string>();
4646
}
4747
string[] letters = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
48-
IList<string> ans = new List<string>();
48+
List<string> ans = new List<string>();
4949
StringBuilder sb = new StringBuilder();
5050
FindCombinations(0, digits, letters, sb, ans);
5151
return ans;
5252
}
5353

5454
private void FindCombinations(
55-
int start, string nums, string[] letters, StringBuilder curr, IList<string> ans) {
55+
int start, string nums, string[] letters, StringBuilder curr, List<string> ans) {
5656
if (curr.Length == nums.Length) {
5757
ans.Add(curr.ToString());
5858
return;
5959
}
6060
for (int i = start; i < nums.Length; i++) {
61-
int n = int.Parse(nums[i].ToString());
61+
int n = nums[i] - '0'; // Convert char to int
6262
for (int j = 0; j < letters[n].Length; j++) {
6363
char ch = letters[n][j];
6464
curr.Append(ch);
6565
FindCombinations(i + 1, nums, letters, curr, ans);
66-
curr.Length--; // Equivalent to deleting the last character in StringBuilder
66+
curr.Remove(curr.Length - 1, 1); // Remove last character
6767
}
6868
}
6969
}

LeetCodeNet/G0001_0100/S0019_remove_nth_node_from_end_of_list/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public class Solution {
6868
}
6969
RemoveNth(node.next);
7070
this.n--;
71-
7271
if (this.n == 0) {
7372
node.next = node.next.next;
7473
}

LeetCodeNet/G0001_0100/S0021_merge_two_sorted_lists/readme.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public class Solution {
5353
public ListNode MergeTwoLists(ListNode l1, ListNode l2) {
5454
ListNode list = new ListNode(-1);
5555
ListNode head = list;
56-
5756
while (l1 != null || l2 != null) {
5857
if (l1 != null && l2 != null) {
5958
if (l1.val <= l2.val) {
@@ -72,7 +71,6 @@ public class Solution {
7271
}
7372
list = list.next;
7473
}
75-
7674
return head.next;
7775
}
7876
}

LeetCodeNet/G0001_0100/S0079_word_search/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public class Solution {
6868
char currChar = board[r][c];
6969
board[r][c] = '!';
7070
char nextChar = word[count];
71-
7271
if (r > 0 && board[r - 1][c] == nextChar) {
7372
if (helper(r - 1, c, board, word, count + 1)) return true;
7473
}

LeetCodeNet/G0001_0100/S0084_largest_rectangle_in_histogram/readme.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,16 @@ Given an array of integers `heights` representing the histogram's bar height whe
3535
```csharp
3636
public class Solution {
3737
public int LargestRectangleArea(int[] heights) {
38-
int maxArea = 0, i = 0;
38+
int len = heights.Length;
39+
int maxArea = 0;
3940
Stack<int> stack = new Stack<int>();
40-
while (i <= heights.Length) {
41-
var currHeight = i == heights.Length ? 0 : heights[i];
42-
if (!stack.Any() || currHeight >= heights[stack.Peek()]) {
43-
stack.Push(i);
44-
i++;
45-
}
46-
else {
47-
int index = stack.Pop();
48-
int height = heights[index];
49-
int width = (!stack.Any()) ? i : (i - 1) - stack.Peek();
41+
for (int i = 0; i <= len; i++) {
42+
while (stack.Count > 0 && (i == len || heights[stack.Peek()] >= (i < len ? heights[i] : 0))) {
43+
int height = heights[stack.Pop()];
44+
int width = stack.Count == 0 ? i : i - stack.Peek() - 1;
5045
maxArea = Math.Max(maxArea, height * width);
5146
}
47+
stack.Push(i);
5248
}
5349
return maxArea;
5450
}

LeetCodeNet/G0101_0200/S0128_longest_consecutive_sequence/readme.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,29 @@ You must write an algorithm that runs in `O(n)` time.
3333
```csharp
3434
public class Solution {
3535
public int LongestConsecutive(int[] nums) {
36-
if (nums.Length == 0) {
37-
return 0;
38-
}
39-
Array.Sort(nums);
40-
int max = int.MinValue;
41-
int thsMax = 1;
42-
for (int i = 0; i < nums.Length - 1; i++) {
43-
if (nums[i + 1] == nums[i] + 1) {
44-
thsMax += 1;
36+
Dictionary<int, int> mapToHighest = new(nums.Length);
37+
int best = 0;
38+
for (int i = 0; i < nums.Length; i++) {
39+
int rangeLow = 0;
40+
int rangeHigh = 0;
41+
if (mapToHighest.ContainsKey(nums[i])) {
4542
continue;
4643
}
47-
if (nums[i + 1] == nums[i]) {
48-
continue;
44+
if (mapToHighest.TryGetValue(nums[i]-1, out var downCount)) {
45+
rangeLow = downCount;
46+
}
47+
if (mapToHighest.TryGetValue(nums[i]+1, out var upCount)) {
48+
rangeHigh = upCount;
49+
}
50+
int thisSum = rangeLow + rangeHigh + 1;
51+
mapToHighest[nums[i] - rangeLow] = thisSum;
52+
mapToHighest[nums[i] + rangeHigh] = thisSum;
53+
if (rangeLow != 0 && rangeHigh != 0) {
54+
mapToHighest[nums[i]] = 1;
4955
}
50-
// Start of a new Sequene
51-
max = Math.Max(max, thsMax);
52-
thsMax = 1;
56+
best = Math.Max(thisSum, best);
5357
}
54-
return Math.Max(max, thsMax);
58+
return best;
5559
}
5660
}
5761
```

0 commit comments

Comments
 (0)