Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New submission #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Submitted PeopleGroupBySize.java
  • Loading branch information
akash-vartak authored Oct 1, 2020
commit b83a0a7e09168da9e0ceaaf8f964de059b7d68da
73 changes: 73 additions & 0 deletions problems/src/array/PeopleGroupBySize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Submitted by: Akash Vartak https://github.com/akash-vartak
Problem: https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/
Difficulty: Medium
*/

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class PeopleGroupBySize
{
public static void main(String args[]) throws IOException
{
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));

int a[] = {2,1,3,3,3,2};

List<List<Integer>> ret = groupThePeople(a);

for(int i=0;i<ret.size();i++)
{
List<Integer> list = ret.get(i);
for(int j=0;j<list.size();j++)
{
System.out.print(list.get(j) + " ");
}
System.out.println();
}
}

public static List<List<Integer>> groupThePeople(int[] groupSizes)
{
List<List<Integer>> combined = new ArrayList<List<Integer>>();

boolean done[] = new boolean[groupSizes.length];
for(int i=0;i<groupSizes.length;i++)
{
done[i] = false;
}

List<Integer> list1 = new ArrayList<Integer>();
for(int i=0;i<done.length;i++)
{
if(!done[i])
{
// System.out.println("i:"+i+" groupSizes:"+groupSizes[i]+" done:"+done[i]);
list1.add(i);
int maxSize = groupSizes[i]-1;
for(int j=(i+1);j<done.length;j++)
{
// System.out.println("\tj:"+j);
if(maxSize<=0)
break;
if(!done[i] && groupSizes[j]==groupSizes[i])
{
// System.out.println("\tj:"+j+" groupSizes:"+groupSizes[j]+" done:"+done[j]+" maxSize:"+maxSize);
list1.add(j);
maxSize -= 1;
done[j] = true;
}
}
done[i] = true;
combined.add(list1);
list1 = new ArrayList<Integer>();
}
}

return combined;
}
}