aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sort_arr_incr_freq.c42
-rw-r--r--sort_arr_incr_freq.py25
2 files changed, 67 insertions, 0 deletions
diff --git a/sort_arr_incr_freq.c b/sort_arr_incr_freq.c
new file mode 100644
index 0000000..20beeb4
--- /dev/null
+++ b/sort_arr_incr_freq.c
@@ -0,0 +1,42 @@
+// 1636. Sort Array by Increasing Frequency
+#include "leetcode.h"
+
+/*
+ * given an array of integers 'nums', sort the array in increasing order based
+ * on the frequency of the values. if multiple values have the same frequency,
+ * sort them in decreasing order. return the sorted array.
+ */
+
+int cmp(const void *a, const void *b, void *cnt) {
+ int *cnt_arr = (int *)cnt;
+ int a_val = *(int *)a, b_val = *(int *)b;
+ return cnt_arr[a_val + 100] == cnt_arr[b_val + 100]
+ ? b_val - a_val
+ : cnt_arr[a_val + 100] - cnt_arr[b_val + 100];
+}
+
+int *frequencySort(int *nums, int numsSize, int *returnSize) {
+ int cnt[201] = {0};
+ for (int i = 0; i < numsSize; ++i)
+ ++cnt[nums[i] + 100];
+ qsort_r(nums, numsSize, sizeof(int), cmp, cnt); // requires _GNU_SOURCE
+ return nums;
+}
+
+int main() {
+ int n1[] = {1, 1, 2, 2, 2, 3}, n2[] = {2, 3, 1, 3, 2},
+ n3[] = {-1, 1, -6, 4, 5, -6, 1, 4, 1}, rs1, rs2, rs3;
+ int *fs1 = frequencySort(n1, ARRAY_SIZE(n1), &rs1);
+ int *fs2 = frequencySort(n2, ARRAY_SIZE(n2), &rs2);
+ int *fs3 = frequencySort(n3, ARRAY_SIZE(n3), &rs3);
+ for (int i = 0; i < rs1; i++)
+ printf("%d ", fs1[i]); // expect: 3,1,1,2,2,2
+ printf("\n");
+ for (int i = 0; i < rs2; i++)
+ printf("%d ", fs2[i]); // expect: 1,3,3,2,2
+ printf("\n");
+ for (int i = 0; i < rs3; i++)
+ printf("%d ", fs3[i]); // expect: 5,-1,4,4,-6,-6,1,1,1
+ printf("\n");
+ free(fs1), free(fs2), free(fs3);
+}
diff --git a/sort_arr_incr_freq.py b/sort_arr_incr_freq.py
new file mode 100644
index 0000000..65c9d1a
--- /dev/null
+++ b/sort_arr_incr_freq.py
@@ -0,0 +1,25 @@
+# 1636. Sort Array by Increasing Frequency
+from collections import Counter
+
+"""
+given an array of integers 'nums', sort the array in increasing order based
+on the frequency of the values. if multiple values have the same frequency,
+sort them in decreasing order. return the sorted array.
+"""
+
+
+class Solution(object):
+ def frequencySort(self, nums):
+ """
+ :type nums: List[int]
+ :rtype: List[int]
+ """
+ cnt = Counter(nums)
+ return sorted(nums, key=lambda n: (cnt[n], -n))
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.frequencySort(nums=[1, 1, 2, 2, 2, 3]))
+ print(obj.frequencySort(nums=[2, 3, 1, 3, 2]))
+ print(obj.frequencySort(nums=[-1, 1, -6, 4, 5, -6, 1, 4, 1]))