简体   繁体   English

找到差值小于 k 的最小数组组

[英]Find minimum group of array with difference less than k

I am working on code where I need to find the count of array group with difference between the elements of the array group should be less than k我正在编写代码,我需要找到数组组的计数,数组组的元素之间的差异应该小于 k

Example The numbers of awards per movie are awards = [1, 5, 4, 6, 8, 9, 2], and the maximum allowed difference is k = 3.示例 每部电影的奖项数量为 awards = [1, 5, 4, 6, 8, 9, 2],最大允许差值为 k = 3。

  • One way to divide the movies into the minimum number of groups is:将电影分成最少数量的组的一种方法是:
    The first group can contain [2, 1].第一组可以包含 [2, 1]。 The maximum difference between awards of any two movies is 1 which does not exceed k.任意两部电影的奖项之间的最大差异为1,不超过k。

  • The second group can contain [5, 4, 6].第二组可以包含 [5, 4, 6]。 The maximum difference between awards of any two movies is 2 which does not exceed k任意两部电影的奖项最多相差2,不超过k

  • The third group can contain [8, 9].第三组可以包含 [8, 9]。 The maximum difference between awards of any two movies is 1 which does not exceed k.任意两部电影的奖项之间的最大差异为1,不超过k。 The movies can be divided into a minimum of 3 groups.电影至少可以分为 3 组。

below is my code But it is not working.下面是我的代码,但它不起作用。 What I am doinng wrong.我做错了什么。 Please help me.请帮我。

function minimumGroups(arr, k) {
    // Write your code here
arr.sort();
  let start = 0;
  if(arr.length == 0)
    return 0;
  // If arr has some value then at least can form 1 group
  let count = 1;
  for(let i = 0; i < arr.length; i++) {
    if(arr[i] - arr[start] > k) {
      count++;
      start = i;
    }
  }
  return count;
}

Some of hidden test cases are not passing for same scenario一些隐藏的测试用例没有通过相同的场景

Arr =[ 1, 13, 6, 8, 9, 3, 5 ] and K= 4 Expected output is 3 but I am getting 2 Arr =[ 1, 13, 6, 8, 9, 3, 5 ] and K= 4 预期 output 是 3 但我得到 2

This code snippet fixes the code by update the .sort() .此代码片段通过更新.sort()来修复代码。

 function minimumGroups(arr, k) { // Write your code here arr.sort((a, b) => ab); // this line is updated. let start = 0; if(arr.length == 0) return 0; // If arr has some value then at least can form 1 group let count = 1; for(let i = 0; i < arr.length; i++) { if(arr[i] - arr[start] > k) { count++; start = i; } } return count; }; console.log(minimumGroups([ 1, 13, 6, 8, 9, 3, 5 ], 4));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找到k个彼此之间差异最小的数组元素 - Find k number of array elements having the minimum difference amongst themselves 如何检查数组下的值是否存在且差值是否小于0.000002 - How to check if the value is present under array and difference is less than of 0.000002 按日期分组并在 JSON 对象数组中找到温度的最小值和最大值 - Group by Date and find the minimum and maximum values of temperature in JSON array of objects 如何找到小于O(n)的数组中出现次数最多的数字? - How to find a number that occurs the most in an array in less than O(n)? 如何找到小于Java数组中x的第一个元素? - How to find first element that's less than the x in an array in Javascript? 在组中找到最低级别并合并 - Find minimum level in group and combine 如何找到两个数组比较的数组索引并返回一个数组项小于另一个? - How find the array index for two array comparision and return once array item less than another? 检查时差是否小于 1 秒 - Check if the time difference is less than 1 second in moment 如果用户输入的长度小于最小长度,则显示错误消息 - Display error message if user entered less than minimum length Javascript:最短日期始终小于任何其他日期 - Javascript: the minimum date that is always less than any other date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM