Ctrl K

Triple From Maximums

Medium
You are given a list of integer triples triples and a target triple goal. In one operation you pick two different triples at indices i and j and replace triple j with the element-wise maximum of the two. For example, [2, 5, 3] and [1, 7, 5] turn the second one into [2, 7, 5]. You may perform any number of operations, including none. Return True if goal can end up as one of the triples in the list, and False otherwise.
Example 1
triples=[[4, 1, 2], [1, 6, 3], [2, 3, 9], [3, 5, 1]], goal=[4, 6, 3] → True

Merging [4, 1, 2] with [1, 6, 3] gives [4, 6, 3]. The triple [2, 3, 9] must stay out because 9 exceeds 3.

Example 2
triples=[[2, 2, 7], [5, 1, 1]], goal=[5, 2, 5] → False

No usable triple has 5 in the last position: [2, 2, 7] has 7, which is too big, and [5, 1, 1] has only 1. It cannot be built.

Example 3
triples=[[8, 8, 8]], goal=[8, 8, 8] → True
Constraints
  • 1 ≤ len(triples) ≤ 10⁵
  • Every triple, and goal, has exactly three numbers
  • All numbers are between 1 and 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo Boards Holding Water MediumFewest Jumps to the End MediumReach the Last Cell MediumAs Many Trades as You Like MediumWhere to Start the Lap Medium
loading Python…
Input
triples=[[4, 1, 2], [1, 6, 3], [2, 3, 9], [3, 5, 1]], goal=[4, 6, 3]
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 122 tests on the server, hidden ones included.