## Thursday, June 26, 2014

### YahtzeeScore

Problem:
Overview:

Given a dice roll, determine which set of die values adds up to the greatest score.

Java Source:
```01: /*
02: TopCoder
03: Single Round Match: 146
04: Division: 2
05: Level: 1
06: Points: 250 Points
07: Description: http://community.topcoder.com/stat?c=problem_statement&pm=1692
08:  */
09:
10: import java.util.*;
11:
12: public class YahtzeeScore {
13:
14:     public int maxPoints(int[] toss) {
15:
16:         // Holds die points, and the sum of those points.
17:         Map score = new HashMap();
18:
19:         // Loop through each toss.
20:         for (int i = 0; i < toss.length; i++) {
21:             int d = toss[i];
22:
23:             Integer s = score.get(d);
24:
25:             // Add or update total for that value.
26:             if (s == null) {
27:                 score.put(d, d);
28:             } else {
29:                 score.put(d, score.get(d) + d);
30:             }
31:         }
32:
33:         // Find the largest score.
34:         int maxScore = 0;
35:         for (Integer s : score.values()) {
36:             if (s > maxScore) {
37:                 maxScore = s;
38:             }
39:         }
40:
41:         return maxScore;
42:     }
43: }
```
Notes:

This solution utilizes a HashMap to store each die value along with the total off all dice of that value. So, if roll is 1,3,3,1,5 then the map will look like this:

 1 : 2 3 : 6 5 : 5

It then loops through all the values in the map (line 35) and returns the largest score.