mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
Merge branch 'master' into problem_121
This commit is contained in:
commit
98ab38f170
@ -8,6 +8,7 @@ LeetCode
|
|||||||
|---| ----- | -------- | ---------- |
|
|---| ----- | -------- | ---------- |
|
||||||
|1|[Two Sum](https://https://leetcode.com/problems/two-sum/) | [C](./src/1.c)|Easy
|
|1|[Two Sum](https://https://leetcode.com/problems/two-sum/) | [C](./src/1.c)|Easy
|
||||||
|3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [C](./src/3.c)|Medium|
|
|3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [C](./src/3.c)|Medium|
|
||||||
|
|4|[Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [C](./src/4.c)|Hard|
|
||||||
|20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [C](./src/20.c)|Easy|
|
|20|[Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [C](./src/20.c)|Easy|
|
||||||
|24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [C](./src/24.c)|Medium|
|
|24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [C](./src/24.c)|Medium|
|
||||||
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [C](./src/26.c)|Easy|
|
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [C](./src/26.c)|Easy|
|
||||||
@ -55,6 +56,7 @@ LeetCode
|
|||||||
|520|[Detect Capital](https://leetcode.com/problems/detect-capital/) | [C](./src/520.c)|Easy|
|
|520|[Detect Capital](https://leetcode.com/problems/detect-capital/) | [C](./src/520.c)|Easy|
|
||||||
|561|[Array Partition I](https://leetcode.com/problems/array-partition-i/) | [C](./src/561.c)|Easy|
|
|561|[Array Partition I](https://leetcode.com/problems/array-partition-i/) | [C](./src/561.c)|Easy|
|
||||||
|617|[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [C](./src/617.c)|Easy|
|
|617|[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/) | [C](./src/617.c)|Easy|
|
||||||
|
|647|[Palindromic Substring](https://leetcode.com/problems/palindromic-substrings/) | [C](./src/647.c)|Medium|
|
||||||
|674|[Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | [C](./src/674.c)|Easy|
|
|674|[Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | [C](./src/674.c)|Easy|
|
||||||
|700|[Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [C](./src/700.c)|Easy|
|
|700|[Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [C](./src/700.c)|Easy|
|
||||||
|701|[Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [C](./src/701.c)|Medium|
|
|701|[Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [C](./src/701.c)|Medium|
|
||||||
@ -68,4 +70,7 @@ LeetCode
|
|||||||
|938|[Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/) | [C](./src/938.c)|Easy|
|
|938|[Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/) | [C](./src/938.c)|Easy|
|
||||||
|965|[Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [C](./src/965.c)|Easy|
|
|965|[Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/) | [C](./src/965.c)|Easy|
|
||||||
|977|[Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [C](./src/977.c)|Easy|
|
|977|[Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [C](./src/977.c)|Easy|
|
||||||
|
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/) | [C](./src/1089.c)|Easy|
|
||||||
|
|1184|[Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops/) | [C](./src/1184.c)|Easy|
|
||||||
|1189|[Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/) | [C](./src/1189.c)|Easy|
|
|1189|[Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/) | [C](./src/1189.c)|Easy|
|
||||||
|
|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/) | [C](./src/1207.c)|Easy|
|
20
leetcode/src/1089.c
Normal file
20
leetcode/src/1089.c
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
void duplicateZeros(int* arr, int arrSize){
|
||||||
|
int i, start = 0;
|
||||||
|
int *tmp = malloc(arrSize * sizeof(int));
|
||||||
|
/* Copy arr into tmp arr */
|
||||||
|
for(i = 0; i < arrSize; i++) {
|
||||||
|
tmp[i] = arr[i];
|
||||||
|
}
|
||||||
|
i = 0;
|
||||||
|
for(start = 0; start < arrSize; start++) {
|
||||||
|
arr[start] = tmp[i];
|
||||||
|
if(tmp[i] == 0) {
|
||||||
|
start++;
|
||||||
|
if (start < arrSize)
|
||||||
|
arr[start] = 0;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
16
leetcode/src/1184.c
Normal file
16
leetcode/src/1184.c
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
int distanceBetweenBusStops(int* distance, int distanceSize, int start, int destination){
|
||||||
|
|
||||||
|
int sum1 = 0, sum2 = 0;
|
||||||
|
if (start > destination) {
|
||||||
|
int tmp = start;
|
||||||
|
start = destination;
|
||||||
|
destination = tmp;
|
||||||
|
}
|
||||||
|
for (auto i = 0; i < distanceSize; ++i) {
|
||||||
|
if (i >= start && i < destination)
|
||||||
|
sum1 += distance[i];
|
||||||
|
else
|
||||||
|
sum2 += distance[i];
|
||||||
|
}
|
||||||
|
return sum1 < sum2 ? sum1 : sum2;
|
||||||
|
}
|
25
leetcode/src/1207.c
Normal file
25
leetcode/src/1207.c
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#define MAP_SIZE 2048
|
||||||
|
|
||||||
|
int cmpvalue(const void *a, const void *b) {
|
||||||
|
return *(int *)b - *(int *)a;
|
||||||
|
}
|
||||||
|
bool uniqueOccurrences(int* arr, int arrSize){
|
||||||
|
int *map = calloc(MAP_SIZE, sizeof(int));
|
||||||
|
int i;
|
||||||
|
for(i = 0; i < arrSize; i++) {
|
||||||
|
if (arr[i] < 0)
|
||||||
|
map[arr[i] + MAP_SIZE/2] += 1;
|
||||||
|
else
|
||||||
|
map[arr[i]] += 1;
|
||||||
|
}
|
||||||
|
/* number of occurrences is sorted by decreasing order
|
||||||
|
Ex: 3 2 1 0 0 0 0 */
|
||||||
|
qsort(map, MAP_SIZE, sizeof(int), cmpvalue);
|
||||||
|
i = 0;
|
||||||
|
while(map[i]) {
|
||||||
|
if(map[i] == map[i+1])
|
||||||
|
return 0;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
40
leetcode/src/4.c
Normal file
40
leetcode/src/4.c
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
|
||||||
|
|
||||||
|
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size){
|
||||||
|
int index1=0;
|
||||||
|
int index2=0;
|
||||||
|
int v[nums1Size+nums2Size];
|
||||||
|
int v_index=0;
|
||||||
|
|
||||||
|
while(index1<nums1Size && index2<nums2Size){
|
||||||
|
if(nums1[index1]<=nums2[index2]){
|
||||||
|
v[v_index++]=nums1[index1++];
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
v[v_index++]=nums2[index2++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(index1<nums1Size){
|
||||||
|
while(index1<nums1Size){
|
||||||
|
v[v_index++]=nums1[index1++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(index2<nums2Size){
|
||||||
|
while(index2<nums2Size){
|
||||||
|
v[v_index++]=nums2[index2++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(v_index==1){
|
||||||
|
return v[0];
|
||||||
|
}
|
||||||
|
if(v_index%2==0){
|
||||||
|
double n1,n2;
|
||||||
|
n1=v[v_index/2];
|
||||||
|
n2=v[(v_index/2)-1];
|
||||||
|
return (n1+n2)/2;
|
||||||
|
}
|
||||||
|
int new_index=(int)v_index/2;
|
||||||
|
int i=0;
|
||||||
|
return v[new_index];
|
||||||
|
}
|
||||||
|
|
23
leetcode/src/647.c
Normal file
23
leetcode/src/647.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
|
||||||
|
/* Author : Saurav Dubey */
|
||||||
|
|
||||||
|
|
||||||
|
int countSubstrings(char* s) {
|
||||||
|
int len = strlen(s);
|
||||||
|
int i;
|
||||||
|
int count = 0;
|
||||||
|
for( i=0; i<len; i++){
|
||||||
|
// cases handled for both odd and even lenghted Palindrome
|
||||||
|
|
||||||
|
count += countPalin(s, i, i, len);
|
||||||
|
if(i!=len-1)
|
||||||
|
count += countPalin(s, i, i+1, len);
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
int countPalin(char *s, int head, int tail, int len){
|
||||||
|
int ret = (s[head]==s[tail])? 1:0 ;
|
||||||
|
if( ret && head-1>=0 && tail+1<len)
|
||||||
|
ret += countPalin(s, head-1, tail+1, len);
|
||||||
|
return ret;
|
||||||
|
}
|
@ -29,7 +29,7 @@ void swap(int *first, int *second){
|
|||||||
void bubbleSort(int arr[], int size){
|
void bubbleSort(int arr[], int size){
|
||||||
|
|
||||||
for(int i=0; i<size-1; i++) {
|
for(int i=0; i<size-1; i++) {
|
||||||
for(int j=0; j<size-1; j++) {
|
for(int j=0; j<size-1-i; j++) {
|
||||||
if(arr[j]>arr[j+1]) {
|
if(arr[j]>arr[j+1]) {
|
||||||
swap(&arr[j], &arr[j+1]);
|
swap(&arr[j], &arr[j+1]);
|
||||||
}
|
}
|
||||||
|
95
sorting/random_quick_sort.c
Normal file
95
sorting/random_quick_sort.c
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
/*
|
||||||
|
Randomised quick sort implementation in C language.
|
||||||
|
In normal quick sort, pivot chosen to partition is either the first or the last element of the array.
|
||||||
|
This can take time O(n*n) to sort in the worst case.
|
||||||
|
Now in randomised quick sort, pivot is randomly chosen and then recursively sort the left and right sub-arrays.
|
||||||
|
The expected running time of the algorithm is O(nlog(n)).
|
||||||
|
*/
|
||||||
|
#include<stdio.h>
|
||||||
|
#include<stdlib.h>
|
||||||
|
#include<time.h>
|
||||||
|
|
||||||
|
int getBig(int a[], int i, int right, int pivot)
|
||||||
|
{
|
||||||
|
for(int k = i; k <= right; k++)
|
||||||
|
{
|
||||||
|
if (a[k] > pivot)
|
||||||
|
return k;
|
||||||
|
}
|
||||||
|
return right+1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getSmall(int a[], int j, int left, int pivot)
|
||||||
|
{
|
||||||
|
for(int k = j; k >= left; k--)
|
||||||
|
{
|
||||||
|
if (a[k] < pivot)
|
||||||
|
return k;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void swap(int *a, int *b)
|
||||||
|
{
|
||||||
|
int t = *a;
|
||||||
|
*a = *b;
|
||||||
|
*b = t;
|
||||||
|
}
|
||||||
|
|
||||||
|
void random_quick(int a[], int left, int right)
|
||||||
|
{
|
||||||
|
if (left>=right)
|
||||||
|
return;
|
||||||
|
int index = left + (rand()%(right-left)), i = left, j = right;
|
||||||
|
int pivot_index = index;
|
||||||
|
int pivot = a[index];
|
||||||
|
// storing index of element greater than pivot
|
||||||
|
i = getBig(a, i, right, pivot);
|
||||||
|
// storing index of element smaller than pivot
|
||||||
|
j = getSmall(a, j, left, pivot);
|
||||||
|
while(i <= j)
|
||||||
|
{
|
||||||
|
swap(&a[i], &a[j]);
|
||||||
|
i = getBig(a, i, right, pivot);
|
||||||
|
j = getSmall(a, j, left, pivot);
|
||||||
|
}
|
||||||
|
// after separating the smaller and greater elements, there are 3 cases possible
|
||||||
|
if(pivot_index>j && pivot_index>i)
|
||||||
|
{
|
||||||
|
// case 1. When the pivot element index is greater than both i and j
|
||||||
|
swap(&a[i], &a[pivot_index]);
|
||||||
|
random_quick(a, left, i-1);
|
||||||
|
random_quick(a, i+1, right);
|
||||||
|
}
|
||||||
|
else if (pivot_index<j && pivot_index<i)
|
||||||
|
{
|
||||||
|
// case 2. When the pivot element index is smaller than both i and j
|
||||||
|
swap(&a[j], &a[pivot_index]);
|
||||||
|
random_quick(a, left, j-1);
|
||||||
|
random_quick(a, j+1, right);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// the pivot element is at its origin position.
|
||||||
|
random_quick(a, left, pivot_index-1);
|
||||||
|
random_quick(a, pivot_index+1, right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
srand(time(0));
|
||||||
|
int num;
|
||||||
|
scanf("%d", &num);
|
||||||
|
int arr[num];
|
||||||
|
for(int i = 0; i < num; i++)
|
||||||
|
{
|
||||||
|
scanf("%d", &arr[i]);
|
||||||
|
}
|
||||||
|
random_quick(arr, 0, num-1);
|
||||||
|
for(int i = 0; i < num; i++)
|
||||||
|
{
|
||||||
|
printf("%d ", arr[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user