返回介绍

solution / 1600-1699 / 1636.Sort Array by Increasing Frequency / README_EN

发布于 2024-06-17 01:03:16 字数 4217 浏览 0 评论 0 收藏 0

1636. Sort Array by Increasing Frequency

中文文档

Description

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_.

 

Example 1:

Input: nums = [1,1,2,2,2,3]
Output: [3,1,1,2,2,2]
Explanation: '3' has a frequency of 1, '1' has a frequency of 2, and '2' has a frequency of 3.

Example 2:

Input: nums = [2,3,1,3,2]
Output: [1,3,3,2,2]
Explanation: '2' and '3' both have a frequency of 2, so they are sorted in decreasing order.

Example 3:

Input: nums = [-1,1,-6,4,5,-6,1,4,1]
Output: [5,-1,4,4,-6,-6,1,1,1]

 

Constraints:

  • 1 <= nums.length <= 100
  • -100 <= nums[i] <= 100

Solutions

Solution 1

class Solution:
  def frequencySort(self, nums: List[int]) -> List[int]:
    cnt = Counter(nums)
    return sorted(nums, key=lambda x: (cnt[x], -x))
class Solution {
  public int[] frequencySort(int[] nums) {
    int[] cnt = new int[201];
    List<Integer> t = new ArrayList<>();
    for (int v : nums) {
      v += 100;
      ++cnt[v];
      t.add(v);
    }
    t.sort((a, b) -> cnt[a] == cnt[b] ? b - a : cnt[a] - cnt[b]);
    int[] ans = new int[nums.length];
    int i = 0;
    for (int v : t) {
      ans[i++] = v - 100;
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> frequencySort(vector<int>& nums) {
    vector<int> cnt(201);
    for (int v : nums) {
      ++cnt[v + 100];
    }
    sort(nums.begin(), nums.end(), [&](const int a, const int b) {
      if (cnt[a + 100] == cnt[b + 100]) return a > b;
      return cnt[a + 100] < cnt[b + 100];
    });
    return nums;
  }
};
func frequencySort(nums []int) []int {
  cnt := make([]int, 201)
  for _, v := range nums {
    cnt[v+100]++
  }
  sort.Slice(nums, func(i, j int) bool {
    a, b := nums[i]+100, nums[j]+100
    return cnt[a] < cnt[b] || cnt[a] == cnt[b] && a > b
  })
  return nums
}
function frequencySort(nums: number[]): number[] {
  const map = new Map<number, number>();
  for (const num of nums) {
    map.set(num, (map.get(num) ?? 0) + 1);
  }
  return nums.sort((a, b) => map.get(a) - map.get(b) || b - a);
}
use std::collections::HashMap;
impl Solution {
  pub fn frequency_sort(mut nums: Vec<i32>) -> Vec<i32> {
    let n = nums.len();
    let mut map = HashMap::new();
    for &num in nums.iter() {
      *map.entry(num).or_insert(0) += 1;
    }
    nums.sort_by(|a, b| {
      if map.get(a) == map.get(b) {
        return b.cmp(a);
      }
      map.get(a).cmp(&map.get(b))
    });
    nums
  }
}
/**
 * @param {number[]} nums
 * @return {number[]}
 */
var frequencySort = function (nums) {
  const m = new Map();
  for (let i = 0; i < nums.length; i++) {
    m.set(nums[i], (m.get(nums[i]) || 0) + 1);
  }
  nums.sort((a, b) => (m.get(a) != m.get(b) ? m.get(a) - m.get(b) : b - a));
  return nums;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文