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
| public class Solution { int max_kind = 0; public int distributeCandies(int[] nums) { permute(nums, 0); return max_kind; } public void permute(int[] nums, int l) { if (l == nums.length - 1) { HashSet < Integer > set = new HashSet < > (); for (int i = 0; i < nums.length / 2; i++) { set.add(nums[i]); } max_kind = Math.max(max_kind, set.size()); } for (int i = l; i < nums.length; i++) { swap(nums, i, l); permute(nums, l + 1); swap(nums, i, l); } } public void swap(int[] nums, int x, int y) { int temp = nums[x]; nums[x] = nums[y]; nums[y] = temp; } }
|