1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.*; class Solution { public int solution(int X, int[] A) { HashSet<Integer> container = new HashSet<Integer>(); for (int i = 0; i < A.length; i++){ if (A[i] >= 1 && A[i] <= X){ container.add(A[i]); } if (container.size() == X) return i; } return -1; } } |

A quick correction: I am not skipping the 3rd items. After sorting, I skip all the items from the 3rd position to the 4th last position. For example, with sorted content [x0, x1, x2, x3, x4, x5, x6, x7], I ignored x2, x3, and x4.

]]>1. For [-1, -2, -3, -4, 1, 2], [1, 3] is NOT holding the global minimum average.

2. My whole article is trying to prove that MA exists in 2- or 3-item slice. Please read before posting.

3. Please double read the problem description: If there is more than one slice with a minimal average, you should return the smallest starting position of such a slice.

]]>
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 30 31 32 33 34 |
import java.util.*; class Solution { public int solution(int[] A) { if(A.length==1) return 0; ArrayList<Integer> al=new ArrayList<Integer>(); for(int i=1;i<A.length-1;i++) { if(A[i]>A[i-1] && A[i]>A[i+1]) { al.add(i); } } int s=al.size(); if(s==1) return 1; if(s==0)return 0; s=(int) Math.ceil(Math.sqrt(A.length)); while(s>=0) { int lp=al.get(0); int c=1; for(int i=1;i<al.size();i++) { int d=Math.abs(al.get(i)-lp); if(d>=s) { lp=al.get(i); c++; if(c==s) return c; } } s--;} return 0;} } |