18. The K Weakest Rows in a Matrix
The problem can be found at the following link: Question Link
My Approach
Create an empty vector of pairs
rowStrengths. Each pair will store the row's strength (the sum of elements in the row) and the row index.Iterate through each row of the matrix
mat: a. Calculate the strength of the current row by summing all its elements usingaccumulate. b. Push a pair containing the row's strength and its index into therowStrengthsvector.Sort the
rowStrengthsvector in ascending order based on the first element of each pair (i.e., row strength).Create an empty vector
resultto store the indices of thekweakest rows.Iterate from
i = 0toi < k: a. Push the index of thei-th row from the sortedrowStrengthsvector into theresultvector.Return the
resultvector, which contains the indices of thekweakest rows.
Here's the pointwise algorithm in a step-by-step format:
Initialize an empty vector
rowStrengths.Iterate through each row of the matrix
mat:Calculate the strength of the current row by summing all its elements.
Create a pair containing the row's strength and its index.
Push the pair into the
rowStrengthsvector.
Sort the
rowStrengthsvector in ascending order based on the first element of each pair (i.e., row strength).Initialize an empty vector
resultto store the indices of thekweakest rows.Iterate from
i = 0toi < k:Push the index of the
i-th row from the sortedrowStrengthsvector into theresultvector.
Return the
resultvector, which contains the indices of thekweakest rows.
Time and Auxiliary Space Complexity
Time Complexity:
O(nlogn)Auxiliary Space Complexity:
O(1)
Code (C++)
class Solution {
public:
vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {
vector<pair<int, int>> rowStrengths;
for (int i = 0; i < mat.size(); ++i) {
int strength = accumulate(mat[i].begin(), mat[i].end(), 0);
rowStrengths.push_back({strength, i});
}
sort(rowStrengths.begin(), rowStrengths.end());
vector<int> result;
for (int i = 0; i < k; ++i) {
result.push_back(rowStrengths[i].second);
}
return result;
}
};
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the rishabhv12/Daily-Leetcode-Solution repository.
Last updated
Was this helpful?