首页 > 其他分享 >Leetcode 1203. 项目管理

Leetcode 1203. 项目管理

时间:2024-10-13 12:32:30浏览次数:1  
标签:group 项目管理 List 1203 groupId int result 邻接 Leetcode

1.题目基本信息

1.1.题目描述

有 n 个项目,每个项目或者不属于任何小组,或者属于 m 个小组之一。group[i] 表示第 i 个项目所属的小组,如果第 i 个项目不属于任何小组,则 group[i] 等于 -1。项目和小组都是从零开始编号的。可能存在小组不负责任何项目,即没有任何项目属于这个小组。

请你帮忙按要求安排这些项目的进度,并返回排序后的项目列表:

  • 同一小组的项目,排序后在列表中彼此相邻。
  • 项目之间存在一定的依赖关系,我们用一个列表 beforeItems 来表示,其中 beforeItems[i] 表示在进行第 i 个项目前(位于第 i 个项目左侧)应该完成的所有项目。

如果存在多个解决方案,只需要返回其中任意一个即可。如果没有合适的解决方案,就请返回一个 空列表 。

1.2.题目地址

https://leetcode.cn/problems/sort-items-by-groups-respecting-dependencies/description/

2.解题方法

2.1.解题思路

拓扑排序

2.2.解题步骤

第一步,-1的组的重新定义

第二步,构建组间邻接表和组内邻接表

第三步,组间拓扑排序

第四步,组内拓扑排序

3.解题代码

Python代码

from typing import Dict,List
from collections import deque,defaultdict
# ==> 通过Dict[List]结构的邻接表图和Dict形式的入度信息进行拓扑排序,返回拓扑排序序列,如果不存在,则返回空列表
def topoSort(adjListGraph:Dict[object,List[object]],inDegreeList:Dict[object,int]):
    que=deque()
    for node in adjListGraph.keys():
        inDegree=inDegreeList[node]
        if inDegree==0:
            que.append(node)
    result=[]
    while que:
        node=que.popleft()
        result.append(node)
        for subNode in adjListGraph[node]:
            inDegreeList[subNode]-=1
            if inDegreeList[subNode]==0:
                que.append(subNode)
    return result if len(result)==len(adjListGraph) else []

class Solution:
    def sortItems(self, n: int, m: int, group: List[int], beforeItems: List[List[int]]) -> List[int]:
        # 第一步,-1的组的重新定义
        maxGroupId=max(group)
        distinctGroupsSet=set()
        for i in range(len(group)):
            if group[i]==-1:
                group[i]=maxGroupId+1
                maxGroupId+=1
            distinctGroupsSet.add(group[i])
        distinctGroups=list(distinctGroupsSet)
        # 第二步,构建组间邻接表和组内邻接表
        groupsAdjList=defaultdict(list) # 组间的邻接表
        groupsIndegreeList=defaultdict(int) # 组间邻接表的入度表
        groupItemsAdjList={groupId:defaultdict(list) for groupId in distinctGroups}  # 各个组中各个元素的邻接表
        groupItemsIndegreeList={groupId:defaultdict(int) for groupId in distinctGroups}  # 各个组中各个元素的邻接表的入度信息
        for groupId in distinctGroups:
            groupsAdjList[groupId]=[]
        for i in range(n):
            itemId,groupId,beforeIds=i,group[i],beforeItems[i]
            for beforeId in beforeIds:
                beforeGroupId=group[beforeId]
                if beforeGroupId!=groupId and groupId not in groupsAdjList[beforeGroupId]:
                    groupsAdjList[beforeGroupId].append(groupId)
                    groupsIndegreeList[groupId]+=1
                if beforeGroupId==groupId:
                    groupItemsAdjList[groupId][beforeId].append(itemId)
                    # print(groupItemsIndegreeList[groupId])
                    groupItemsIndegreeList[groupId][itemId]+=1
            if itemId not in groupItemsAdjList[groupId]:
                groupItemsAdjList[groupId][itemId]=[]
        # 第三步,组间拓扑排序
        groupSortArr=topoSort(groupsAdjList,groupsIndegreeList)
        if not groupSortArr:
            return []
        # print(groupSortArr)
        # 第四步,组内拓扑排序
        result=[]
        for groupId in groupSortArr:
            thisItemsArr=topoSort(groupItemsAdjList[groupId],groupItemsIndegreeList[groupId])
            # print(groupId,thisItemsArr,groupItemsAdjList[groupId])
            if not thisItemsArr:
                return []
            result.extend(thisItemsArr)
        # print(result)
        return result

