Rank: 350/1675。
- [Solved] stands for that the problems are solved in the competition.
- [Unsolved] stands for that I failed to solve them in the competition.
[Solved] 1281. Subtract the Product and Sum of Digits of an Integer
Description:
Given an integer number n, return the difference between the product of its digits and the sum of its digits.
Example 1:
1 | Input: n = 234 |
Example 2:
1 | Input: n = 4421 |
Constraints:
1 <= n <= 10^5
Idea:
The straight idea is that compute the product and sum of the integer respectively and then
get the answer.
1 | # Time complexity : O(N), N is the number of the digits |
[Solved] 1282. Group the People Given the Group Size They Belong To
Description:
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:
1 | Input: groupSizes = [3,3,3,3,3,1,3] |
Example 2:
1 | Input: groupSizes = [2,1,3,3,3,2] |
Constraints:
groupSizes.length == n
1 <= n <= 500
1 <= groupSizes[i] <= n
Idea:
Here is a simple rule which can be easily found, when we try to
figure out the calculation process of the example 1. We allocate the groupSize space to store the IDs, if there is no corresponding group, and put the ID in it when iterate the array. If any group is filled up, we append it into the result and then allocate a new group. Thus, it is the greedy algorithm.
1 | # Time Complexity : O(N) |
[Solved] 1283. Find the Smallest Divisor Given a Threshold
Description:
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:
1 | Input: nums = [1,2,5,9], threshold = 6 |
Example 2:
1 | Input: nums = [2,3,5,7,11], threshold = 11 |
Example 3:
1 | Input: nums = [19], threshold = 5 |
Constraints:
1 <= nums.length <= 5 * 10^4
1 <= nums[i] <= 10^6
nums.length <= threshold <= 10^6
Idea:
At the beginning, I try to iterate the divisor from 1 to the max(nums). The time complexity of this algorithm is O(N**2) and the program exceeded the time limitation.
Then, I try to use the binary search algorithm and actually it is a easy way to pass all the test cases.
1 | # Time Complexity : O(nlogn) |
[Unsolved] 1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix
Description:
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:
1 | Input: mat = [[0,0],[0,1]] |
Example 2:
1 | Input: mat = [[0]] |
Example 3:
1 | Input: mat = [[1,1,1],[1,0,1],[0,0,0]] |
Example 4:
1 | Input: mat = [[1,0,0],[1,0,0]] |
Constraints:
m == mat.length
n == mat[0].length
1 <= m <= 3
1 <= n <= 3
mat[i][j] is 0 or 1
Idea:
We store the states which are visited in a hashmap, and use the breadth first searching algorithm to enumerate all the flipping states. If the states are not visited before, we append them into the queue.
——“bfs 枚举,并记录矩阵状态“
1 | # Time complexity : O(2**(nm)) |