Skip to content

Commit 8eca8c9

Browse files
authored
Merge branch 'main' into main
2 parents f51f5b7 + cb98925 commit 8eca8c9

File tree

84 files changed

+1984
-113
lines changed

Some content is hidden

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

84 files changed

+1984
-113
lines changed

.github/workflows/stale.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ jobs:
2424
repo-token: ${{ secrets.GITHUB_TOKEN }}
2525
stale-issue-message: 'Stale issue message'
2626
stale-pr-message: 'Stale pull request message'
27-
stale-issue-label: 'no-issue-activity'
28-
stale-pr-label: 'no-pr-activity'
27+
stale-issue-label: 'stale'
28+
stale-pr-label: 'stale'
2929
days-before-stale: 30
3030
days-before-close: 7
31-
days-before-issue-stale: 90
31+
days-before-issue-stale: 900
3232
exempt-milestones: true
3333
exempt-pr-labels: 'pending'

README.md

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

c/108-Convert-Sorted-Array.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Given an integer array nums where the elements are sorted in
3+
ascending order, convert it to a height-balanced binary search tree.
4+
5+
Space: O(n)
6+
Time: O(n)
7+
*/
8+
9+
struct TreeNode* dichomoty_rec(int* nums, int i, int j) {
10+
if (i>j)
11+
return NULL;
12+
struct TreeNode* new_t = malloc(sizeof(struct TreeNode));
13+
int m = (i+j)/2;
14+
new_t->val = nums[m];
15+
new_t->left = dichomoty_rec(nums, i, m-1);
16+
new_t->right = dichomoty_rec(nums, m+1, j);
17+
return new_t;
18+
}
19+
20+
struct TreeNode* sortedArrayToBST(int* nums, int numsSize){
21+
return dichomoty_rec(nums, 0, numsSize-1);
22+
}

c/112-Path-Sum.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Given the root of a binary tree and an integer targetSum, return true
3+
if the tree has a root-to-leaf path such that adding up all the values
4+
along the path equals targetSum.
5+
6+
Space: O(log(n)) (due to recursive calls)
7+
Time: O(n)
8+
*/
9+
10+
bool hasPathSum(struct TreeNode* root, int targetSum){
11+
if (!root)
12+
return false;
13+
if (!root->left && !root->right)
14+
return root->val == targetSum;
15+
return hasPathSum(root->left, targetSum - root->val) || hasPathSum(root->right, targetSum - root->val);
16+
}

c/118-Pascals-Triangle.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Given an integer numRows, return the first numRows of Pascal's triangle.
3+
4+
Space: O(n²) (n=numRows)
5+
Time: O(n²)
6+
*/
7+
8+
int** generate(int numRows, int* returnSize, int** returnColumnSizes){
9+
*returnSize = numRows;
10+
(*returnColumnSizes) = malloc(sizeof(int*)*numRows);
11+
int** ans = malloc(sizeof(int*)*numRows);
12+
for (int i=0; i<numRows; i++) {
13+
(*returnColumnSizes)[i] = i+1;
14+
ans[i] = malloc(sizeof(int)*(i+1));
15+
ans[i][0] = 1;
16+
ans[i][i] = 1;
17+
}
18+
for (int i=2; i<numRows; i++) {
19+
for (int j=1; j<i; j++) {
20+
ans[i][j] = ans[i-1][j-1] + ans[i-1][j];
21+
}
22+
}
23+
return ans;
24+
}

c/1189-Maximum-Number-of-Balloons.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Given a string text, you want to use the characters of text to form as many instances of the word "balloon" as possible.
3+
4+
Space: O(1)
5+
Time: O(n)
6+
*/
7+
8+
9+
int min(int a, int b) {
10+
return a<b?a:b;
11+
}
12+
13+
int maxNumberOfBalloons(char * text){
14+
// Counter for each letter
15+
int b=0;
16+
int a=0;
17+
int l=0;
18+
int o=0;
19+
int n=0;
20+
for (int i=0; text[i]!='\0'; i++) {
21+
if (text[i]=='b') {
22+
b++;
23+
} else if (text[i]=='a') {
24+
a++;
25+
} else if (text[i]=='l') {
26+
l++;
27+
} else if (text[i]=='o') {
28+
o++;
29+
} else if (text[i]=='n') {
30+
n++;
31+
}
32+
}
33+
l /= 2; // There is 2 'l' in balloon
34+
o /= 2; // There is 2 'o' in balloon
35+
return min(b, min(a, min(l, min(o, n))));
36+
}

c/129-Sum-Root-To-Leaf.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
Return the total sum of all root-to-leaf numbers.
3+
4+
Space: O(1)
5+
Time: O(n)
6+
*/
7+
8+
int dfs(struct TreeNode* r, int acc) {
9+
if (r==NULL)
10+
return acc;
11+
if (r->left==NULL && r->right==NULL)
12+
return acc*10 + r->val;
13+
if (r->left==NULL)
14+
return dfs(r->right, acc*10 + r->val);
15+
if (r->right==NULL)
16+
return dfs(r->left, acc*10 + r->val);
17+
return dfs(r->right, acc*10 + r->val) + dfs(r->left, acc*10 + r->val);
18+
}
19+
20+
int sumNumbers(struct TreeNode* root){
21+
return dfs(root, 0);
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1.
3+
4+
Space: O(1)
5+
Time: O(n)
6+
*/
7+
8+
int max(int a, int b) {
9+
return a>b?a:b;
10+
}
11+
12+
int* replaceElements(int* arr, int arrSize, int* returnSize){
13+
int greatest = -1;
14+
*returnSize = arrSize;
15+
for (int i=arrSize-1; i>=0; i--) {
16+
int m = greatest;
17+
greatest = max(greatest, arr[i]);
18+
arr[i] = m;
19+
}
20+
return arr;
21+
}

c/169-Majority-Element.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Given an array nums of size n, return the majority element.
3+
4+
Space: O(1)
5+
Time: O(n)
6+
*/
7+
8+
int majorityElement(int* nums, int numsSize){
9+
int candidate=nums[0];
10+
int count=1;
11+
for (int i=1; i<numsSize; i++) {
12+
if (candidate==nums[i]) {
13+
count++;
14+
} else {
15+
count--;
16+
if (count==0) {
17+
count=1;
18+
candidate=nums[i];
19+
}
20+
}
21+
}
22+
return candidate;
23+
}

c/1905-Count-Sub-Islands.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
3+
Space: O(n²) (due to recursives calls)
4+
Time: O(n²)
5+
*/
6+
7+
bool dfs_test(int** grid1, int** grid2, int i, int j, int n, int m) {
8+
// Test if the island in grid2 is entirely in grid1 and delete the island in grid2
9+
bool ans = (grid1[i][j] == 1);
10+
grid2[i][j] = 0;
11+
if (i>0 && grid2[i-1][j]==1)
12+
ans = dfs_test(grid1, grid2, i-1, j, n ,m) && ans;
13+
if (j>0 && grid2[i][j-1]==1)
14+
ans = dfs_test(grid1, grid2, i, j-1, n ,m) && ans;
15+
if (i<(n-1) && grid2[i+1][j]==1)
16+
ans = dfs_test(grid1, grid2, i+1, j, n ,m) && ans;
17+
if (j<(m-1) && grid2[i][j+1]==1)
18+
ans = dfs_test(grid1, grid2, i, j+1, n ,m) && ans;
19+
return ans;
20+
}
21+
22+
int countSubIslands(int** grid1, int grid1Size, int* grid1ColSize, int** grid2, int grid2Size, int* grid2ColSize){
23+
int cpt=0;
24+
for (int i=0; i<grid1Size; i++) {
25+
for (int j=0; j<grid1ColSize[i]; j++) {
26+
if (grid2[i][j]==1) {
27+
// Test if the island in grid2 is contained in an island in grid1
28+
if (dfs_test(grid1, grid2, i, j, grid1Size, grid1ColSize[i])){
29+
cpt++;
30+
}
31+
}
32+
}
33+
}
34+
return cpt;
35+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy