지난 공부기록/알고리즘 풀이

Python - Algorithm #41

hkl22 2024. 8. 28. 10:15

Python Algorithm

LeetCode

1450. Number of Students Doing Homework at a Given Time

  • Given two integer arrays [startTime] and [endTime] and given an integer queryTime.
  • The iᵗʰ student started doing their homework at the time startTime[i] and finished it at time endTime[i].
  • Return the number of students doing their homework at time queryTime. More formally, return the number of students where queryTime lays in the interval [startTime[i], endTime[i]] inclusive.
  • Constraints
    • startTime.length == endTime.length
    • 1 startTime.length 100
    • 1 startTime[i] endTime[i] 1000
    • 1 queryTime 1000
class Solution:
    def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
        # for, zip
        answer = 0
        for start, end in zip(startTime, endTime):
            # queryTime이 저 중간에 위치하면 1만큼 answer 늘려주기
            if start <= queryTime <= end:
                answer += 1
        return answer
class Solution:
    def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
        answer = 0
        for s, e in zip(startTime, endTime):
            if s <= queryTime and e >= queryTime:
                answer += 1
        return answer

2073. Time Needed to Buy Tickets

  • There are n people in a line queuing to buy tickets, where the 0ᵗʰ person is at the front of the line and the (n - 1)ᵗʰ person is at the back of the line.
  • You are given a 0-indexed integer array tickets of length n where the number of tickets that the iᵗʰ person would like to buy is tickets[i].
  • Each person takes exactly 1 second to buy a ticket. A person can only buy 1 ticket at a time and has to go back to the end of the line (which happens instantaneously) in order to buy more tickets. If a person does not have any tickets left to buy, the person will leave the line.
  • Return the time taken for the person at position k (0-indexed) to finish buying tickets.
  • Constraints
    • n == tickets.length
    • 1 n 100
    • 1 tickets[i] 100
    • 0 k < n
class Solution:
    def timeRequiredToBuy(self, tickets: List[int], k: int) -> int:
        # k번째 사람이 사고 싶어 하는 티켓 수와 
        # 특정 위치의 사람이 티켓 사고 싶어 하는 수 비교
        # 해당 위치가 k보다 앞인지 뒤인지
        # 앞에 있는데 사고 싶은 티켓 수가 작은 경우
        # 앞에 있는데 사고 싶은 티켓이 많은 경우
        # 뒤에 있는데 사고 싶은 티켓이 많은 경우
        # 뒤에 있는데 사고 싶은 티켓이 작은 경우
        answer = 0
        for i in range(len(tickets)):
            if i <= k:
                if tickets[i] < tickets[k]:
                    answer += tickets[i]
                else:
                    # 내가 사고 싶은 양을 살 때까지 내 앞에 있음
                    answer += tickets[k]
            else:
                if tickets[i] < tickets[k]:
                    # 자기 사고 싶은 만큼만 사고 떠남
                    answer += tickets[i]
                else:
                    # 내가 사고 싶은 양 살 때까지 줄에 머무는데, 나보다 뒤에 있어서
                    # 내가 다 사게 되는 시점에는 이 사람을 안 기다려도 됨
                    answer += (tickets[k] - 1)
        return answer
class Solution:
    def timeRequiredToBuy(self, tickets: List[int], k: int) -> int:
        time = 0
        for i in range(len(tickets)):
            if tickets[i] >= tickets[k]:
                if i <= k:
                    time += tickets[k]
                else:
                    time += tickets[k] - 1
            else:
                time += tickets[i]
        return time
class Solution:
    def timeRequiredToBuy(self, tickets: List[int], k: int) -> int:
        n = len(tickets)
        i = 0
        time = 0
        while tickets[k] != 0:
            i %= n
            if tickets[i] != 0:
                tickets[i] -= 1
                time += 1
            i += 1
        return time

https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/

https://leetcode.com/problems/time-needed-to-buy-tickets/

'지난 공부기록 > 알고리즘 풀이' 카테고리의 다른 글

Python - Algorithm #43  (0) 2024.08.31
Python - Algorithm #42  (0) 2024.08.29
Python - Algorithm #40  (0) 2024.08.27
Python - Algorithm #39  (0) 2024.08.26
Python - Algorithm #38  (0) 2024.08.23