I got accepted by two lines of code...
public int findMin(int[] num)
{
Arrays.sort(num);
return num[0];
}
We have to find a O(N) solution, right?
I have another solution, it looks bad but it solves the problem in O(N).
public int findMin(int[] num)
{
boolean flag = false;
for (int i = 0; i < num.length; i++)
{
if (num[i % num.length] != num[(i+1) % num.length])
{
flag = true;
}
}
int result = num[0];
if (flag)
{
for (int i = 0; i < num.length; i++)
{
if (num[i % num.length] > num[(i+1) % num.length])
{
result = num[(i+1) % num.length];
break;
}
}
}
return result;
}