第一题 两个数对之间的最大乘积差
Python
![image](https://i-blog.csdnimg.cn/blog_migrate/fbc262a2bb3c3a8f3777fe2479ed0ed5.png)
C++
![image](https://i-blog.csdnimg.cn/blog_migrate/2c428d7d2b86b48ea884dfe35a8367f8.png)
Java
![image](https://i-blog.csdnimg.cn/blog_migrate/38734c19c614b0e8107522dd81f47c57.png)
第二题 三角形的最大周长
Python
![image](https://i-blog.csdnimg.cn/blog_migrate/61e94231927d29ac6a03ccfe47facdfe.png)
C++
![image](https://i-blog.csdnimg.cn/blog_migrate/c2a4766f78de7a8477bc8181df3392c3.png)
Java
![image](https://i-blog.csdnimg.cn/blog_migrate/a1f40d07812ef76bcdeabb01714fc6fe.png)
第三题 数组拆分
Python
![image](https://i-blog.csdnimg.cn/blog_migrate/4c54061c4495c4eb1b8d45d8387d05ce.png)
C++
![image](https://i-blog.csdnimg.cn/blog_migrate/524ceee57a3fbf165e515bdc382a3547.png)
Java
![image](https://i-blog.csdnimg.cn/blog_migrate/4821735e38c0a941d381ab4aa417e82e.png)
第四题 救生艇
Python
![image](https://i-blog.csdnimg.cn/blog_migrate/c1c09a378f455e8715d937876b097032.png)
C++
![image](https://user-images.githubusercontent.com/59593996/153739465-3dadd930-be51-4527-b86f-bf2c805a401c.png)
Java
![image](https://i-blog.csdnimg.cn/blog_migrate/59e824015839e91c7400ca1ced640625.png)
第五题
Python
![image](https://i-blog.csdnimg.cn/blog_migrate/9f024fba61e79661bb82691008562e3e.png)
C++
![image](https://i-blog.csdnimg.cn/blog_migrate/2e5767fddd3ced80f499f2a99d3fca83.png)
Java
![image](https://i-blog.csdnimg.cn/blog_migrate/75e3e8d7c76a1c403db73f4f95a15c39.png)
第六题 分发饼干
Python
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
n, m = len(g), len(s)
i = j = count = 0
while i < n and j < m:
while j < m and g[i] > s[j]:
j += 1
if j < m:
count += 1
i += 1
j += 1
return count
C++
class Solution {
public:
int findContentChildren(vector<int>& g, vector<int>& s) {
sort(g.begin(), g.end());
sort(s.begin(), s.end());
int numOfChildren = g.size(), numOfCookies = s.size();
int count = 0;
for (int i = 0, j = 0; i < numOfChildren && j < numOfCookies; i++, j++) {
while (j < numOfCookies && g[i] > s[j]) {
j++;
}
if (j < numOfCookies) {
count++;
}
}
return count;
}
};
Java
class Solution {
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int numOfChildren = g.length, numOfCookies = s.length;
int count = 0;
for (int i = 0, j = 0; i < numOfChildren && j < numOfCookies; i++, j++) {
while (j < numOfCookies && g[i] > s[j]) {
j++;
}
if (j < numOfCookies) {
count++;
}
}
return count;
}
}
https://leetcode-cn.com/problems/assign-cookies/
第七题 最少操作数使数组递增
Python
class Solution:
def minOperations(self, nums):
length = len(nums)
if length <= 1:
return 0
ret = 0
left = nums[0]
for right in nums[1:]:
tmp = max(left + 1, right)
ret += tmp - right
left = tmp
return ret
C++
class Solution
{
public:
int minOperations(vector<int>& nums)
{
int n = nums.size();
if (n == 0)
return 0;
int res = 0;
for (int i = 1; i < n; i++)
{
if (nums[i-1] < nums[i])
continue;
else
{
int cur = nums[i-1] + 1 - nums[i];
res += cur;
nums[i] = nums[i-1] + 1;
}
}
return res;
}
};
Java
class Solution {
public int minOperations(int[] nums) {
int res=0;
for(int i=1;i<nums.length;i++){
if(nums[i]>nums[i-1]){
continue;
}else{
res+=nums[i-1]+1-nums[i];
nums[i]=nums[i-1]+1;
}
}
return res;
}
}
https://leetcode-cn.com/problems/minimum-operations-to-make-the-array-increasing/
第八题 使数组唯一的最小增量
https://leetcode-cn.com/problems/minimum-increment-to-make-array-unique/submissions/
Python
class Solution:
def minIncrementForUnique(self, A: List[int]) -> int:
# 贪心算法
A.sort()
count = 0
for i in range(1, len(A)):
if A[i] <= A[i - 1]:
count += A[i - 1] - A[i] + 1
A[i] = A[i - 1] + 1
return count
C++
class Solution {
public:
int minIncrementForUnique(vector<int>& nums) {
sort(nums.begin(), nums.end());
int ans = 0, taken = 0;
for (int i = 1; i < (int)nums.size(); ++i) {
if (nums[i - 1] == nums[i]) {
taken++;
ans -= nums[i];
} else {
int give = min(taken, nums[i] - nums[i - 1] - 1);
ans += give * (give + 1) / 2 + give * nums[i - 1];
taken -= give;
}
}
if (nums.size() > 0) {
ans += taken * (taken + 1) / 2 + taken * nums.back();
}
return ans;
}
};
Java
class Solution {
public:
int minIncrementForUnique(vector<int>& nums) {
sort(nums.begin(), nums.end());
int ans = 0, taken = 0;
for (int i = 1; i < (int)nums.size(); ++i) {
if (nums[i - 1] == nums[i]) {
taken++;
ans -= nums[i];
} else {
int give = min(taken, nums[i] - nums[i - 1] - 1);
ans += give * (give + 1) / 2 + give * nums[i - 1];
taken -= give;
}
}
if (nums.size() > 0) {
ans += taken * (taken + 1) / 2 + taken * nums.back();
}
return ans;
}
};
第九题 有效三角形个数
https://leetcode-cn.com/problems/valid-triangle-number/solution/you-xiao-san-jiao-xing-de-ge-shu-by-leet-t2td/
Python
class Solution:
def triangleNumber(self, nums: List[int]) -> int:
n = len(nums)
nums.sort()
ans = 0
for i in range(n):
k = i
for j in range(i + 1, n):
while k + 1 < n and nums[k + 1] < nums[i] + nums[j]:
k += 1
ans += max(k - j, 0)
return ans
C++
class Solution {
public:
int triangleNumber(vector<int>& nums) {
int n = nums.size();
sort(nums.begin(), nums.end());
int ans = 0;
for (int i = 0; i < n; ++i) {
int k = i;
for (int j = i + 1; j < n; ++j) {
while (k + 1 < n && nums[k + 1] < nums[i] + nums[j]) {
++k;
}
ans += max(k - j, 0);
}
}
return ans;
}
};
Java
class Solution {
public int triangleNumber(int[] nums) {
int n = nums.length;
Arrays.sort(nums);
int ans = 0;
for (int i = 0; i < n; ++i) {
int k = i;
for (int j = i + 1; j < n; ++j) {
while (k + 1 < n && nums[k + 1] < nums[i] + nums[j]) {
++k;
}
ans += Math.max(k - j, 0);
}
}
return ans;
}
}