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
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ My accepted leetcode solutions to some of the common interview problems.
- [Meeting Scheduler](problems/src/array/MeetingScheduler.java) (Medium)
- [Minimum Swaps to Group All 1's Together](problems/src/array/MinimumSwapsToGroupAll1Together.java) (Medium)
- [Array Nesting](problems/src/array/ArrayNesting.java) (Medium)
- [Group the People Given the Group Size They Belong To](problems/src/array/PeopleGroupBySize.java) (Medium)

#### [Backtracking](problems/src/backtracking)

Expand Down
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;
}
}