Skip to content

Commit 552d6e5

Browse files
committed
Added tasks 240-1143
1 parent b2fe2ea commit 552d6e5

File tree

20 files changed

+1655
-0
lines changed

20 files changed

+1655
-0
lines changed

Diff for: README.md

+49
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
## 240\. Search a 2D Matrix II
5+
6+
Medium
7+
8+
Write an efficient algorithm that searches for a value `target` in an `m x n` integer matrix `matrix`. This matrix has the following properties:
9+
10+
* Integers in each row are sorted in ascending from left to right.
11+
* Integers in each column are sorted in ascending from top to bottom.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg)
16+
17+
**Input:** matrix = \[\[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
18+
19+
**Output:** true
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg)
24+
25+
**Input:** matrix = \[\[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
26+
27+
**Output:** false
28+
29+
**Constraints:**
30+
31+
* `m == matrix.length`
32+
* `n == matrix[i].length`
33+
* `1 <= n, m <= 300`
34+
* <code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code>
35+
* All the integers in each row are **sorted** in ascending order.
36+
* All the integers in each column are **sorted** in ascending order.
37+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
38+
39+
## Solution
40+
41+
```c
42+
#include <stdbool.h>
43+
#include <stdio.h>
44+
45+
bool searchMatrix(int** matrix, int matrixSize, int* matrixColSize, int target) {
46+
int r = 0;
47+
int c = matrixColSize[0] - 1; // Start from the top-right corner
48+
49+
while (r < matrixSize && c >= 0) {
50+
if (matrix[r][c] == target) {
51+
return true;
52+
} else if (matrix[r][c] > target) {
53+
c--; // Move left
54+
} else {
55+
r++; // Move down
56+
}
57+
}
58+
return false;
59+
}
60+
```

Diff for: src/main/c/g0201_0300/s0283_move_zeroes/readme.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
## 283\. Move Zeroes
5+
6+
Easy
7+
8+
Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements.
9+
10+
**Note** that you must do this in-place without making a copy of the array.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [0,1,0,3,12]
15+
16+
**Output:** [1,3,12,0,0]
17+
18+
**Example 2:**
19+
20+
**Input:** nums = [0]
21+
22+
**Output:** [0]
23+
24+
**Constraints:**
25+
26+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
27+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
28+
29+
**Follow up:** Could you minimize the total number of operations done?
30+
31+
## Solution
32+
33+
```c
34+
#include <stdio.h>
35+
36+
void swap(int* a, int* b) {
37+
int temp = *a;
38+
*a = *b;
39+
*b = temp;
40+
}
41+
42+
void moveZeroes(int* nums, int numsSize) {
43+
int firstZero = 0;
44+
for (int i = 0; i < numsSize; i++) {
45+
if (nums[i] != 0) {
46+
swap(&nums[firstZero], &nums[i]);
47+
firstZero++;
48+
}
49+
}
50+
}
51+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
## 287\. Find the Duplicate Number
5+
6+
Medium
7+
8+
Given an array of integers `nums` containing `n + 1` integers where each integer is in the range `[1, n]` inclusive.
9+
10+
There is only **one repeated number** in `nums`, return _this repeated number_.
11+
12+
You must solve the problem **without** modifying the array `nums` and uses only constant extra space.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,3,4,2,2]
17+
18+
**Output:** 2
19+
20+
**Example 2:**
21+
22+
**Input:** nums = [3,1,3,4,2]
23+
24+
**Output:** 3
25+
26+
**Constraints:**
27+
28+
* <code>1 <= n <= 10<sup>5</sup></code>
29+
* `nums.length == n + 1`
30+
* `1 <= nums[i] <= n`
31+
* All the integers in `nums` appear only **once** except for **precisely one integer** which appears **two or more** times.
32+
33+
**Follow up:**
34+
35+
* How can we prove that at least one duplicate number must exist in `nums`?
36+
* Can you solve the problem in linear runtime complexity?
37+
38+
## Solution
39+
40+
```c
41+
#include <stdio.h>
42+
43+
int findDuplicate(int* nums, int numsSize) {
44+
// Create an array of size numsSize + 1, initialized to 0
45+
int arr[numsSize + 1];
46+
for (int i = 0; i <= numsSize; i++) {
47+
arr[i] = 0;
48+
}
49+
50+
// Iterate over the nums array and update the arr counts
51+
for (int i = 0; i < numsSize; i++) {
52+
arr[nums[i]] += 1;
53+
if (arr[nums[i]] == 2) {
54+
return nums[i]; // Return the duplicate element
55+
}
56+
}
57+
58+
return 0; // In case no duplicate is found, though the problem assumes there is always a duplicate
59+
}
60+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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+
## 295\. Find Median from Data Stream
5+
6+
Hard
7+
8+
The **median** is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the mean of the two middle values.
9+
10+
* For example, for `arr = [2,3,4]`, the median is `3`.
11+
* For example, for `arr = [2,3]`, the median is `(2 + 3) / 2 = 2.5`.
12+
13+
Implement the MedianFinder class:
14+
15+
* `MedianFinder()` initializes the `MedianFinder` object.
16+
* `void addNum(int num)` adds the integer `num` from the data stream to the data structure.
17+
* `double findMedian()` returns the median of all elements so far. Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.
18+
19+
**Example 1:**
20+
21+
**Input**
22+
23+
["MedianFinder", "addNum", "addNum", "findMedian", "addNum", "findMedian"]
24+
[[], [1], [2], [], [3], []]
25+
26+
**Output:** [null, null, null, 1.5, null, 2.0]
27+
28+
**Explanation:**
29+
30+
MedianFinder medianFinder = new MedianFinder();
31+
medianFinder.addNum(1); // arr = [1]
32+
medianFinder.addNum(2); // arr = [1, 2]
33+
medianFinder.findMedian(); // return 1.5 (i.e., (1 + 2) / 2)
34+
medianFinder.addNum(3); // arr[1, 2, 3]
35+
medianFinder.findMedian(); // return 2.0
36+
37+
**Constraints:**
38+
39+
* <code>-10<sup>5</sup> <= num <= 10<sup>5</sup></code>
40+
* There will be at least one element in the data structure before calling `findMedian`.
41+
* At most <code>5 * 10<sup>4</sup></code> calls will be made to `addNum` and `findMedian`.
42+
43+
**Follow up:**
44+
45+
* If all integer numbers from the stream are in the range `[0, 100]`, how would you optimize your solution?
46+
* If `99%` of all integer numbers from the stream are in the range `[0, 100]`, how would you optimize your solution?
47+
48+
## Solution
49+
50+
```c
51+
#include <stdio.h>
52+
#include <stdlib.h>
53+
54+
typedef struct {
55+
int* maxHeap;
56+
int maxHeapSize;
57+
int maxHeapCapacity;
58+
59+
int* minHeap;
60+
int minHeapSize;
61+
int minHeapCapacity;
62+
} MedianFinder;
63+
64+
MedianFinder* medianFinderCreate() {
65+
MedianFinder* obj = (MedianFinder*)malloc(sizeof(MedianFinder));
66+
67+
obj->maxHeapCapacity = 10; // Initial capacity for maxHeap
68+
obj->maxHeapSize = 0;
69+
obj->maxHeap = (int*)malloc(obj->maxHeapCapacity * sizeof(int));
70+
71+
obj->minHeapCapacity = 10; // Initial capacity for minHeap
72+
obj->minHeapSize = 0;
73+
obj->minHeap = (int*)malloc(obj->minHeapCapacity * sizeof(int));
74+
75+
return obj;
76+
}
77+
78+
// Helper functions for max-heap and min-heap
79+
void swap(int* a, int* b) {
80+
int temp = *a;
81+
*a = *b;
82+
*b = temp;
83+
}
84+
85+
// Max-Heap functions
86+
void maxHeapPush(MedianFinder* obj, int value) {
87+
if (obj->maxHeapSize == obj->maxHeapCapacity) {
88+
obj->maxHeapCapacity *= 2;
89+
obj->maxHeap = (int*)realloc(obj->maxHeap, obj->maxHeapCapacity * sizeof(int));
90+
}
91+
obj->maxHeap[obj->maxHeapSize++] = value;
92+
93+
// Sift-up
94+
int i = obj->maxHeapSize - 1;
95+
while (i > 0 && obj->maxHeap[(i - 1) / 2] < obj->maxHeap[i]) {
96+
swap(&obj->maxHeap[(i - 1) / 2], &obj->maxHeap[i]);
97+
i = (i - 1) / 2;
98+
}
99+
}
100+
101+
int maxHeapPop(MedianFinder* obj) {
102+
int result = obj->maxHeap[0];
103+
obj->maxHeap[0] = obj->maxHeap[--obj->maxHeapSize];
104+
105+
// Sift-down
106+
int i = 0;
107+
while (2 * i + 1 < obj->maxHeapSize) {
108+
int j = 2 * i + 1;
109+
if (j + 1 < obj->maxHeapSize && obj->maxHeap[j + 1] > obj->maxHeap[j]) {
110+
j++;
111+
}
112+
if (obj->maxHeap[i] >= obj->maxHeap[j]) break;
113+
swap(&obj->maxHeap[i], &obj->maxHeap[j]);
114+
i = j;
115+
}
116+
return result;
117+
}
118+
119+
int maxHeapTop(MedianFinder* obj) {
120+
return obj->maxHeap[0];
121+
}
122+
123+
// Min-Heap functions
124+
void minHeapPush(MedianFinder* obj, int value) {
125+
if (obj->minHeapSize == obj->minHeapCapacity) {
126+
obj->minHeapCapacity *= 2;
127+
obj->minHeap = (int*)realloc(obj->minHeap, obj->minHeapCapacity * sizeof(int));
128+
}
129+
obj->minHeap[obj->minHeapSize++] = value;
130+
131+
// Sift-up
132+
int i = obj->minHeapSize - 1;
133+
while (i > 0 && obj->minHeap[(i - 1) / 2] > obj->minHeap[i]) {
134+
swap(&obj->minHeap[(i - 1) / 2], &obj->minHeap[i]);
135+
i = (i - 1) / 2;
136+
}
137+
}
138+
139+
int minHeapPop(MedianFinder* obj) {
140+
int result = obj->minHeap[0];
141+
obj->minHeap[0] = obj->minHeap[--obj->minHeapSize];
142+
143+
// Sift-down
144+
int i = 0;
145+
while (2 * i + 1 < obj->minHeapSize) {
146+
int j = 2 * i + 1;
147+
if (j + 1 < obj->minHeapSize && obj->minHeap[j + 1] < obj->minHeap[j]) {
148+
j++;
149+
}
150+
if (obj->minHeap[i] <= obj->minHeap[j]) break;
151+
swap(&obj->minHeap[i], &obj->minHeap[j]);
152+
i = j;
153+
}
154+
return result;
155+
}
156+
157+
int minHeapTop(MedianFinder* obj) {
158+
return obj->minHeap[0];
159+
}
160+
161+
// Balancing the heaps
162+
void balanceHeaps(MedianFinder* obj) {
163+
if (obj->maxHeapSize > obj->minHeapSize + 1) {
164+
minHeapPush(obj, maxHeapPop(obj));
165+
} else if (obj->minHeapSize > obj->maxHeapSize) {
166+
maxHeapPush(obj, minHeapPop(obj));
167+
}
168+
}
169+
170+
void medianFinderAddNum(MedianFinder* obj, int num) {
171+
if (obj->maxHeapSize == 0 || num <= maxHeapTop(obj)) {
172+
maxHeapPush(obj, num);
173+
} else {
174+
minHeapPush(obj, num);
175+
}
176+
balanceHeaps(obj);
177+
}
178+
179+
double medianFinderFindMedian(MedianFinder* obj) {
180+
if (obj->maxHeapSize > obj->minHeapSize) {
181+
return maxHeapTop(obj);
182+
} else {
183+
return (maxHeapTop(obj) + minHeapTop(obj)) / 2.0;
184+
}
185+
}
186+
187+
void medianFinderFree(MedianFinder* obj) {
188+
free(obj->maxHeap);
189+
free(obj->minHeap);
190+
free(obj);
191+
}
192+
193+
/**
194+
* Your MedianFinder struct will be instantiated and called as such:
195+
* MedianFinder* obj = medianFinderCreate();
196+
* medianFinderAddNum(obj, num);
197+
198+
* double param_2 = medianFinderFindMedian(obj);
199+
200+
* medianFinderFree(obj);
201+
*/
202+
```

0 commit comments

Comments
 (0)