leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit ade3ae909d5530e632695b3ce840cf1c7a022cc1
parent 152e80a8f793e797fe3edda9f4f7881d868c455b
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Thu, 13 Jul 2023 10:11:17 +0200

Improved Daily Problem

Diffstat:
MProblems/0207.cpp | 19++++++++++---------
1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/Problems/0207.cpp b/Problems/0207.cpp @@ -1,12 +1,12 @@ class Solution { + static int adj[2048][2048]; + public: - bool canFinish(int n, vector<vector<int>> &prerequisites) { - vector<vector<int>> adj(n); - vector<int> count(n, 0); - int num = 0; + bool canFinish(int n, const vector<vector<int>> &prerequisites) { + int count[2048] = {0}, size[2048] = {0}; for (auto &p : prerequisites) { - adj[p[0]].push_back(p[1]); + adj[p[0]][size[p[0]]++] = p[1]; count[p[1]]++; } @@ -16,12 +16,13 @@ public: while (!q.empty()) { int root = q.front(); - q.pop(); - n--; - for (int c : adj[root]) - if (!--count[c]) q.push(c); + q.pop(), n--; + for (int i = 0; i < size[root]; i++) + if (!--count[adj[root][i]]) q.push(adj[root][i]); } return n == 0; } }; + +int Solution::adj[2048][2048];