Online store~15 min2s · 256 MBtarget O(total size) time, O(1) spacetraversal
Your online store has a function that finds the lowest price change in a list. A teammate wrote it with a running minimum that starts at 0. You suspect it is wrong, and you have a correct version.
A stress run feeds both versions the same random lists. Each list has at least one value. A value can be negative, because a price can fall.
Write first_disagreement(lists).
Input: lists = [[3, -1, 4], [0, 7], [5, 2, 9], [8]]
Output: 3The correct minimum and the teammate's minimum are compared on each list.
| List | Values | Correct minimum | Teammate's minimum | Same? |
|---|---|---|---|---|
| 1 | 3, -1, 4 | -1 | -1 | yes |
| 2 | 0, 7 | 0 | 0 | yes |
| 3 | 5, 2, 9 | 2 | 0 | no |
| 4 | 8 | 8 | 0 | no |
List 3 is the first list where they differ.
Input: lists = [[-4, 2], [0]]
Output: 0The smallest values are -4 and 0. The teammate's function returns -4 and 0 too, so the answer is 0.
Input: lists = [[6]]
Output: 1The only list has the value 6. The teammate's function returns 0 and the correct one returns 6.
1 ≤ number of lists ≤ 601 ≤ length of each list ≤ 10^5-10^6 ≤ each value ≤ 10^6A list of 100,000 values in falling order costs about 5 x 10^9 reads if you test each value against all the others to find the minimum. Find the minimum with one pass instead.
All values fit in a 32-bit integer. The number of lists is small, so one pass over every list is at most a few million steps.