1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| class Solution { public int numRabbits(int[] answers) { int ans = 0; if(answers.length == 0){ return 0; } Arrays.sort(answers); int k = 0; for(int i = 0; i < answers.length; i++){ if(k == 0){ ans += answers[i] + 1; k = answers[i]; continue; }
if(answers[i] == answers[i-1] && k > 0){ k--; continue; }
if(answers[i] != answers[i-1]){ ans += answers[i] + 1; k = answers[i]; continue; } } return ans; } }
|