Skip to content

Commit 1412caa

Browse files
authored
Merge branch 'neetcode-gh:main' into main
2 parents 44f5b3f + 758e425 commit 1412caa

File tree

200 files changed

+5472
-299
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

200 files changed

+5472
-299
lines changed

README.md

Lines changed: 101 additions & 101 deletions
Large diffs are not rendered by default.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
int removeDuplicates(int* nums, int numsSize){
2+
int indx = 1;
3+
4+
for(int i = 1; i < numsSize; i++){
5+
if(nums[i] != nums[i-1]){
6+
nums[indx] = nums[i];
7+
indx++;
8+
}
9+
}
10+
return indx;
11+
}

c/0041-first-missing-positive.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
int firstMissingPositive(int* nums, int numsSize){
2+
3+
int i = 0;
4+
5+
while(i < numsSize){
6+
int val = nums[i];
7+
if(val > 0) val = val - 1;
8+
9+
if(nums[i] > 0 && nums[i] < numsSize && nums[i] != nums[val]){
10+
int temp = nums[i];
11+
nums[i] = nums[val];
12+
nums[val] = temp;
13+
}
14+
else i++;
15+
}
16+
17+
int ans = numsSize + 1;
18+
for(int i = 0 ; i < numsSize ; i++){
19+
if(nums[i] != i+1){
20+
ans = i+1;
21+
break;
22+
}
23+
}
24+
25+
return ans;
26+
}

c/0043-multiply-strings.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
char* multiply(char* num1, char* num2) {
2+
int len1 = strlen(num1);
3+
int len2 = strlen(num2);
4+
int len = len1 + len2;
5+
int* result = (int*)calloc(len, sizeof(int));
6+
7+
for (int i = len1 - 1; i >= 0; i--) {
8+
for (int j = len2 - 1; j >= 0; j--) {
9+
int product = (num1[i] - '0') * (num2[j] - '0');
10+
int sum = product + result[i + j + 1];
11+
result[i + j + 1] = sum % 10;
12+
result[i + j] += sum / 10;
13+
}
14+
}
15+
16+
char* res = (char*)malloc((len + 1) * sizeof(char));
17+
int idx = 0;
18+
for (int i = 0; i < len; i++) {
19+
if (idx == 0 && result[i] == 0) {
20+
continue;
21+
}
22+
res[idx++] = result[i] + '0';
23+
}
24+
res[idx] = '\0';
25+
26+
if (idx == 0) {
27+
return "0";
28+
}
29+
30+
free(result);
31+
return res;
32+
}

c/0054-spiral-matrix.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Note: The returned array must be malloced, assume caller calls free().
3+
*/
4+
int* spiralOrder(int** matrix, int matrixSize, int* matrixColSize, int* returnSize) {
5+
// Allocate the result array and set its size
6+
int *result = (int*)malloc(sizeof(int) * matrixSize * matrixColSize[0]);
7+
*returnSize = 0;
8+
9+
// Handle the empty matrix case
10+
if (matrixSize == 0 || matrixColSize[0] == 0) {
11+
return result;
12+
}
13+
14+
// Initialize the boundaries of the matrix
15+
int top = 0, bottom = matrixSize - 1, left = 0, right = matrixColSize[0] - 1;
16+
17+
// Traverse the matrix in a clockwise spiral order
18+
while (top <= bottom && left <= right) {
19+
// Traverse the top row
20+
for (int col = left; col <= right; col++) {
21+
*result = matrix[top][col];
22+
(*returnSize)++;
23+
result++;
24+
}
25+
top++;
26+
27+
// Traverse the right column
28+
for (int row = top; row <= bottom; row++) {
29+
*result = matrix[row][right];
30+
(*returnSize)++;
31+
result++;
32+
}
33+
right--;
34+
35+
// Traverse the bottom row
36+
if (top <= bottom) {
37+
for (int col = right; col >= left; col--) {
38+
*result = matrix[bottom][col];
39+
(*returnSize)++;
40+
result++;
41+
}
42+
bottom--;
43+
}
44+
45+
// Traverse the left column
46+
if (left <= right) {
47+
for (int row = bottom; row >= top; row--) {
48+
*result = matrix[row][left];
49+
(*returnSize)++;
50+
result++;
51+
}
52+
left++;
53+
}
54+
}
55+
56+
return result - (*returnSize); // Adjust the pointer back before returning
57+
}

