diff --git a/leetcode/README.md b/leetcode/README.md index 0c370e42..3ac0dccd 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -8,6 +8,7 @@ LeetCode |---| ----- | -------- | ---------- | |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| +|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| |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| @@ -55,6 +56,7 @@ LeetCode |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| |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| |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| @@ -68,4 +70,7 @@ LeetCode |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| |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| +|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/) | [C](./src/1207.c)|Easy| \ No newline at end of file diff --git a/leetcode/src/1089.c b/leetcode/src/1089.c new file mode 100644 index 00000000..df1cfbb7 --- /dev/null +++ b/leetcode/src/1089.c @@ -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++; + } +} + + diff --git a/leetcode/src/1184.c b/leetcode/src/1184.c new file mode 100644 index 00000000..50aef4a4 --- /dev/null +++ b/leetcode/src/1184.c @@ -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; +} diff --git a/leetcode/src/1207.c b/leetcode/src/1207.c new file mode 100644 index 00000000..e8e830b0 --- /dev/null +++ b/leetcode/src/1207.c @@ -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; +} diff --git a/leetcode/src/4.c b/leetcode/src/4.c new file mode 100644 index 00000000..b5a2e830 --- /dev/null +++ b/leetcode/src/4.c @@ -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=0 && tail+1arr[j+1]) { swap(&arr[j], &arr[j+1]); } diff --git a/sorting/random_quick_sort.c b/sorting/random_quick_sort.c new file mode 100644 index 00000000..d4426a06 --- /dev/null +++ b/sorting/random_quick_sort.c @@ -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 +#include +#include + +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