没有medium难度的 一道medium题,甚至也用不到动态规划,只要找规律和做一做四则运算就可以AC了。时间复杂度O(n),空间复杂度O(1)
class Solution {
public:
int clumsy(int N) {
if(N < 4)
{
switch(N){
case 3:
return 3*2/1;
case 2:
return 2*1;
case 1:
return 1;
case 0:
return 0;
}
}
int circle = N / 4;
int res = N % 4;
int temp=0;
for(int i = 0; i < circle; i++)
{
if(N < 4) break;
if(i == 0)temp = N*(N-1)/(N-2)+(N-3);
else temp = temp - N*(N-1)/(N-2) + (N-3);
N -= 4;
}
switch(res)
{
case 3:
temp -= 3*2/1;
break;
case 2:
temp -= 2*1;
break;
case 1:
temp -= 1;
break;
case 0:
break;
}
return temp;
}
};