Lesson4 - FrogRiverOne

作成日:2015/5/16
Task Score:100%
FrogRiverOne coding task - Learn to Code - Codility

// you can also use imports, for example:
// import java.util.*;

// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.HashMap;

class Solution {

    public static  int solution(int X, int[] A) {

      int goal = 0;
      int comp_goal = 0;
      HashMap<Integer,Boolean> hash = new HashMap<Integer,Boolean>();

      do {
        goal += X;
      } while(X-- > 0);

      for(int i = 0; i < A.length; i++) {
        if (!hash.containsKey(A[i])) {
          hash.put(A[i], true);
          comp_goal += A[i];
        }
        if (comp_goal != 0 && comp_goal == goal) {
          return i;
        }
      }

      return -1;
    }
}