4.执行结果

在这里插入图片描述

标签:group,项目管理,List,1203,groupId,int,result,邻接,Leetcode
From: https://www.cnblogs.com/geek0070/p/18462149

相关文章

  • 基于nodejs+vue基于Springboot的测试项目管理平台[开题+源码+程序+论文]计算机毕业设
    本系统(程序+源码+数据库+调试部署+开发环境)带文档lw万字以上,文末可获取源码系统程序文件列表开题报告内容研究背景随着信息技术的迅猛发展,软件开发已成为现代社会不可或缺的一部分。在软件开发过程中,测试环节扮演着至关重要的角色,它直接关系到软件的质量和用户体验。然而,......
  • 计算机毕业设计源码 基于MVC的教学项目管理平台的设计
    标题: 基于MVC的教学项目管理平台的设计一个基于MVC(Model-View-Controller,模型-视图-控制器)架构的教学项目管理平台,旨在提高教育机构中教学项目从规划到实施再到评估的整个过程的效率与质量。以下是该平台可能包含的主要功能模块:1.用户管理与权限分配:•角色定义:区分教师、......
  • Leetcode--位运算
     小伙伴们,大家好。今天给大家介绍一下位运算。 首先给大家介绍一下常用的位运算符号: &(按位与) |(按位或)^(按位异或)<<(左移)>>(右移) 以a=4,b=6为例: 4的二进制为100,6的二进制为110(假设前名的0省略) a&b=100(每一位进行运算时如果均为1则该位为1否则为0) a|b=110(每......
  • 代码随想录Day24 | LeetCode 122. 买卖股票的最佳时机 II、LeetCode 55. 跳跃游戏、Le
    LeetCode122.买卖股票的最佳时机IIclassSolution:defmaxProfit(self,prices:List[int])->int:res=0foriinrange(1,len(prices)):res+=max(0,prices[i]-prices[i-1])returnresLeetCode55.跳跃游戏class......
  • Leetcode 贪心算法之 Container With Most Water
    题目描述给定一个长度为n的整数数组height。有n条垂线,第i条线的两个端点是(i,0)和(i,height[i])。找出其中的两条线,使得它们与x轴共同构成的容器可以容纳最多的水。返回容器可以储存的最大水量。说明:你不能倾斜容器。示例:输入:[1,8,6,2,5,4,8,3,7]输......
  • Leetcode 贪心算法之Jump Game II
    题目描述给定一个长度为n的0索引整数数组nums。初始位置为nums[0]。每个元素nums[i]表示从索引i向前跳转的最大长度。换句话说,如果你在nums[i]处,你可以跳转到任意nums[i+j]处:0<=j<=nums[i]i+j<n返回到达nums[n-1]的最小跳跃次数。生成的......
  • 【JavaScript】LeetCode:61-65
    文章目录61课程表62实现Trie(前缀树)63全排列64子集65电话号码的字母组合61课程表Map+BFS拓扑排序:将有向无环图转为线性顺序。遍历prerequisites:1.数组记录每个节点的入度,2.哈希表记录依赖关系。n=6,prerequisites=[[3,0],[3,1],[4,1],[4,2],[5,3],[5,4]]。0、1......
  • leetcode 179. Largest Number
    179.LargestNumber要比较拼接以后谁应该放在前面,先试着把他们拼起来就行了然后因为正着拼和反着拼,长度一致,所以自带的string比较函数会严格比较他们的大小关系,不会因为字符串长度而误判boolcmp(constint&a,constint&b);classSolution{public:std::string......
  • 代码随想录训练营第五天|Leetcode.349,Leetcode.454,Leetcode19,Leetcode18
    一、哈希表和set和map和数组的关系 用哈希表,判断是否出现过。数值很大或者数值很分散时,不用数组,占用空间大,用set。set,multiset数组的大小是受限制的,而且如果元素很少,而哈希值太大会造成内存空间的浪费。set是一个集合,里面放的元素只能是一个key,而两数之和这道题目,不仅要判......
  • 软考攻略/超详细/系统集成项目管理工程师/基础知识分享13
    5.3软件设计(掌握)        需求阶段解决“做什么”的问题,而软件设计阶段解决“怎么做”的问题。软件设计分为结构化设计与面向对象设计。5.3.1结构化设计(掌握)        结构化设计(SD)是一种面向数据流的方法,其目的在于确定软件结构。它以SRS和SA阶段所产生的D......