HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Google Code Jam (Milkshakes) solution

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
jamgooglecodesolutionmilkshakes

Problem

For practice, I am working through some Google Code Jam problems. The code below is for the Milkshakes problem, which is located here.


Problem


You own a milkshake shop. There are N different flavors that you can
prepare, and each flavor can be prepared "malted" or "unmalted". So,
you can make \$2N\$ different types of milkshakes.


Each of your customers has a set of milkshake types that they like,
and they will be satisfied if you have at least one of those types
prepared. At most one of the types a customer likes will be a "malted"
flavor.


You want to make \$N\$ batches of milkshakes, so that:


There is exactly one batch for each flavor of milkshake, and it is
either malted or unmalted. For each customer, you make at least one
milkshake type that they like. The minimum possible number of batches
are malted. Find whether it is possible to satisfy all your customers
given these constraints, and if it is, what milkshake types you should
make. If it is possible to satisfy all your customers, there will be
only one answer which minimizes the number of malted batches.


Input



  • One line containing an integer \$C\$, the number of test cases in the


input file.



For each test case, there will be:



  • One line containing the


integer \$N\$, the number of milkshake flavors.

  • One line containing the


integer \$M\$, the number of customers.

  • \$M\$ lines, one for each customer, each containing:




  • An integer \$T >= 1\$, the number of milkshake types the


customer likes, followed by

  • \$T\$ pairs of integers "X Y", one for each


type the customer likes, where \$X\$ is the milkshake flavor between 1 and
\$N\$ inclusive, and \$Y\$ is either 0 to indicate unmalted, or 1 to indicated
malted. Note that:


  • No pair will occur more than once for a single


customer.

  • Each customer will have at least one flavor that they like


(\$T >= 1\$).

  • Each customer will like at most

Solution

Your go() method is waaaaaaaayyyyyyyyyyyyyy to long, break it up in digestible parts. Try to think in different abstraction levels, and don't mix them in one method (e.g. you deal with file input directly, but have a separate method for writing into a file). Follow the Single Responsibility Principle.

You should use extended for loops where possible, e.g.

for(int j = 0; j 

for(String customerPref : customerPrefs) {
   custRow.add(Integer.parseInt(customerPref));
}

Code Snippets

for(int j = 0; j < customerPrefs.length; ++j) {
   custRow.add(Integer.parseInt(customerPrefs[j]));
}

-->

for(String customerPref : customerPrefs) {
   custRow.add(Integer.parseInt(customerPref));
}

Context

StackExchange Code Review Q#4463, answer score: 3

Revisions (0)

No revisions yet.