任意两名学生之间的年龄差的最小组数最多为1。

huangapple go评论64阅读模式
英文:

minimum number of groups of kids with difference of ages between any 2 students is at most 1

问题

我需要帮助处理以下代码,我正在编写一段代码,以最小化聚会上儿童年龄的分组数量,即组内任意两名儿童年龄之间的差异应为0/1。

示例:

儿童总数 = 13

儿童年龄 = [2,2,2,3,3,3,4,4,4,5,5,6,7]

输出

[2,2,2,3,3,3] - 第1组
[4,4,4,5,5] - 第2组
[6,7] - 第3组

我们无需打印所有三个数组,只需输出分组数目,您可以检查我的代码,告诉我是否有错误。

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("输入儿童数量:");
    int n = scan.nextInt();
    int[] kids = new int[n];
    System.out.println("输入儿童年龄:");
    for (int i = 0; i < n; i++) {
        kids[i] = scan.nextInt();
    }
    Arrays.sort(kids);
    int groups = 0;
    int i = 0;
    int k = 0;
    int a = 0;
    int b = 0;
    for (i = a; i < kids.length - a; i++) {
        for (k = b + 1; k < kids.length - b; k++) {
            if (kids[k] - kids[i] != 0 || kids[k] - kids[i] != 1) {
                groups++;
                kids[i] = kids[k];
                a++;
                b++;
            }
        }
    }
    System.out.println(groups);
}
英文:

I need help with the following code,
I am writing a code to minimize the number of groups of the ages of children at a party, that is the difference between the ages of any two children in a group is supposed to be 0/1.

Example:

total number of children = 13

ages of the children = [2,2,2,3,3,3,4,4,4,5,5,6,7]

output:

[2,2,2,3,3,3] - group1
[4,4,4,5,5]- group2 
[6,7] - group3

We don't have to print all three arrays, the number of groups will suffice, kindly check my code and tell me if there is something wrong

public static void main(String[] args) {
		
    Scanner scan = new Scanner(System.in);
   	System.out.println(&quot;Enter the number of Kids :&quot;);
   	int n = scan.nextInt();
   	int[] kids = new int[n];
   	System.out.println(&quot;Enter the ages of kids :&quot;);
   	for(int i = 0;i&lt;n;i++) {
   		kids[i] = scan.nextInt();
   	}
   	Arrays.sort(kids);
   	int groups =0;
   	int i =0;
   	int k =0;
   	int a = 0;
   	int b =0;
   	for(i=a;i&lt;kids.length-a;i++) {
   		for(k=b+1;k&lt;kids.length-b;k++) {
   			if(kids[k]-kids[i]!=0 || kids[k]-kids[i]!=1) {
   				groups++;
   				kids[i] = kids[k];
   				a++;
   				b++;
   			}
   				
   		}
   	}
   	System.out.println(groups);
}
    		

答案1

得分: 2

你可以在平均情况下以O(n*log(n))时间复杂度进行排序,以O(n)时间复杂度进行分组计算

在循环中保持一个变量,其中包含当前孩子的年龄。如果超过任何一个孩子的年龄,则增加分组并将该变量更新为当前年龄。

请查看下面的代码,以更好地理解。

public class Main {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the number of Kids:");
        int n = scan.nextInt();
        int[] kids = new int[n];
        System.out.println("Enter the ages of kids:");
        for(int i = 0; i < n; i++) {
            kids[i] = scan.nextInt();
        }
        Arrays.sort(kids);

        int groups = 1;
        int i = 0;
        int a = kids[i];

        for(i = 0; i < kids.length; i++) {
            if(kids[i] - a > 1){
                a = kids[i];
                groups = groups + 1;
            }    
        }

        System.out.println(groups);

    }
}
英文:

You can do it within O(n*log(n)) time complexity on an average for sorting and in O(n) time complexity for group calculation.

Maintain a variable that contains the current kid's age in a loop. If it exceeds any kid's age increment groups and updates that variable with the current one.

Take a look at the code below to have a better understanding.

