Ah-Choo!
InterviewTime limit1sMemory limit512 MB
Compute the least Dynamic Time Warping distance between two equal-length integer sequences, where every point must match at least one point of the other and matches cannot cross.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Array, Implementation
- Solved
- No attempts yet
Problem
Every person has a distinct sneeze. Loud sneezes and short cute sneezes mix with different voices, so each person produces a distinct waveform.
Dongi studies data science in graduate school and wants to test whether sneezes identify people. With graduation near, Dongi starts the experiment at once for the thesis. Jiae and Jisu, whose sneezes sound alike, join as subjects.
To decide whether two sneezes come from the same person, Dongi needs a similarity score. The score uses DTW (Dynamic Time Warping).
A sound waveform is time series data with time on the axis. Even one person sneezing again produces a slightly different waveform each time. A direct comparison at equal times reports low similarity for similar shapes. DTW first matches each time point of one waveform to the closest point of the other waveform and then measures the error. This matching absorbs shifts from sneezing speed and recording start.
Write the heights of sounds and at times and as and . Matching point of with point of costs . Every point of matches at least one point of , and every point of matches at least one point of . The sum over matched pairs is the distance, and the inverse of the least DTW distance is the similarity.
Matches keep order. If of matches of and a later of matches of , then holds. Crossed matches are invalid. Matching second 7 of to second 6 of and then second 8 of to second 5 of is an example of a forbidden match.
Take and . Matching equal times gives . A better order-preserving match gives . Matches with a crossing do not count.
Given two recorded sneezes as sequences in time order, write a program that computes the least DTW distance.
Input
The first line gives a natural number , the length of each waveform. The second line gives integers in time order, the heights of the first waveform , separated by spaces. The third line gives the heights of the second waveform in the same format.
Every height is an integer with .
Output
Print the least distance between the two waveforms as an integer.