Skip to main content
All Questions

Lexicographic Grid Travel.

Unlock detailed company stats for this questionUpgrade
You are given an m × n grid with a start cell S and a destination cell D. Each cell contains a value from {0,1,2,3,4}, where 0 is blocked, and 1–4 correspond to bicycle, motorbike, car, and bus, respectively. You are also given matrices T and C representing the time and cost to enter each cell. You must choose exactly one mode of transport for the entire trip (no switching) and can move up, down, left, or right to adjacent cells compatible with your mode. Your goal is to find a path from S to D that minimizes (total_time, total_cost) lexicographically—that is, prefer smaller time, and among paths with equal time, smaller cost. Return the minimal (time, cost) across all modes, or -1 if the destination is unreachable.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.