LeetCode- 166 Race Course Week
1281.subtract-the-product-and-sum-of-digits-of-an-integer
1282.group-the-people-given-the-group-size-they-belong-to
1283.find-the-smallest-divisor-given-a-threshold
1284.minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix
1281. integers difference between you and the product
1281.subtract-the-product-and-sum-of-digits-of-an-integer
Title Description Description
To give you an integer n, please help calculate and return the integer difference "product you figures" and "figures and" the.
Sample input and output sample Sample Input and Sample Output
Example 1:
输入:n = 234
输出:15
解释:
各位数之积 = 2 * 3 * 4 = 24
各位数之和 = 2 + 3 + 4 = 9
结果 = 24 - 9 = 15
Example 2:
输入:n = 4421
输出:21
解释:
各位数之积 = 4 * 4 * 2 * 1 = 32
各位数之和 = 4 + 4 + 2 + 1 = 11
结果 = 32 - 11 = 21
Tip Hint
1 <= n <= 10^5
Code
class Solution {
public:
int p(int n) {
int ans(1);
while(n)
ans *= n % 10, n /= 10;
return ans;
}
int s(int n) {
int ans(0);
while(n)
ans += n % 10, n /= 10;
return ans;
}
int subtractProductAndSum(int n) {
return p(n) - s(n);
}
};
1282. User groups
1282.group-the-people-given-the-group-size-they-belong-to
Title Description Description
There are n users to participate in activities, their ID from 0 to n - 1, each user happens to belong to a particular group of users. Give you an array of length n groupSizes, which contains the size of each user's user group, you return to user grouping (user group as well as the presence of users in each group ID).
You can order the return of any solution, the order ID is not limited. Further, the subject data analysis to ensure the presence of at least one solution.
Sample input and output sample Sample Input and Sample Output
Example 1:
输入:groupSizes = [3,3,3,3,3,1,3]
输出:[[5],[0,1,2],[3,4,6]]
解释:
其他可能的解决方案有 [[2,1,6],[5],[0,4,3]] 和 [[5],[0,6,2],[4,3,1]]。
Example 2:
输入:groupSizes = [2,1,3,3,3,2]
输出:[[1],[0,5],[2,3,4]]
Tip Hint
groupSizes.length == n
1 <= n <= 500
1 <= groupSizes[i] <= n
Code
using std::vector;
class Solution {
public:
vector<vector<int>> groupThePeople(vector<int>& groupSizes) {
int n = groupSizes.size();
vector<vector<int>> ans;
vector<int> g[n + 1];
for(int i = 0; i < n; ++i) {
g[groupSizes[i]].push_back(i);
if(g[groupSizes[i]].size() == groupSizes[i]) {
ans.push_back(g[groupSizes[i]]);
g[groupSizes[i]].clear();
}
}
return ans;
}
};
1283. Results not exceed the threshold so that the minimum divisor
1283.find-the-smallest-divisor-given-a-threshold
Title Description Description
Nums give you an array of integers and a positive integer threshold, you need to choose a positive integer divisor, then the array each number is divided by it, and the result of the division sums.
You can find out so that the result is less than the threshold value threshold equal to the divisor of the smallest one.
Each number divided by the divisor are rounded up, say 3 = 7/3, 10/2 = 5.
Topic guaranteed solvable.
Sample input and output sample Sample Input and Sample Output
Example 1:
输入:nums = [1,2,5,9], threshold = 6
输出:5
解释:如果除数为 1 ,我们可以得到和为 17 (1+2+5+9)。
如果除数为 4 ,我们可以得到和为 7 (1+1+2+3) 。如果除数为 5 ,和为 5 (1+1+1+2)。
Example 2:
输入:nums = [2,3,5,7,11], threshold = 11
输出:3
Example 3:
输入:nums = [19], threshold = 5
输出:4
Tip Hint
1 <= nums.length <= 5 * 10^4
1 <= nums[i] <= 10^6
nums.length <= threshold <= 10^6
Code
class Solution {
public:
int cal(vector<int>&nums, int x) {
int ans(0);
for(auto i : nums) {
ans += (i+x-1) / x;
}
return ans;
}
int smallestDivisor(vector<int>& nums, int threshold) {
int first = 1, middle, half, len = *max_element(nums.begin(), nums.end());
while(len > 0) {
half = len >> 1;
middle = first + half;
if(cal(nums, middle) > threshold) {
first = middle + 1;
len = len - half - 1;
} else
len = half;
}
return first;
}
};
1284. into a matrix of zeros with a minimum number of inversions
1284.minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix
Title Description Description
Give you a mxn binary matrix mat.
Each step, you can select a cell and its inversion (inversion represents 0 to 1, 1 to 0). If it and the adjacent cell exists, these adjacent cells will be reversed. (Note: two adjacent cells sharing the same side.)
Please return to the matrix mat into a matrix of zeros with the least number of reverse, if not into all-zero matrix, please return -1.
Each grid binary matrix is either 0 or 1.
All-zero matrix is a matrix of all the grid are zero.
Sample input and output sample Sample Input and Sample Output
Example 1:
输入:mat = [[0,0],[0,1]]
输出:3
解释:一个可能的解是反转 (1, 0),然后 (0, 1) ,最后是 (1, 1) 。
Example 2:
输入:mat = [[0]]
输出:0
解释:给出的矩阵是全零矩阵,所以你不需要改变它。
Example 3:
输入:mat = [[1,1,1],[1,0,1],[0,0,0]]
输出:6
Example 4:
输入:mat = [[1,0,0],[1,0,0]]
输出:-1
解释:该矩阵无法转变成全零矩阵
Tip Hint
m == mat.length
n == mat[0].length
1 <= m <= 3
1 <= n <= 3
mat[i][j] 是 0 或 1 。
Code
#include <bits/stdc++.h>
using std::vector;
using std::min;
class Solution {
public:
int digitCount(int x) {
int ans(0);
while(x) {
if(x & 1)
ans ++;
x >>= 1;
}
return ans;
}
inline void flip(int &x) {
if(x)
x = 0;
else
x = 1;
}
int chk(vector<vector<int>>mat, int x) {
int n = mat.size();
int m = mat[0].size();
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j) {
if(x & (1 << i * m + j)) {
flip(mat[i][j]);
if(i)
flip(mat[i - 1][j]);
if(i < n - 1)
flip(mat[i + 1][j]);
if(j)
flip(mat[i][j - 1]);
if(j < m - 1)
flip(mat[i][j + 1]);
}
}
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
if(mat[i][j])
return false;
return true;
}
int minFlips(vector<vector<int>>& mat) {
int n = mat.size(), m = mat[0].size();
const int inf = 0x3f3f3f3f;
int ans(inf);
for(int i = 0; i < (1 << n * m); ++i)
if(chk(mat, i))
ans = min(ans, digitCount(i));
return ans == inf ? -1 : ans;
}
};