第一题代码:
class Solution {
public:
int maximumProduct(vector<int>& nums) {
sort(nums.begin(), nums.end());
int n = nums.size();
return max(nums[0] * nums[1] * nums[n - 1], nums[n - 3] * nums[n - 2] * nums[n - 1]);
}
};
结果:
第二题代码:
class Solution {
public:
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
int sum = 0;
vector<int> p;
for (int i = 0; i < nums.size(); i ++ )
{
for (int j = 0; j < nums.size(); j ++ )
if (nums[i] > nums[j])
sum ++;
p.push_back(sum);
sum = 0;
}
return p;
}
};
结果:
第三题代码:
class Solution {
public:
int minIncrementForUnique(vector<int>& nums) {
int len = nums.size();
int i;
int sum = 0;
sort(nums.begin(), nums.end());
for (i = 1; i < len; ++i) {
if (nums[i] > nums[i - 1]) continue;
else {
sum += nums[i - 1] + 1 - nums[i];
nums[i] = nums[i - 1] + 1;
}
}
return sum;
}
};
结果:
第四题代码:
#include <iostream>
#include <vector>
using namespace std;
int inp;
int n, k;
vector<int> tmp;
int main()
{
cin >> inp;
while (inp--) {
mp.clear();
cin >> n >> k;
int i, t;
int ans = 0;
for (i = 0; i < n; ++i) {
cin >> t;
tmp.push_back(t);
}
if (k == 1) {
ans = (n - 1) / 2;
} else {
for (i = 1; i < n - 1; ++i) {
if (tmp[i] > tmp [i - 1] + tmp[i + 1]) ++ans;
}
}
cout << ans << endl;
}
return 0;
}
ji