public class Main {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        System.out.println(&quot;Enter the number of Kids :&quot;);
        int n = scan.nextInt();
        int[] kids = new int[n];
        System.out.println(&quot;Enter the ages of kids :&quot;);
        for(int i = 0;i&lt;n;i++) {
            kids[i] = scan.nextInt();
        }
        Arrays.sort(kids);
        
        int groups = 1;
        int i = 0;
        int a = kids[i];
        
        for(i=0;i&lt;kids.length;i++) {
            if(kids[i] - a &gt; 1){
                a = kids[i];
                groups = groups + 1;
            }    
        }
        
        System.out.println(groups);
        
        
    }
}

答案2

得分: 0

抱歉,我只能提供英文内容的翻译。以下是您提供的内容的翻译:

抱歉我使用了非常通俗的 Java 代码,但我只是想演示逻辑。如果我们只需要计算数组的数量,那么在排序之后可以在线性时间内完成,因此复杂度为 O(nlogn),由于排序造成。

public static void main(String[] args) {

    int[] arr = new int[] { 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 7 };
    int maxArrays = 0;
    Arrays.sort(arr);
    int maxLength = removeDuplicates(arr);

    for (int i = 1; i < maxLength; i++) {
        maxArrays++;
        if (arr[i] - arr[i - 1] == 1)
            i++; // shift
    }

    System.out.println(maxArrays);
}

private static int removeDuplicates(int[] arr) {

    int index = 1;
    for (int i = 1; i < arr.length; i++) {
        if (arr[i] != arr[i - 1]) {
            arr[index] = arr[i];
            index++;
        }
    }

    return index;
}

如果您需要进一步的协助,请随时提问。

英文:

Sorry for using very layman Java, but I wanted to just demonstrate the logic. If we just need the count of arrays, then this can be done in linear time after sorting, so complexity is O(nlogn) due to sort.

public static void main(String[] args) {

	int[] arr = new int[] { 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 7 };
	int maxArrays = 0;
	Arrays.sort(arr);
	int maxLength = removeDuplicates(arr);

	for (int i = 1; i &lt; maxLength; i++) {
		maxArrays++;
		if (arr[i] - arr[i - 1] == 1)
			i++;// shift
	}

	System.out.println(maxArrays);
}

private static int removeDuplicates(int[] arr) {

	int index = 1;
	for (int i = 1; i &lt; arr.length; i++) {
		if (arr[i] != arr[i - 1]) {
			arr[index] = arr[i];
			index++;
		}
	}

	return index;
}

答案3

得分: 0

如果你只关心你可以在线性时间内完成的分组数量:

int[] kids = {2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7};
Set<Integer> groups = new HashSet<>();

for (int k = 0; k < kids.length; k++) {
    // 键始终为偶数值(2->2,3->2,4->4,...)
    int key = kids[k] - kids[k] % 2;
    // 对于Set.add,不需要检查!groups.contains(key),因为它保证不会添加重复项。
    groups.add(key);
}
System.out.println("分组数量:" + groups.size());

该集合包含偶数年龄,其中存在具有年龄键或键+1的儿童。此示例的输出为3。

如果您还想在每个组中保留儿童年龄,请改用HashMap而不是Set:

HashMap<Integer, List<Integer>> groups;

键是偶数年龄与集合中的相同),值是儿童年龄列表
英文:

If you're only interested in the number of groups you can do it in a linear time:

int[] kids= {2,2,2,2,3,3,4,4,5,5,6,7};
Set&lt;Integer&gt; groups=new HashSet&lt;&gt;();

for (int k=0;k&lt;kids.length;k++)
{
    // key is always the even value (2-&gt;2, 3-&gt;2, 4-&gt;4,...)
	int key=kids[k]-kids[k]%2;
	// check on !groups.contains(key) is not necessary as Set.add guarantees no duplicates are added.
	groups.add(key);
}
System.out.println(&quot;Number of groups:&quot;+groups.size());

The set contains the even ages wherefor children exist with age key or key+1 in the list.
The output for this example is 3.

If you also want to keep the childrens ages in every group, use a HashMap instead of a Set:

HashMap&lt;Integer,List&lt;Integer&gt;&gt; groups;

in which key is the even age (as in the set) and the value is a list of children's ages.

huangapple
  • 本文由 发表于 2020年10月19日 20:55:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64427789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定