Skip to content

Commit 8989c83

Browse files
Merge branch 'neetcode-gh:main' into main
2 parents 185a553 + c3fc8cd commit 8989c83

7 files changed

+114
-16
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Large diffs are not rendered by default.

c/0057-insert-interval.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
int** insert(int** intervals, int intervalsSize, int* intervalsColSize, int* newInterval, int newIntervalSize, int* returnSize, int** returnColumnSizes) {
2+
// Create a result array to store the merged intervals
3+
int** result = (int**)malloc(sizeof(int*) * (intervalsSize + 1));
4+
*returnColumnSizes = (int*)malloc(sizeof(int) * (intervalsSize + 1));
5+
6+
int i = 0, j = 0;
7+
8+
// Add intervals that end before the new interval starts
9+
while (i < intervalsSize && intervals[i][1] < newInterval[0]) {
10+
result[j] = (int*)malloc(sizeof(int) * 2);
11+
result[j][0] = intervals[i][0];
12+
result[j][1] = intervals[i][1];
13+
(*returnColumnSizes)[j] = 2;
14+
j++;
15+
i++;
16+
}
17+
18+
// Merge overlapping intervals
19+
while (i < intervalsSize && intervals[i][0] <= newInterval[1]) {
20+
newInterval[0] = (newInterval[0] < intervals[i][0]) ? newInterval[0] : intervals[i][0];
21+
newInterval[1] = (newInterval[1] > intervals[i][1]) ? newInterval[1] : intervals[i][1];
22+
i++;
23+
}
24+
25+
// Add the merged interval
26+
result[j] = (int*)malloc(sizeof(int) * 2);
27+
result[j][0] = newInterval[0];
28+
result[j][1] = newInterval[1];
29+
(*returnColumnSizes)[j] = 2;
30+
j++;
31+
32+
// Add remaining intervals
33+
while (i < intervalsSize) {
34+
result[j] = (int*)malloc(sizeof(int) * 2);
35+
result[j][0] = intervals[i][0];
36+
result[j][1] = intervals[i][1];
37+
(*returnColumnSizes)[j] = 2;
38+
j++;
39+
i++;
40+
}
41+
42+
// Update the return size
43+
*returnSize = j;
44+
45+
return result;
46+
}

c/0763-partition-labels.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
int* partitionLabels(char* s, int* returnSize) {
2+
int* result = NULL;
3+
int* lastOccurrence = (int*)calloc(26, sizeof(int)); // Store the last occurrence index of each character
4+
5+
// Find the last occurrence index of each character
6+
for (int i = 0; s[i] != '\0'; i++) {
7+
lastOccurrence[s[i] - 'a'] = i;
8+
}
9+
10+
int start = 0, end = 0; // Pointers to track the current partition
11+
*returnSize = 0;
12+
13+
for (int i = 0; s[i] != '\0'; i++) {
14+
end = (end > lastOccurrence[s[i] - 'a']) ? end : lastOccurrence[s[i] - 'a'];
15+
16+
if (i == end) {
17+
(*returnSize)++;
18+
result = (int*)realloc(result, sizeof(int) * (*returnSize));
19+
result[(*returnSize) - 1] = end - start + 1;
20+
start = end + 1;
21+
}
22+
}
23+
24+
free(lastOccurrence);
25+
return result;
26+
}

c/0846-hand-of-straights.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
int cmpfunc(const void* a, const void* b) {
2+
return (*(int*)a - *(int*)b);
3+
}
4+
5+
bool isNStraightHand(int* hand, int handSize, int groupSize) {
6+
if (handSize % groupSize != 0) return false;
7+
8+
qsort(hand, handSize, sizeof(int), cmpfunc);
9+
10+
for (int i = 0; i < handSize; i++) {
11+
if (hand[i] == -1) continue;
12+
int k = i;
13+
for (int j = 1; j < groupSize; j++) {
14+
while (k < handSize && hand[i] + j != hand[k]) k++;
15+
if (k == handSize) return false;
16+
hand[k] = -1;
17+
}
18+
}
19+
20+
return true;
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
bool mergeTriplets(int** triplets, int tripletsSize, int* tripletsColSize, int* target, int targetSize) {
2+
int x = 0, y = 0, z = 0;
3+
4+
for (int i = 0; i < tripletsSize; i++) {
5+
if (triplets[i][0] <= target[0] && triplets[i][1] <= target[1] && triplets[i][2] <= target[2]) {
6+
x = (x > triplets[i][0]) ? x : triplets[i][0];
7+
y = (y > triplets[i][1]) ? y : triplets[i][1];
8+
z = (z > triplets[i][2]) ? z : triplets[i][2];
9+
}
10+
}
11+
12+
return (x == target[0] && y == target[1] && z == target[2]);
13+
}

cpp/0021-merge-two-sorted-lists.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,8 @@ class Solution {
3131
return list1;
3232
}
3333

34-
ListNode* head = NULL;
35-
if (list1->val <= list2->val) {
36-
head = list1;
37-
list1 = list1->next;
38-
} else {
39-
head = list2;
40-
list2 = list2->next;
41-
}
42-
ListNode* curr = head;
43-
34+
ListNode* dummy = new ListNode();
35+
ListNode *curr = dummy;
4436
while (list1 != NULL && list2 != NULL) {
4537
if (list1->val <= list2->val) {
4638
curr->next = list1;
@@ -58,6 +50,6 @@ class Solution {
5850
curr->next = list1;
5951
}
6052

61-
return head;
53+
return dummy->next;
6254
}
6355
};

python/0128-longest-consecutive-sequence.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def longestConsecutive(self, nums: List[int]) -> int:
33
numSet = set(nums)
44
longest = 0
55

6-
for n in nums:
6+
for n in numSet:
77
# check if its the start of a sequence
88
if (n - 1) not in numSet:
99
length = 1

0 commit comments

Comments
 (0)