상위 150개 인터뷰쉬움

K개 정류장 내 최저가 항공편

'K개 정류장 내 가장 저렴한 항공편' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

n개의 도시가 몇 개의 항공편으로 연결되어 있습니다. 항공편[i] = [from_i, to_i, 가격_i]가 가격_i 비용으로 도시 from_i에서 도시 to_i까지 항공편이 있음을 나타내는 배열 항공편이 제공됩니다.

또한 세 개의 정수 src, dst 및 k가 주어지며 최대 k 정거장을 사용하여 src에서 dst까지 가장 저렴한 가격을 반환합니다. 해당 경로가 없으면 -1을 반환합니다.

findCheapestPrice(n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int 함수를 작성하세요.

제약
  • 1 <= n <= 100
  • 0 <= len(flights) <= (n * (n - 1) / 2)
  • flights[i].length == 3
  • 0 <= from_i, to_i < n
  • 1 <= price_i <= 10^4
  • 0 <= src, dst < n
  • src != dst
  • 0 <= k < n

Example 1
Input
n = 4, flights = [[0,1,100],[1,2,100],[2,0,100],[1,3,600],[2,3,200]], src = 0, dst = 3, k = 1
Output
700
Explanation

The cheapest path from city 0 to city 3 with at most 1 stop is 0 -> 1 -> 3 with cost 100 + 600 = 700.

Example 2
Input
n = 3, flights = [[0,1,100],[1,2,100],[0,2,500]], src = 0, dst = 2, k = 1
Output
200
Explanation

Cheapest path is 0 -> 1 -> 2 with cost 200, which has exactly 1 stop.

Need a Hint?
세트나 힙과 같은 고급 그래프 관련 데이터 구조를 사용해 보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.