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; +}