c/0056-merge-intervals.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Interval structure
2+
struct Interval {
3+
int start;
4+
int end;
5+
};
6+
7+
// Function to compare intervals for sorting
8+
int compareIntervals(const void* a, const void* b) {
9+
return ((struct Interval*)a)->start - ((struct Interval*)b)->start;
10+
}
11+
12+
// Function to merge intervals
13+
int** merge(int** intervals, int intervalsSize, int* intervalsColSize, int* returnSize, int** returnColumnSizes) {
14+
if (intervalsSize <= 1) {
15+
*returnSize = intervalsSize;
16+
*returnColumnSizes = intervalsColSize;
17+
return intervals;
18+
}
19+
20+
// Create an array of Interval structures
21+
struct Interval* sortedIntervals = (struct Interval*)malloc(sizeof(struct Interval) * intervalsSize);
22+
for (int i = 0; i < intervalsSize; i++) {
23+
sortedIntervals[i].start = intervals[i][0];
24+
sortedIntervals[i].end = intervals[i][1];
25+
}
26+
27+
// Sort intervals based on start times
28+
qsort(sortedIntervals, intervalsSize, sizeof(struct Interval), compareIntervals);
29+
30+
// Merge intervals
31+
struct Interval* mergedIntervals = (struct Interval*)malloc(sizeof(struct Interval) * intervalsSize);
32+
int mergedCount = 0;
33+
34+
for (int i = 0; i < intervalsSize; i++) {
35+
if (mergedCount == 0 || mergedIntervals[mergedCount - 1].end < sortedIntervals[i].start) {
36+
mergedIntervals[mergedCount++] = sortedIntervals[i];
37+
} else {
38+
mergedIntervals[mergedCount - 1].end = mergedIntervals[mergedCount - 1].end > sortedIntervals[i].end
39+
? mergedIntervals[mergedCount - 1].end
40+
: sortedIntervals[i].end;
41+
}
42+
}
43+
44+
// Allocate memory for return arrays
45+
int** result = (int**)malloc(sizeof(int*) * mergedCount);
46+
*returnColumnSizes = (int*)malloc(sizeof(int) * mergedCount);
47+
48+
for (int i = 0; i < mergedCount; i++) {
49+
result[i] = (int*)malloc(sizeof(int) * 2);
50+
result[i][0] = mergedIntervals[i].start;
51+
result[i][1] = mergedIntervals[i].end;
52+
(*returnColumnSizes)[i] = 2;
53+
}
54+
55+
*returnSize = mergedCount;
56+
57+
free(sortedIntervals);
58+
free(mergedIntervals);
59+
60+
return result;
61+
}

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/0069-sqrtx.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
int mySqrt(int x){
2+
int l = 0;
3+
int r = x;
4+
5+
while(l <= r){
6+
long int m = (l + r) / 2;
7+
if(m * m == x){
8+
return m;
9+
}
10+
else if(m * m > x){
11+
r = m - 1;
12+
}
13+
else{
14+
l = m + 1;
15+
}
16+
}
17+
return r;
18+
}

c/0120-triangle.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
int min(int a, int b) {
2+
return a < b ? a : b;
3+
}
4+
5+
int minimumTotal(int** triangle, int triangleSize, int* triangleColSize) {
6+
// We'll use bottom-up dynamic programming approach
7+
for (int i = triangleSize - 2; i >= 0; i--) {
8+
for (int j = 0; j <= i; j++) {
9+
triangle[i][j] += min(triangle[i + 1][j], triangle[i + 1][j + 1]);
10+
}
11+
}
12+
return triangle[0][0];
13+
}
File renamed without changes.

0 commit comments

Comments
 (0)