KatInterview

Longest Eastward Chain of Cities from Raw CSV Text

Dynamic ProgrammingAuto-graded · 22 test cases
MediumAuto-gradedDynamic Programmingcsv-parsinglongest-increasing-subsequencedag-dp

Last updated

Parse CSV text with quoted fields, then find the longest chain of cities with increasing longitude and non-decreasing latitude, breaking ties by the smallest list of names.

Longest Eastward Chain of Cities

A travel app exports its stop list as one CSV string, csv_text. Parse it, then find the longest chain of cities that only moves east and never moves south.

Input

csv_text consists of lines separated by \n. The first line is always the header city_name,latitude,longitude. Every other line is one city record with exactly three fields: the name, the latitude, and the longitude. The latitude and longitude are decimal numbers.

Fields follow standard CSV quoting rules:

  • A field may be wrapped in double quotes.
  • Inside a quoted field, a comma is part of the text, not a separator.
  • Inside a quoted field, two consecutive double quotes ("") stand for one literal " character.
  • An unquoted field never contains a comma or a double quote.

The surrounding quotes are not part of the city name. For example, the field "City ""A""" is the name City "A".

Empty lines carry no record and must be ignored. If there are no records after the header, the answer is an empty list.

Chain rules

A chain is an ordered list of cities c_1, c_2, ..., c_k. It may use the records in any order, not only the order in which they appear in the input. Each step from c_i to c_{i+1} must satisfy both of these:

  1. The longitude of c_{i+1} is strictly greater than the longitude of c_i.
  2. The latitude of c_{i+1} is greater than or equal to the latitude of c_i.

A single city is a valid chain of length 1.

Goal

Return the names of a chain with the maximum possible number of cities. If several chains have that maximum length, return the one whose list of names is lexicographically smallest. Compare the lists element by element, and compare individual names by ordinary string order (by character code). For example, ["A", "C"] is smaller than ["B", "A"].

Return the chosen chain as a list of city names, in travel order.

Constraints

  • 0 <= number of city records N <= 1,000 (N is 0 only when just the header is present)
  • -90.0 <= latitude <= 90.0
  • -180.0 <= longitude <= 180.0
  • City names are printable ASCII and at most 100 characters long.
  • Different cities may share the same name, latitude, or longitude.
  • A solution running in O(N^2) time is expected.

Examples

Example 1

Input: longest_eastward_chain('city_name,latitude,longitude\n"Denver, CO",39.7392,-104.9903\n"Omaha, NE",41.2565,-95.9345\n"Chicago, IL",41.8781,-87.6298\n"St. Louis, MO",38.6270,-90.1994')

Output: ['Denver, CO', 'Omaha, NE', 'Chicago, IL']

Example 2

Input: longest_eastward_chain('city_name,latitude,longitude\n"City ""A""",10.0,20.0\nCity B,15.0,20.0')

Output: ['City "A"']

Hints

Hint 1

Treat each city as a node, with an edge from city a to city b when b is strictly east of a and not south of a. Because longitude strictly increases along every edge, there are no cycles, so this is a longest-path problem on a DAG.

Hint 2

For parsing, do not split on commas. Use a CSV reader, or a small two-state scanner (inside quotes / outside quotes) that turns a doubled quote into one literal quote.

Hint 3

Compute, for each city, the longest chain starting there, by processing cities from the largest longitude down. For tie-breaking, do not store whole paths. Within each chain-length group, give every city a rank based on the pair (its name, rank of its best next city), so that comparing two chains takes constant work.