Connection Status:
Competition Arena > SantaGifts
SRM 331 · 2006-12-21 · by slex · Simulation, String Manipulation
Class Name: SantaGifts
Return Type: String[]
Method Name: distribute
Arg Types: (vector<string>, int)
Problem Statement

Problem Statement

Santa Claus has come with a bag full of presents this year. A group of N kids stand in line, anxious to get their gifts. Santa Claus takes a present from his bag and gives it to the kid who is currently first in line. That kid then goes to the end of the line, unless he already has 4 gifts, in which case, he goes home. This process continues as long as there are presents in the bag and kids in the line.

You are given a String[] gifts containing the presents in Santa's bag, in the order they are given away. Return a String[] containing exactly N elements, where the k-th element is a single space separated list of presents received by the k-th kid, in the order he received them. Kid 0 is the first kid in the initial lineup, kid 1 is the second kid, etc.

Constraints

  • gifts will contain between 1 and 50 elements, inclusive.
  • Each element of gifts will contain between 1 and 10 lowercase letters ('a'-'z'), inclusive.
  • N will be between 1 and 50, inclusive.
Examples
0)
{"ball","plane","robot","puzzle"}
3
Returns: {"ball puzzle", "plane", "robot" }

The first kid gets a ball, the second one a plane, and the third one a robot. Then the first kid reaches the front of the line again and gets the last gift.

1)
{"ball","plane","robot","puzzle","bike"}
1
Returns: {"ball plane robot puzzle" }

There is only one kid, so he will take the first four presents.

2)
{"ball","ball","plane","plane"}
2
Returns: {"ball plane", "ball plane" }

In this case Santa Claus is fair and gives a ball and a plane to both kids.

3)
{"ball","plane","robot"}
5
Returns: {"ball", "plane", "robot", "", "" }

Unfortunately, not all the kids will get gifts.

4)
{"dsoggb","zep","ldxkwwjg","erkbrzb","jrrmwxcbzk","noihltl","jhdq","orvustvqqr","nyliepmu","shko","bwc","fwfnjkw"}
33
Returns: {"dsoggb", "zep", "ldxkwwjg", "erkbrzb", "jrrmwxcbzk", "noihltl", "jhdq", "orvustvqqr", "nyliepmu", "shko", "bwc", "fwfnjkw", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" }

Submissions are judged against all 68 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class SantaGifts with a public method vector<string> distribute(vector<string> gifts, int N) · 68 test cases · 2 s / 256 MB per case

Submitting as anonymous