How do you write a quicker code?



  • Description of the task

    The Sporting School of Simultaneous Navigation " Goldfish " came to Children ' s " Jobs " camp for summer fees. First of all We're gonna need a roommate. Chief Coach proposed the following rule for settlement: The team is all and no one else. There's no team here. Athlete. Number of rooms assigned to the camp limited. Determine if the vocals can settle the children?

    Introduction form

    The first line has a number of n-- the number of athletes. Second a[i] - number of sportsmen i - team. Next, number k is the number of types of room, and k of type of room capacity, number of such rooms. All Input data are positive whole not exceeding 10000.

    Form of withdrawal

    Get Yes, if the vocals can settle the athletes, and No, If it doesn't work.

    Introduction

    3 
    1 2 3
    1 
    2 3
    

    Conclusion

    No
    

    My code is:

    n = int(input())
    com_list = list(map(int, input().split()))
    k = int(input())
    k_list = []
    

    for _ in range(k):
    res = list(map(int, input().split()))
    k_list.extend([res[0]]*res[1])

    if len(k_list) < n:
    print('No')
    else:
    s = 0
    for i in range(n):
    if sorted(com_list, reverse=True)[i] <= sorted(k_list, reverse=True)[i]:
    s+=1
    else:
    break
    print("No" if s<n else 'Yes')

    Continuing problem code speed and memory consumption



  • Note: I guess I actually have your decision, just written shorter and clearer. And how did you know? EzikBro, you've had a sorting at every step of the cycle, which slows the test.

    Let's say the data's already in place and posted on the relevant lists (which I have described as more understandable). Then it is practically one line, and it is clear that the challenge is:

    teams = [1, 2, 3, 3]
    rooms = [2, 3, 2, 1, 4]
    

    print('Yes'
    if len(rooms)>=len(teams)
    and all(r >= t for r,t in zip(sorted(rooms, reverse=True), sorted(teams, reverse=True)))
    else 'No')

    In order to be possible, two conditions must be met which we must check:

    • rooms should be as good as teams.
    • If the size of the rooms and crews is to be matched, the size of each room shall be at least equal to that of the team.

    This should work very quickly, however, as the verification will end without waiting for the end of the bulkhead if one of the two values fails the condition. So, complexity = difficulty of sorting two rolls + not more O(n) For testing.

    Added a steam-free version (size of room, number) to save memory, but it's not in the same line and without the library:

    from itertools import zip_longest

    teems = [1, 2, 3, 3]
    rooms = [(3, 4)]

    rooms_gen = (size for room,count in rooms for size in (room for _ in range(count)))
    print('Yes'
    if all(r >= t for r,t in zip_longest(sorted(rooms_gen, reverse=True),sorted(teems, reverse=True), fillvalue=0))
    else 'No')




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2