iT邦幫忙

2025 iThome 鐵人賽

1

https://ithelp.ithome.com.tw/upload/images/20260510/20177944lacCI9oV9R.jpg

https://ithelp.ithome.com.tw/upload/images/20260510/20177944WKG7MVhIob.jpg

https://ithelp.ithome.com.tw/upload/images/20260510/20177944dUVcKSqW0R.jpg

https://ithelp.ithome.com.tw/upload/images/20260510/20177944u8HibYflev.jpg

https://ithelp.ithome.com.tw/upload/images/20260510/20177944Tri2h9pWSn.jpg

https://ithelp.ithome.com.tw/upload/images/20260510/20177944CBGRWoI9l3.jpg

https://ithelp.ithome.com.tw/upload/images/20260510/20177944Umd5b29dDj.jpg

https://ithelp.ithome.com.tw/upload/images/20260510/20177944KHVT9otpJO.jpg

https://ithelp.ithome.com.tw/upload/images/20260510/20177944FRqEWTPkct.jpg

class Solution { //both m + n 
public:
    vector<int> findOrder(int n, vector<vector<int>>& p) {
        vector<vector<int>> nextCourses(n);
        vector<int> indegree(n), ready, ans;
        for (auto& pre : p){
            nextCourses[pre[1]].push_back(pre[0]);
            ++indegree[pre[0]];
        }
        for (int i = 0; i < n; ++i)
            if(indegree[i] == 0)
                ready.push_back(i);
        for (int i = 0; i < (int)ready.size(); ++i){
            int course = ready[i];
            ans.push_back(course);
            for (int nextOne : nextCourses[course])
                if (--indegree[nextOne] == 0)
                    ready.push_back(nextOne);
        }
        return (int)ans.size() == n ? ans : vector<int>{};
    }
};

上一篇
補更0407的133 圖片又歪了><
系列文
轉職仔之Data Science and ai master後的持續精進技術之路42
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言