@LIUHUAN
2019-06-30T11:25:31.000000Z
字数 1992
阅读 941
algorithm
int minSteps(int n) {
vector<int> dp( n + 1 , 0);
for(int i = 2; i<= n; i++ ) {
dp[i] = i;
int m = i / 2;
for(int j = 1; j <= m; j++ ) {
if(i % j == 0 ) {
int c = i / j - 1;
c += 1;
dp[i] = min(dp[i],dp[j] +c );
}
}
}
return dp[n];
}
int brokenCalc(int X, int Y) {
if( X == Y )
return 0;
if( X > Y )
return X - Y;
if(Y & 0x01 ) {
return brokenCalc(X, Y + 1 ) + 1 ;
}else {
return brokenCalc(X, Y / 2 ) + 1;
}
return 0;
}
int triangleNumber(vector<int>& nums) {
int count = 0;
for (int i = 0; i < nums.length - 2; i++) {
for (int j = i + 1; j < nums.length - 1; j++) {
for (int k = j + 1; k < nums.length; k++) {
if (sum2judge(nums,i,j,k) && sum2judge(nums,i,k,j) && sum2judge(nums,j,k,i))
count++;
}
}
} I
return count;
}
int sum2judge(vector<int>& nums, int i,int j,int k) {
return nums[i] + nums[j] > nums[k];
}
int triangleNumber(vector<int>& nums) {
sort(nums.begin(),nums.end());
int n = nums.size();
int sum = 0;
for( int i = 0 ;i < n; i++ ) {
for( int j = i + 1;j < n; j++ ) {
int u = nums[i] + nums[j] - 1;
auto hi = upper_bound(nums.begin() + j + 1, nums.end(), u);
int c = hi - (nums.begin() + j + 1);
if ( c > 0 ) {
sum += c;
}
}
}
return sum;
}