Leecode Hot100 刷题笔记本-BFS/DFS系列(C++版)

91 阅读2分钟
  1. 509. 斐波那契数
  2. 207. 课程表
  3. 1857. 有向图中最大颜色值

509. 斐波那契数

Screen Shot 2023-09-12 at 4.37.49 PM.png

解法1: 先序BFS
class Solution {
    int fib(int n) {

        if (n == 0) return 0;
        if (n == 1) return 1;

        queue<int> qu;
        qu.push(0);
        qu.push(1);

        vector<int> res(n+1, 0);
        res[1] = 1;

        vector<int> indeg(n+1, 2);
        indeg[0] = 0;
        indeg[1] = 0;

        while(!qu.empty()) {
            int x = qu.front();
            qu.pop();
            if (x == n) {
            return res[x];
        }

        vector<int> next = x == 0 ? vector<int>{2} : vector<int>{x + 1, x + 2};
        for (int y: next) {
            if (y > n) break;
            res[y] += res[x];
            indeg[y]--;
                if (indeg[y] == 0) {
                    qu.push(y);
                }
         }
    }
    return res[n];
    }
};
解法2: 先序DFS+递归
class Solution {
public:
    int fib(int n) {
        if(n == 0) return 0;
        if(n == 1) return 1;
        vector<int> dp(n + 1, 0);
        dp[1] = 1;
        vector<int> indeg(n + 1, 2);
        indeg[0] = 0;
        indeg[1] = 0;
        vector<int> begin = {0, 1};
        for(int x : begin) dfs(x, n, dp, indeg);
        return dp[n];
    }
    void dfs(int x, int n, vector<int>& dp, vector<int>& indeg) {
        if(x == n) return;
        vector<int> next = x == 0 ? vector<int>{2} : vector<int>{x + 1, x + 2};
        for(int y : next) {
            if(y > n) continue;
            dp[y] += dp[x];
            indeg[y]--;
            if(indeg[y] == 0) dfs(y, n, dp, indeg);
        }
    }
};
解法3: 先序DFS+栈
class Solution {
public:
    int fib(int n) {
        if(n == 0) return 0;
        if(n == 1) return 1;
        vector<int> dp(n + 1, 0);
        dp[1] = 1;
        vector<int> indeg(n + 1, 2);
        indeg[0] = 0;
        indeg[1] = 0;
        vector<int> begin = {0, 1};
        for(int x : begin) dfs(x, n, dp, indeg);
        return dp[n];
    }
    void dfs(int x, int n, vector<int>& dp, vector<int>& indeg) {
        stack<pair<int,int>> stack;
        stack.push(make_pair(x,0));
        while(!stack.empty()){
            pair<int,int>* cur = &stack.top();
            int x1 = (*cur).first;
            int vis = (*cur).second;
            bool case1 = x1 == 0 && vis == 1;
            bool case2 = x1 != 0 && vis == 2;
            if(!case1 && !case2){
                int next = x1 == 0 ? 2 : x1 + vis + 1;
                (*cur).second++; 
                if(next > n) continue; // 这里千万不要写成break,因为栈内还有其他的状态需要继续传递
                indeg[next]--;
                dp[next] += dp[x1];
                if(indeg[next] == 0) stack.push(make_pair(next,0));
            }
            else stack.pop();
        }
    }
};

207. 课程表

Screen Shot 2023-09-12 at 4.43.48 PM.png

解法1: 先序BFS
class Solution {
public:

bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {

    unordered_map<int, vector<int>> courMap;
    vector<int> ingreed(numCourses, 0);
    for (int index = 0; index < prerequisites.size(); index++) {
        ingreed[prerequisites[index][0]]++;
        courMap[prerequisites[index][1]].emplace_back(prerequisites[index][0]);
    }

    queue<int> qu;
    for (int i = 0; i < ingreed.size(); i++) {
        if (ingreed[i] == 0) {
            qu.push(i);
        }
    }

    int cnt = 0;
    while (!qu.empty()) {
        int top = qu.front();
        qu.pop();
        cnt++;
        for (auto adj: courMap[top]) {
            ingreed[adj]--;
            if (ingreed[adj] == 0) {
                qu.push(adj);
            }
        }
    }

    return (cnt == numCourses);
    }
};

1857. 有向图中最大颜色值

Screen Shot 2023-09-12 at 4.50.49 PM.png

class Solution {

public:

int largestPathValue(string colors, vector<vector<int>>& edges) {
    int n = colors.size(); // 获取节点数量
    vector<int> indeg(n, 0);
    vector<vector<int>> graph(n);
    
    // 构建有向图并计算入度

    for (const auto& edge : edges) {

        int u = edge[0];
        int v = edge[1];
        graph[u].push_back(v);
        indeg[v]++;
    }

    vector<vector<int>> dp(n, vector<int>(26, 0));
    queue<int> q;

    // 将入度为0的节点加入队列
    for (int i = 0; i < n; i++) {
        if (indeg[i] == 0) {
            q.push(i);
        }
    }

    int longestPath = 0;
    
    while (!q.empty()) {

        int u = q.front();
        q.pop();
        dp[u][colors[u] - 'a']++;
        for (int v : graph[u]) {
            indeg[v]--;
            for (int i = 0; i < 26; i++) {
                dp[v][i] = max(dp[v][i], dp[u][i]);
            }
            if (indeg[v] == 0) {
                q.push(v);
            }
        }

        int maxCount = *max_element(dp[u].begin(), dp[u].end());
        longestPath = max(longestPath, maxCount);
    }

  


    if (count(indeg.begin(), indeg.end(), 0) != n) {
        return -1; // 存在环
    }
    return longestPath;
    }
};