1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-19 17:56:55 +02:00
LeetCode/problems/solving-questions-with-brainpower.cpp
Matej Focko 6c3cfcd876
chore: reorganize files
Signed-off-by: Matej Focko <mfocko@redhat.com>
2022-07-23 12:53:31 +02:00

51 lines
1.2 KiB
C++

#include <cassert>
#include <functional>
#include <vector>
using std::vector;
class Solution {
struct Question {
long long answered;
long long skipped;
long long get() const { return std::max(answered, skipped); }
};
long long getWithOffset(vector<Question> &questions, int offset) {
auto i = questions.size() - offset - 1;
if (i < 0 || i >= questions.size()) {
return 0;
}
return questions[i].get();
}
public:
long long mostPoints(vector<vector<int>> &questions) {
vector<Question> stack;
std::for_each(questions.rbegin(), questions.rend(), [&](auto &q_in) {
Question q;
q.answered = q_in[0] + getWithOffset(stack, q_in[1]);
q.skipped = getWithOffset(stack, 0);
stack.push_back(q);
});
return stack.back().get();
}
};
int main() {
Solution s;
vector<vector<int>> questions;
questions = {{3, 2}, {4, 3}, {4, 4}, {2, 5}};
assert(s.mostPoints(questions) == 5);
questions = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
assert(s.mostPoints(questions) == 7);
return 0;
}