From 7f350fd507b8f017d50d300aab812e41419bb6c0 Mon Sep 17 00:00:00 2001 From: Sayam Kumar Date: Tue, 1 Oct 2019 20:25:15 +0530 Subject: [PATCH 1/7] Create random_quick_sort.c --- sorting/random_quick_sort.c | 95 +++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 sorting/random_quick_sort.c 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 Date: Wed, 2 Oct 2019 14:37:54 +0530 Subject: [PATCH 2/7] added median of two sorted arrays --- leetcode/README.md | 1 + leetcode/src/4.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 leetcode/src/4.c diff --git a/leetcode/README.md b/leetcode/README.md index 155d3f37..05732280 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -7,6 +7,7 @@ LeetCode | # | Title | Solution | Difficulty | |---| ----- | -------- | ---------- | |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| |27|[Remove Element](https://leetcode.com/problems/remove-element/) | [C](./src/27.c)|Easy| |24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [C](./src/24.c)|Medium| 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 Date: Wed, 2 Oct 2019 14:39:17 +0530 Subject: [PATCH 3/7] fix with readme --- leetcode/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/leetcode/README.md b/leetcode/README.md index 05732280..1a9cb957 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -7,7 +7,7 @@ LeetCode | # | Title | Solution | Difficulty | |---| ----- | -------- | ---------- | |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| +|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| |27|[Remove Element](https://leetcode.com/problems/remove-element/) | [C](./src/27.c)|Easy| |24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [C](./src/24.c)|Medium| From 28c59fc7c40082f505985d84bf25f20e65756029 Mon Sep 17 00:00:00 2001 From: shivam agarwal <43515429+shivamagarwal1999@users.noreply.github.com> Date: Thu, 3 Oct 2019 17:01:53 +0530 Subject: [PATCH 4/7] Update BubbleSort.c --- sorting/BubbleSort.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting/BubbleSort.c b/sorting/BubbleSort.c index 0b6a1879..fb025988 100644 --- a/sorting/BubbleSort.c +++ b/sorting/BubbleSort.c @@ -29,7 +29,7 @@ void swap(int *first, int *second){ void bubbleSort(int arr[], int size){ for(int i=0; iarr[j+1]) { swap(&arr[j], &arr[j+1]); } From 031ae5f1d24eeea2bcb86e999c604f6f8e020f05 Mon Sep 17 00:00:00 2001 From: hai dang Date: Thu, 10 Oct 2019 11:34:58 -0700 Subject: [PATCH 5/7] Add some solution leetcode --- leetcode/README.md | 5 ++++- leetcode/src/1089.c | 20 ++++++++++++++++++++ leetcode/src/1184.c | 16 ++++++++++++++++ leetcode/src/1207.c | 25 +++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 leetcode/src/1089.c create mode 100644 leetcode/src/1184.c create mode 100644 leetcode/src/1207.c diff --git a/leetcode/README.md b/leetcode/README.md index a9bf3c79..bffa4aa3 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -63,4 +63,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| -|1189|[Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/) | [C](./src/1189.c)|Easy| \ No newline at end of file +|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| 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; +} From eccc3c126f75e18fe07724baff5a76e8c38159cd Mon Sep 17 00:00:00 2001 From: Saurav Kumar Dubey <37882053+sauravkdubey@users.noreply.github.com> Date: Fri, 11 Oct 2019 15:17:20 +0530 Subject: [PATCH 6/7] update --- leetcode/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/leetcode/README.md b/leetcode/README.md index bffa4aa3..0e199d67 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -50,6 +50,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| From e5adce3a52c41fe5fb5be1c1cd2a36476f4e8fe7 Mon Sep 17 00:00:00 2001 From: Saurav Kumar Dubey <37882053+sauravkdubey@users.noreply.github.com> Date: Fri, 11 Oct 2019 15:22:02 +0530 Subject: [PATCH 7/7] 647.c Palindromic substring using dynamic programming --- leetcode/src/647.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 leetcode/src/647.c diff --git a/leetcode/src/647.c b/leetcode/src/647.c new file mode 100644 index 00000000..d78d2439 --- /dev/null +++ b/leetcode/src/647.c @@ -0,0 +1,23 @@ + +/* Author : Saurav Dubey */ + + +int countSubstrings(char* s) { + int len = strlen(s); + int i; + int count = 0; + for( i=0; i=0 && tail+1