561. Array Partition I
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.
Example 1:
Input: [1,4,3,2]
Output: 4
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
Note:
- n is a positive integer, which is in the range of [1, 10000].
- All the integers in the array will be in the range of [-10000, 10000].
Solutions:
class Solution {
public:
int arrayPairSum(vector<int>& nums) {
int sum = 0;
sort(nums.begin(), nums.end());
for(int i = 0; i < nums.size(); i+=2)
sum += nums[i];
return sum;
}
};
Performance:
1351. Count Negative Numbers in a Sorted Matrix
Given a m * n
matrix grid
which is sorted in non-increasing order both row-wise and column-wise.
Return the number of negative numbers in grid
.
Example 1:
Input: grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]]
Output: 8
Explanation: There are 8 negatives number in the matrix.
Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 100
-100 <= grid[i][j] <= 100
Solutions:
class Solution {
public:
int countNegatives(vector<vector<int>>& grid) {
int ans(0);
for(int i=0; i<grid.size();i++)
for(int j=0;j<grid[i].size();j++)
{
if(grid[i][j] < 0)
{
ans += grid[i].size()-j; break;
}
}
return ans;
}
};
Performance:
online collection of Great solutions: from [C++] Three Methods
class Solution {
public:
int countNegatives(vector<vector<int>>& grid) {
int ans(0);
// Brute Force: O(mn) - check if negative
for (const vector<int>& row: grid)
for (const int& i: row)
if (i < 0) ans++;
return ans;
}
};
class Solution {
public:
int countNegatives(vector<vector<int>>& grid) {
int ans(0);
// Binary Search: O(m lg(n)) or equivalently O(n lg(m)) - look for break point of each row / column
for (const vector<int>& row: grid)
ans += upper_bound(row.rbegin(), row.rend(), -1) - row.rbegin();
return ans;
}
};
class Solution {
public:
int countNegatives(vector<vector<int>>& grid) {
int ans(0);
// Search Break Points: O(m + n) - traverse from upper right to lower left
int m(grid.size()), n(grid[0].size()), r(0), c(n - 1);
while (r < m) {
while (c >= 0 && grid[r][c] < 0) c--;
ans += n - c - 1;
r++;
}
return ans;
}
};
Useful Knowledge:
1010. Pairs of Songs With Total Durations Divisible by 60
In a list of songs, the i
-th song has a duration of time[i]
seconds.
Return the number of pairs of songs for which their total duration in seconds is divisible by 60
. Formally, we want the number of indices i < j
with (time[i] + time[j]) % 60 == 0
.
Example 1:
Input: [30,20,150,100,40]
Output: 3
Explanation: Three pairs have a total duration divisible by 60:
(time[0] = 30, time[2] = 150): total duration 180
(time[1] = 20, time[3] = 100): total duration 120
(time[1] = 20, time[4] = 40): total duration 60
Note:
1 <= time.length <= 60000
1 <= time[i] <= 500
class Solution {
public:
int numPairsDivisibleBy60(vector<int>& time) {
vector<int> m(60,0);
int r = 0; int count = 0;
for(const auto i:time)
{
r = i % 60;
count += m[(60-r) % 60];
m[r]++;
}
return count;
}
};
1018. Binary Prefix Divisible By 5
Given an array A
of 0
s and 1
s, consider N_i
: the i-th subarray from A[0]
to A[i]
interpreted as a binary number (from most-significant-bit to least-significant-bit.)
Return a list of booleans answer
, where answer[i]
is true
if and only if N_i
is divisible by 5.
Example 1:
Input: [0,1,1]
Output: [true,false,false]
Explanation:
The input numbers in binary are 0, 01, 011; which are 0, 1, and 3 in base-10. Only the first number is divisible by 5, so answer[0] is true.
Note:
1 <= A.length <= 30000
A[i]
is0
or1
Solutions:
class Solution {
public:
vector<bool> prefixesDivBy5(vector<int>& A) {
vector<bool> res(A.size(), false);
int index = 0;
int num = 0;
for(const auto i:A)
{
num = (num << 1) + i;
if(num % 5 == 0)
res[index] = true;
index++;
num = num % 5; //to avoid integer overflow
}
return res;
}
};
Performance:
Mind-stretching solution:
class Solution {
public:
vector<bool> prefixesDivBy5(vector<int>& A) {
vector <vector <int> > v; //five different reminder condition
v.push_back(vector<int>{0,1});
v.push_back(vector<int>{2,3});
v.push_back(vector<int>{4,0});
v.push_back(vector<int>{1,2});
v.push_back(vector<int>{3,4});
int act_mod = 0;
vector <bool> res;
for(auto w : A)
{
act_mod = v[act_mod][w];
res.push_back(not act_mod);
}
return res;
}
};