Time and space complexity of count team question

邮差的信 提交于 2021-02-11 12:44:55

问题


from typing import List
def countTeams(num: int, skills: List[int], minAssociates: int, minLevel: int,
               maxLevel: int) -> int:
    # WRITE YOUR BRILLIANT CODE HERE
    qualified = []
    possible_teams = [[]]
    for a in skills:
        if minLevel <= a <= maxLevel:
            qualified.append(a)
    num_teams = 0
    while qualified:
        person = qualified.pop()
        new_teams = []
        for team in possible_teams:
            print(team)
            print(person)
            new_team = [person] + team
            print(new_team)
            if len(new_team) >= minAssociates:
                num_teams += 1
            new_teams.append(new_team)
        possible_teams += new_teams
        print(possible_teams)

    return num_teams


if __name__ == "__main__":
    num = 6
    skills = [12, 4, 6, 13, 5, 10]
    minAssociates = 3
    minLevel = 4
    maxLevel = 10
    result = countTeams(num, skills, minAssociates, minLevel, maxLevel)
    print(result)

A Company is trying to assemble a team from available associates. There is a minimum number of associates to be involved, and each associate needs to have a skill rating within a certain range. Given a list of associates' skill levels with desired upper and lower bounds, determine how many teams can be created from the list.

Write an algorithm to find the number of teams that can be created by fulfilling the criteria.

Input The input consists of five arguments:
num: an integer representing the number of associates
skills : a list of integers representing the skill levels of associates
minAssociates: an integer representing the minimum number of team members required
minLevel: an integer representing the lower limit for skill level, inclusive
maxLevel: an integer representing the upper limit for skill level, inclusive.

Example 1: Input:
num = 6
skills = [12, 4, 6, 13, 5, 10]
minAssociates = 3
minLevel = 4
maxLevel = 10

Output: 5
Could anyone explain to me the time and space complexity of the above code.


回答1:


Time complexity is O(n^2) - you are using two loops one inside the other to be precise O( qualified * possible_teams) is your time complexity

For space complexity its O(n) - you are using new_teams inside the while loop to be precise O(qualified ) is your space complexity



来源:https://stackoverflow.com/questions/66072750/time-and-space-complexity-of-count-team-question

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!