Last active
October 25, 2022 22:52
-
-
Save BroVic/0ae2792cf0885a20d599e67003aa1826 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
After becoming famous, the CodeBots decided to move into a new | |
uilding together. Each of the rooms has a different cost, and | |
ome of them are free, but there's a rumour that all the free | |
ooms are haunted! Since the CodeBots are quite superstitious, | |
hey refuse to stay in any of the free rooms, or any of the | |
ooms below any of the free rooms. | |
Given matrix, a rectangular matrix of integers, where each | |
alue represents the cost of the room, your task is to return | |
he total sum of all rooms that are suitable for the CodeBots | |
ie: add up all the values that don't appear below a 0). | |
*/ | |
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
int solution(vector<vector<int>> matrix) { | |
vector<int> indices; | |
auto total = 0; | |
for (auto i = 0; i < matrix.size(); i++) | |
{ | |
const auto ht = matrix[i].size(); | |
for (auto j = 0; j < ht; j++) | |
{ | |
auto res = find(indices.begin(), indices.end(), j); | |
const auto elem = matrix[i][j]; | |
if (elem == 0 || res != indices.end()) | |
{ | |
indices.push_back(j); | |
continue; | |
} | |
else if (res == indices.end()) | |
{ | |
total += elem; | |
} | |
} | |
} | |
return total; | |
} | |
int main() | |
{ | |
vector<vector<int>> m = {{0, 1, 1, 2}, | |
{0, 5, 0, 0}, | |
{2, 0, 3, 3}}; | |
cout << solution(m) << '\n'; // 9 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment