【LeetCode-递归/动态规划】

递归.cpp

某分布式任务调度系统有 taskNum 个任务(编号从 1 到 taskNum)需要调度,调度策略:

任务之间可能存在依赖关系,且无循环依赖,如任务1 依赖任务2,那么要等待任务2执行完才能执行任务1;
如果任务之间没有依赖关系,则可以并发执行(假设并发所需资源是充足的)。
现给出任务间的依赖关系,并假设每个任务的执行时间恒为 1 分钟,请计算执行完这 taskNum 个任务所需的最短时间(单位分钟)。

解答要求
时间限制:2000ms, 内存限制:256MB
输入
第一行为任务的数量 taskNum ,其值范围为:[1, 1000]
第二行为依赖关系的数量 relationsNum ,其值范围:[0, 500000]
接下来 relationsNum 行,每行描述一个依赖关系,格式为 IDi>IDj,表示任务 i 依赖任务 j ,IDi 和 IDj 值的范围为:[1, taskNum]

输出
一个整数,代表执行完所有任务的最短时间。

9
6
1>2
2>3
2>4
4>5
6>4
8>7
输出样例 2

4

https://oj.rnd.huawei.com/problems/1020/details

// 实际求最大深度,自己也能分析出来,代码只能用暴力方法(递归的方法不知道怎么写)
https://oj.rnd.huawei.com/discuss/problems/discussions/c0d9ecd5-e36a-4096-962e-6f3e76ecb697?navName=C%2B%2B%20Recursively%20find%20the%20longest%20path
https://oj.rnd.huawei.com/discuss/problems/discussions/0c35a64e-0562-4d7d-ab48-b527d6de8b40?navName=c%2B%2B%20dfs%20%E5%A4%87%E5%BF%98%E5%BD%95
https://oj.rnd.huawei.com/discuss/problems/discussions/8297df39-ed1b-4269-8312-224841c4d6b8?navName=c%2B%2B%E9%80%92%E5%BD%92

// 部分通过 5/30,很像自己考试的情况,自己的暴力思路
class Solution {
public:
    // 待实现函数,在此函数中填入答题代码;
    int GetMinTime(int taskNum, const vector<pair<int, int>> &relations)
    {
        // 节点值,最大深度
        map<int, int> mp;
        queue<int> q; // 

        map<int, vector<int>> relationsMap;
        // 数组优化成map没有任何效果!!!!!
        for (auto relation : relations) {
            relationsMap[relation.first].push_back(relation.second); // 3 4被存储在2中
        }

        for (auto relation : relations) {
            int f = relation.first;
            int s = relation.second;

            if (mp.count(f)) {
                continue;
            }

            q.push(s);
            int level = 2; // 两个元素,最少需要两分钟
            
            // 尝试第一个元节点1
            while (!q.empty()) {
                int value = q.front();
                q.pop();

                bool flag = false;
                auto iter = relationsMap.find(value); // 找2 循环找,改成map找
                if (iter != relationsMap.end()) {
                    // 有子节点3 4
                    vector<int> subVec = iter->second;
                    for (int i = 0; i < subVec.size(); i++) {
                        q.push(subVec[i]);
                    }
                    flag = true;
                }
                if (flag) {
                    level++;
                }
            }
            
            mp[f] = level; // 循环完,获取了最大的层数
        // for (auto relation : relations) {
        //     int f = relation.first;
        //     int s = relation.second;

        //     if (mp.count(f)) {
        //         continue;
        //     }

        //     q.push(s);
        //     int level = 2; // 两个元素,最少需要两分钟
            
        //     // 尝试第一个元节点1
        //     while (!q.empty()) {
        //         int value = q.front();
        //         q.pop();

        //         int count = 0;
        //         for (int i = 0; i< relations.size(); i++) { // 每次都从头开始循环
        //             if (value == relations[i].first) {
        //                 q.push(relations[i].second); // 新增一层
        //                 count++;
        //             }
        //         }
        //         if (count > 0) {
        //             level++;
        //         }
        //     }
            
        //     mp[f] = level; // 循环完,获取了最大的层数
        }

        vector<int> vec;
        for (auto ele : mp) {
            vec.push_back(ele.second);
        }

        sort(vec.begin(), vec.end(), greater<int>());
        return vec[0];
    }
};

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值