Google-Developer-Student-Club-CCOEW / Competitive-Programming-2023-GDSC-CUMMINS-X-GDSC-MMCOE

Welcome to the Hacktoberfest 2023 Competitive-Programming Cohort for Cummins College and MMCOE students! To request issue assignment, create a pull request, providing: 1. Full Name 🧑‍🎓 2.Email 📧 3.College ID (RNO) 🔢 4.Branch of Study.📚 5. Year 📆 .The Cummins College and MMCOE students' PRs will be considered only. Thank you!
9 stars 69 forks source link

Sort Items by Groups Respecting Dependencies #95

Closed saakshii12 closed 11 months ago

saakshii12 commented 1 year ago

Write code in the preferred language and attach output with it.

There are n items each belonging to zero or one of m groups where group[i] is the group that the i-th item belongs to and it's equal to -1 if the i-th item belongs to no group. The items and the groups are zero indexed. A group can have no item belonging to it.

Return a sorted list of the items such that:

The items that belong to the same group are next to each other in the sorted list. There are some relations between these items where beforeItems[i] is a list containing all the items that should come before the i-th item in the sorted array (to the left of the i-th item). Return any solution if there is more than one solution and return an empty list if there is no solution.

Example 1: image Input: n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3,6],[],[],[]] Output: [6,3,4,1,5,2,0,7]

Example 2: Input: n = 8, m = 2, group = [-1,-1,1,0,0,1,0,-1], beforeItems = [[],[6],[5],[6],[3],[],[4],[]] Output: [] Explanation: This is the same as example 1 except that 4 needs to be before 6 in the sorted list.

RishabhDhawad commented 11 months ago

I would like to work on this issue.