- Today
- Total
작심삼일
[LeetCode] 1094 | Car Pooling | Python 본문
문제 링크: https://leetcode.com/problems/car-pooling/
문제
There is a car with capacity empty seats. The vehicle only drives east (i.e., it cannot turn around and drive west).
You are given the integer capacity and an array trips where trip[i] = [$numPassengers^i, from^i, to^i$] indicates that the $i^{th}$ trip has $numPassengers^i$ passengers and the locations to pick them up and drop them off are $from^i$ and $to^i$ respectively. The locations are given as the number of kilometers due east from the car's initial location.
Return true if it is possible to pick up and drop off all passengers for all the given trips, or false otherwise.
조건
- 1 <= trips.length <= 1000
- trips[i].length == 3
- 1 <= $numPassengers^i$ <= 100
- 0 <= $from^i$ < $to^i$ <= 1000
- 1 <= capacity <= $10^5$
내 풀이
조건을 보면 from, to의 범위가 0 이상 1000 이하이므로 그에 해당하는 배열(car)을 만든다.
trips에서 [num, f, t]가 주어질 때, car[f]부터 car[t-1]까지 num을 더한다.
car[t-1]을 뺀 이유는 거기서 이 사람들이 내리기 때문이다.
이때 car[i]의 값이 capacity보다 크다면 False를 출력하면 된다.
코드
class Solution:
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
car = [0 for _ in range(1001)]
for num, f, t in trips:
for i in range(f, t):
car[i] += num
if car[i] > capacity:
return False
return True
'스터디 > 코테' 카테고리의 다른 글
[LeetCode] 701 | Insert into a Binary Search Tree | Python (0) | 2022.01.12 |
---|---|
[LeetCode] 67 | Add Binary | Python (0) | 2022.01.10 |
[LeetCode] 131 | Palindrome Partitioning | Python (0) | 2022.01.05 |
[LeetCode] 1009 | Complement of Base 10 Integer | Python (0) | 2022.01.04 |
[LeetCode] 997 | Find the Town Judge | Python (0) | 2022.01.03 |