You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
5279. Subtract the Product and Sum of Digits of an Integer
Easy
Given an integer number n, return the difference between the product of its digits and the sum of its digits.
Example 1:
Input: n = 234
Output: 15
Explanation:
Product of digits = 2 * 3 * 4 = 24
Sum of digits = 2 + 3 + 4 = 9
Result = 24 - 9 = 15
Example 2:
Input: n = 4421
Output: 21
Explanation:
Product of digits = 4 * 4 * 2 * 1 = 32
Sum of digits = 4 + 4 + 2 + 1 = 11
Result = 32 - 11 = 21
Constraints:
1 <= n <= 10^5
classSolution {
publicintsubtractProductAndSum(intn) {
intsum = 0, mum = 1;
while(n != 0){
sum += n % 10;
mum *= n % 10;
n /= 10;
}
returnmum - sum;
}
}
5280. Group the People Given the Group Size They Belong To
Medium
There are n people whose IDs go from 0 to n - 1 and each person belongs exactly to one group. Given the array groupSizes of length n telling the group size each person belongs to, return the groups there are and the people's IDs each group includes.
You can return any solution in any order and the same applies for IDs. Also, it is guaranteed that there exists at least one solution.
Example 1:
Input: groupSizes = [3,3,3,3,3,1,3]
Output: [[5],[0,1,2],[3,4,6]]
Explanation:
Other possible solutions are [[2,1,6],[5],[0,4,3]] and [[5],[0,6,2],[4,3,1]].
Given an array of integers nums and an integer threshold, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal to threshold.
Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5).
It is guaranteed that there will be an answer.
Example 1:
Input: nums = [1,2,5,9], threshold = 6
Output: 5
Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1.
If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2).
classSolution{
publicintsmallestDivisor(int[] nums, intthreshold){
intlo = 1, hi = 1000000;
while (lo < hi){
intmid = (lo + hi) / 2;
intresult = cal(nums, mid);
if(result > threshold)
lo = mid + 1;
elsehi = mid;
}
returnlo;
}
privateintcal(int[] nums, intdiv){
intsum = 0;
for(intn : nums){
sum += Math.ceil((double)n / (double)div);
//if(n % div != 0) sum += 1;
}
return (int)sum;
}
}
5282. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix
Hard
Given a m x n binary matrix mat. In one step, you can choose one cell and flip it and all the four neighbours of it if they exist (Flip is changing 1 to 0 and 0 to 1). A pair of cells are called neighboors if they share one edge.
Return the minimum number of steps required to convert mat to a zero matrix or -1 if you cannot.
Binary matrix is a matrix with all cells equal to 0 or 1 only.
Zero matrix is a matrix with all cells equal to 0.
Example 1:
Input: mat = [[0,0],[0,1]]
Output: 3
Explanation: One possible solution is to flip (1, 0) then (0, 1) and finally (1, 1) as shown.
Example 2:
Input: mat = [[0]]
Output: 0
Explanation: Given matrix is a zero matrix. We don't need to change it.
Example 3:
Input: mat = [[1,1,1],[1,0,1],[0,0,0]]
Output: 6
Example 4:
Input: mat = [[1,0,0],[1,0,0]]
Output: -1
Explanation: Given matrix can't be a zero matrix
Constraints:
m == mat.length
n == mat[0].length
1 <= m <= 3
1 <= n <= 3
mat[i][j] is 0 or 1.
The text was updated successfully, but these errors were encountered:
5279. Subtract the Product and Sum of Digits of an Integer
Easy
Given an integer number
n
, return the difference between the product of its digits and the sum of its digits.Example 1:
Example 2:
Constraints:
1 <= n <= 10^5
5280. Group the People Given the Group Size They Belong To
Medium
There are
n
people whose IDs go from0
ton - 1
and each person belongs exactly to one group. Given the arraygroupSizes
of lengthn
telling the group size each person belongs to, return the groups there are and the people's IDs each group includes.You can return any solution in any order and the same applies for IDs. Also, it is guaranteed that there exists at least one solution.
Example 1:
Example 2:
Constraints:
groupSizes.length == n
1 <= n <= 500
1 <= groupSizes[i] <= n
5281. Find the Smallest Divisor Given a Threshold
Medium
Given an array of integers
nums
and an integerthreshold
, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. Find the smallest divisor such that the result mentioned above is less than or equal tothreshold
.Each result of division is rounded to the nearest integer greater than or equal to that element. (For example: 7/3 = 3 and 10/2 = 5).
It is guaranteed that there will be an answer.
Example 1:
Example 2:
Example 3:
Constraints:
1 <= nums.length <= 5 * 10^4
1 <= nums[i] <= 10^6
nums.length <= threshold <= 10^6
5282. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix
Hard
Given a
m x n
binary matrixmat
. In one step, you can choose one cell and flip it and all the four neighbours of it if they exist (Flip is changing 1 to 0 and 0 to 1). A pair of cells are called neighboors if they share one edge.Return the minimum number of steps required to convert
mat
to a zero matrix or -1 if you cannot.Binary matrix is a matrix with all cells equal to 0 or 1 only.
Zero matrix is a matrix with all cells equal to 0.
Example 1:
Example 2:
Example 3:
Example 4:
Constraints:
m == mat.length
n == mat[0].length
1 <= m <= 3
1 <= n <= 3
mat[i][j]
is 0 or 1.The text was updated successfully, but these errors were encountered: