Skip to content

Commit 5bbc5cd

Browse files
committed
Added tasks 56-114
1 parent 646c4d7 commit 5bbc5cd

File tree

21 files changed

+1851
-77
lines changed

21 files changed

+1851
-77
lines changed

Diff for: README.md

+140-77
Large diffs are not rendered by default.
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-C/LeetCode-in-C?label=Stars&style=flat-square)](https://github.com/LeetCode-in-C/LeetCode-in-C)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-C/LeetCode-in-C?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-C/LeetCode-in-C/fork)
3+
4+
## 56\. Merge Intervals
5+
6+
Medium
7+
8+
Given an array of `intervals` where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, merge all overlapping intervals, and return _an array of the non-overlapping intervals that cover all the intervals in the input_.
9+
10+
**Example 1:**
11+
12+
**Input:** intervals = \[\[1,3],[2,6],[8,10],[15,18]]
13+
14+
**Output:** [[1,6],[8,10],[15,18]]
15+
16+
**Explanation:** Since intervals [1,3] and [2,6] overlap, merge them into [1,6].
17+
18+
**Example 2:**
19+
20+
**Input:** intervals = \[\[1,4],[4,5]]
21+
22+
**Output:** [[1,5]]
23+
24+
**Explanation:** Intervals [1,4] and [4,5] are considered overlapping.
25+
26+
**Constraints:**
27+
28+
* <code>1 <= intervals.length <= 10<sup>4</sup></code>
29+
* `intervals[i].length == 2`
30+
* <code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>4</sup></code>
31+
32+
## Solution
33+
34+
```c
35+
#include <stdio.h>
36+
#include <stdlib.h>
37+
38+
/**
39+
* Return an array of arrays of size *returnSize.
40+
* The sizes of the arrays are returned as *returnColumnSizes array.
41+
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
42+
*/
43+
int compare(const void* a, const void* b) {
44+
return (*(int**)a)[0] - (*(int**)b)[0];
45+
}
46+
47+
int** merge(int** intervals, int intervalsSize, int* intervalsColSize, int* returnSize, int** returnColumnSizes) {
48+
// Step 1: Sort the intervals based on the starting times
49+
qsort(intervals, intervalsSize, sizeof(int*), compare);
50+
51+
// Step 2: Create a dynamic array to hold merged intervals
52+
int** merged = (int**)malloc(intervalsSize * sizeof(int*));
53+
*returnColumnSizes = (int*)malloc(intervalsSize * sizeof(int));
54+
*returnSize = 0;
55+
56+
// Initialize the current interval as the first one
57+
int* current = intervals[0];
58+
merged[(*returnSize)++] = (int*)malloc(2 * sizeof(int));
59+
merged[0][0] = current[0];
60+
merged[0][1] = current[1];
61+
62+
// Step 3: Iterate through the intervals
63+
for (int i = 1; i < intervalsSize; i++) {
64+
int* next = intervals[i];
65+
// Check if there is an overlap
66+
if (current[1] >= next[0]) {
67+
current[1] = (current[1] > next[1]) ? current[1] : next[1]; // Merge
68+
merged[*returnSize - 1][1] = current[1]; // Update the last merged interval
69+
} else {
70+
current = next; // Move to the next interval
71+
merged[(*returnSize)++] = (int*)malloc(2 * sizeof(int));
72+
merged[*returnSize - 1][0] = current[0];
73+
merged[*returnSize - 1][1] = current[1];
74+
}
75+
}
76+
77+
// Step 4: Allocate return column sizes
78+
for (int i = 0; i < *returnSize; i++) {
79+
(*returnColumnSizes)[i] = 2; // Each merged interval has size 2
80+
}
81+
82+
return merged;
83+
}
84+
```

Diff for: src/main/c/g0001_0100/s0062_unique_paths/readme.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-C/LeetCode-in-C?label=Stars&style=flat-square)](https://github.com/LeetCode-in-C/LeetCode-in-C)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-C/LeetCode-in-C?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-C/LeetCode-in-C/fork)
3+
4+
## 62\. Unique Paths
5+
6+
Medium
7+
8+
There is a robot on an `m x n` grid. The robot is initially located at the **top-left corner** (i.e., `grid[0][0]`). The robot tries to move to the **bottom-right corner** (i.e., `grid[m - 1][n - 1]`). The robot can only move either down or right at any point in time.
9+
10+
Given the two integers `m` and `n`, return _the number of possible unique paths that the robot can take to reach the bottom-right corner_.
11+
12+
The test cases are generated so that the answer will be less than or equal to <code>2 * 10<sup>9</sup></code>.
13+
14+
**Example 1:**
15+
16+
![](https://assets.leetcode.com/uploads/2018/10/22/robot_maze.png)
17+
18+
**Input:** m = 3, n = 7
19+
20+
**Output:** 28
21+
22+
**Example 2:**
23+
24+
**Input:** m = 3, n = 2
25+
26+
**Output:** 3
27+
28+
**Explanation:** From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
29+
30+
1. Right -> Down -> Down
31+
32+
2. Down -> Down -> Right
33+
34+
3. Down -> Right -> Down
35+
36+
**Constraints:**
37+
38+
* `1 <= m, n <= 100`
39+
40+
## Solution
41+
42+
```c
43+
#include <stdio.h>
44+
#include <stdlib.h>
45+
46+
int uniquePaths(int m, int n) {
47+
// Step 1: Allocate a 2D array for dynamic programming
48+
int** dp = (int**)malloc(m * sizeof(int*));
49+
for (int i = 0; i < m; i++) {
50+
dp[i] = (int*)malloc(n * sizeof(int));
51+
}
52+
53+
// Step 2: Initialize the first column
54+
for (int i = 0; i < m; i++) {
55+
dp[i][0] = 1;
56+
}
57+
58+
// Step 3: Initialize the first row
59+
for (int j = 0; j < n; j++) {
60+
dp[0][j] = 1;
61+
}
62+
63+
// Step 4: Fill the rest of the dp array
64+
for (int i = 1; i < m; i++) {
65+
for (int j = 1; j < n; j++) {
66+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
67+
}
68+
}
69+
70+
// Step 5: Store the result
71+
int result = dp[m - 1][n - 1];
72+
73+
// Step 6: Free allocated memory
74+
for (int i = 0; i < m; i++) {
75+
free(dp[i]);
76+
}
77+
free(dp);
78+
79+
return result;
80+
}
81+
```
+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-C/LeetCode-in-C?label=Stars&style=flat-square)](https://github.com/LeetCode-in-C/LeetCode-in-C)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-C/LeetCode-in-C?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-C/LeetCode-in-C/fork)
3+
4+
## 64\. Minimum Path Sum
5+
6+
Medium
7+
8+
Given a `m x n` `grid` filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.
9+
10+
**Note:** You can only move either down or right at any point in time.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/11/05/minpath.jpg)
15+
16+
**Input:** grid = \[\[1,3,1],[1,5,1],[4,2,1]]
17+
18+
**Output:** 7
19+
20+
**Explanation:** Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.
21+
22+
**Example 2:**
23+
24+
**Input:** grid = \[\[1,2,3],[4,5,6]]
25+
26+
**Output:** 12
27+
28+
**Constraints:**
29+
30+
* `m == grid.length`
31+
* `n == grid[i].length`
32+
* `1 <= m, n <= 200`
33+
* `0 <= grid[i][j] <= 100`
34+
35+
## Solution
36+
37+
```c
38+
#include <stdio.h>
39+
#include <stdlib.h>
40+
#include <limits.h>
41+
42+
int recur(int** grid, int** dm, int r, int c, int rows, int cols) {
43+
if (dm[r][c] == 0 && r != rows - 1 && c != cols - 1) {
44+
dm[r][c] = grid[r][c] + (recur(grid, dm, r + 1, c, rows, cols) < recur(grid, dm, r, c + 1, rows, cols)
45+
? recur(grid, dm, r + 1, c, rows, cols)
46+
: recur(grid, dm, r, c + 1, rows, cols));
47+
}
48+
return dm[r][c];
49+
}
50+
51+
int minPathSum(int** grid, int gridSize, int* gridColSize) {
52+
if (gridSize == 1 && gridColSize[0] == 1) {
53+
return grid[0][0];
54+
}
55+
56+
int rows = gridSize;
57+
int cols = gridColSize[0];
58+
int** dm = (int**)malloc(rows * sizeof(int*));
59+
for (int i = 0; i < rows; i++) {
60+
dm[i] = (int*)malloc(cols * sizeof(int));
61+
for (int j = 0; j < cols; j++) {
62+
dm[i][j] = 0; // Initialize to 0
63+
}
64+
}
65+
66+
int s = 0;
67+
for (int r = rows - 1; r >= 0; r--) {
68+
dm[r][cols - 1] = grid[r][cols - 1] + s;
69+
s += grid[r][cols - 1];
70+
}
71+
72+
s = 0;
73+
for (int c = cols - 1; c >= 0; c--) {
74+
dm[rows - 1][c] = grid[rows - 1][c] + s;
75+
s += grid[rows - 1][c];
76+
}
77+
78+
int result = recur(grid, dm, 0, 0, rows, cols);
79+
80+
// Free allocated memory
81+
for (int i = 0; i < rows; i++) {
82+
free(dm[i]);
83+
}
84+
free(dm);
85+
86+
return result;
87+
}
88+
```
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-C/LeetCode-in-C?label=Stars&style=flat-square)](https://github.com/LeetCode-in-C/LeetCode-in-C)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-C/LeetCode-in-C?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-C/LeetCode-in-C/fork)
3+
4+
## 70\. Climbing Stairs
5+
6+
Easy
7+
8+
You are climbing a staircase. It takes `n` steps to reach the top.
9+
10+
Each time you can either climb `1` or `2` steps. In how many distinct ways can you climb to the top?
11+
12+
**Example 1:**
13+
14+
**Input:** n = 2
15+
16+
**Output:** 2
17+
18+
**Explanation:** There are two ways to climb to the top.
19+
20+
1. 1 step + 1 step
21+
22+
2. 2 steps
23+
24+
**Example 2:**
25+
26+
**Input:** n = 3
27+
28+
**Output:** 3
29+
30+
**Explanation:** There are three ways to climb to the top.
31+
32+
1. 1 step + 1 step + 1 step
33+
34+
2. 1 step + 2 steps
35+
36+
3. 2 steps + 1 step
37+
38+
**Constraints:**
39+
40+
* `1 <= n <= 45`
41+
42+
## Solution
43+
44+
```c
45+
#include <stdio.h>
46+
#include <stdlib.h>
47+
48+
int climbStairs(int n) {
49+
if (n < 2) {
50+
return n;
51+
}
52+
53+
// Create an array to store the number of ways to climb to each step
54+
int* cache = (int*)malloc(n * sizeof(int));
55+
56+
// Base cases
57+
cache[0] = 1; // 1 way to climb to the 1st step
58+
cache[1] = 2; // 2 ways to climb to the 2nd step
59+
60+
// Fill the cache using dynamic programming
61+
for (int i = 2; i < n; i++) {
62+
cache[i] = cache[i - 1] + cache[i - 2];
63+
}
64+
65+
// Store the result
66+
int result = cache[n - 1];
67+
68+
// Free allocated memory
69+
free(cache);
70+
71+
return result;
72+
}
73+
```

0 commit comments

Comments
 (0)