> For the complete documentation index, see [llms.txt](https://code-render.gitbook.io/leetcode-editorials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://code-render.gitbook.io/leetcode-editorials/09-september/18-the-k-weakest-rows-in-a-matrix.md).

# 18. The K Weakest Rows in a Matrix

The problem can be found at the following link: [Question Link](https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/description/)

## My Approach

1. 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.
2. Iterate through each row of the matrix `mat`: a. Calculate the strength of the current row by summing all its elements using `accumulate`. b. Push a pair containing the row's strength and its index into the `rowStrengths` vector.
3. Sort the `rowStrengths` vector in ascending order based on the first element of each pair (i.e., row strength).
4. Create an empty vector `result` to store the indices of the `k` weakest rows.
5. Iterate from `i = 0` to `i < k`: a. Push the index of the `i`-th row from the sorted `rowStrengths` vector into the `result` vector.
6. Return the `result` vector, which contains the indices of the `k` weakest 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 `rowStrengths` vector.
* Sort the `rowStrengths` vector in ascending order based on the first element of each pair (i.e., row strength).
* Initialize an empty vector `result` to store the indices of the `k` weakest rows.
* Iterate from `i = 0` to `i < k`:
  * Push the index of the `i`-th row from the sorted `rowStrengths` vector into the `result` vector.
* Return the `result` vector, which contains the indices of the `k` weakest rows.

## Time and Auxiliary Space Complexity

* **Time Complexity**: `O(nlogn)`
* **Auxiliary Space Complexity**: `O(1)`

## Code (C++)

```cpp

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](https://leetcode.com/discuss/general-discussion). 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](https://github.com/rishabhv12/Daily-Leetcode-Solution) repository.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://code-render.gitbook.io/leetcode-editorials/09-september/18-the-k-weakest-rows-in-a-matrix.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
