merge_triplets_to_form_target_triplet
1# @leet start 2class Solution: 3 def mergeTriplets(self, triplets: list[list[int]], target: list[int]) -> bool: 4 """ 5 To solve this problem, we can merge two triplets by only selecting the 6 max of both items in each triplet. 7 So, we need to meet two criteria: 8 1. All the items in target have to be in the right positions. 9 2. Any triplet that has an item that is larger than that target cannot 10 be used for the target triplet. 11 12 We know 1 must be true (otherwise the problem isn't solvable) and we also 13 know 2 must be true because say we have a triplet where any one of the items 14 is greater than its value in target: 15 target = [2, 4, 6] 16 curr = [2, 4, 8] 17 other = [3, 4, 6] 18 Even if we have a triplet, other, which has a 6 in the right position, 19 the max of 6 and 8 is 8, thus it cannot be used to match the triplet. 20 21 So, we iterate through the list of triplets, excluding all triplets that 22 have any value greater than the target. 23 For all other triplets, we check if any of their values match the target. 24 If so, they can be used in the final triplet. 25 Finally, we check to make sure we have at least one of each item in the right 26 place for the triplet, and if so, the triplet is solvable. 27 """ 28 good = set() 29 tx, ty, tz = target 30 31 for triplet in triplets: 32 x, y, z = triplet 33 if x > tx or y > ty or z > tz: 34 continue 35 for i, val in enumerate(triplet): 36 if val == target[i]: 37 good.add(i) 38 39 return len(good) == 3 40 41 42# @leet end 43 44 45def test(): 46 assert 2 + 2 == 4
3class Solution: 4 def mergeTriplets(self, triplets: list[list[int]], target: list[int]) -> bool: 5 """ 6 To solve this problem, we can merge two triplets by only selecting the 7 max of both items in each triplet. 8 So, we need to meet two criteria: 9 1. All the items in target have to be in the right positions. 10 2. Any triplet that has an item that is larger than that target cannot 11 be used for the target triplet. 12 13 We know 1 must be true (otherwise the problem isn't solvable) and we also 14 know 2 must be true because say we have a triplet where any one of the items 15 is greater than its value in target: 16 target = [2, 4, 6] 17 curr = [2, 4, 8] 18 other = [3, 4, 6] 19 Even if we have a triplet, other, which has a 6 in the right position, 20 the max of 6 and 8 is 8, thus it cannot be used to match the triplet. 21 22 So, we iterate through the list of triplets, excluding all triplets that 23 have any value greater than the target. 24 For all other triplets, we check if any of their values match the target. 25 If so, they can be used in the final triplet. 26 Finally, we check to make sure we have at least one of each item in the right 27 place for the triplet, and if so, the triplet is solvable. 28 """ 29 good = set() 30 tx, ty, tz = target 31 32 for triplet in triplets: 33 x, y, z = triplet 34 if x > tx or y > ty or z > tz: 35 continue 36 for i, val in enumerate(triplet): 37 if val == target[i]: 38 good.add(i) 39 40 return len(good) == 3
4 def mergeTriplets(self, triplets: list[list[int]], target: list[int]) -> bool: 5 """ 6 To solve this problem, we can merge two triplets by only selecting the 7 max of both items in each triplet. 8 So, we need to meet two criteria: 9 1. All the items in target have to be in the right positions. 10 2. Any triplet that has an item that is larger than that target cannot 11 be used for the target triplet. 12 13 We know 1 must be true (otherwise the problem isn't solvable) and we also 14 know 2 must be true because say we have a triplet where any one of the items 15 is greater than its value in target: 16 target = [2, 4, 6] 17 curr = [2, 4, 8] 18 other = [3, 4, 6] 19 Even if we have a triplet, other, which has a 6 in the right position, 20 the max of 6 and 8 is 8, thus it cannot be used to match the triplet. 21 22 So, we iterate through the list of triplets, excluding all triplets that 23 have any value greater than the target. 24 For all other triplets, we check if any of their values match the target. 25 If so, they can be used in the final triplet. 26 Finally, we check to make sure we have at least one of each item in the right 27 place for the triplet, and if so, the triplet is solvable. 28 """ 29 good = set() 30 tx, ty, tz = target 31 32 for triplet in triplets: 33 x, y, z = triplet 34 if x > tx or y > ty or z > tz: 35 continue 36 for i, val in enumerate(triplet): 37 if val == target[i]: 38 good.add(i) 39 40 return len(good) == 3
To solve this problem, we can merge two triplets by only selecting the max of both items in each triplet. So, we need to meet two criteria:
- All the items in target have to be in the right positions.
- Any triplet that has an item that is larger than that target cannot be used for the target triplet.
We know 1 must be true (otherwise the problem isn't solvable) and we also know 2 must be true because say we have a triplet where any one of the items is greater than its value in target: target = [2, 4, 6] curr = [2, 4, 8] other = [3, 4, 6] Even if we have a triplet, other, which has a 6 in the right position, the max of 6 and 8 is 8, thus it cannot be used to match the triplet.
So, we iterate through the list of triplets, excluding all triplets that have any value greater than the target. For all other triplets, we check if any of their values match the target. If so, they can be used in the final triplet. Finally, we check to make sure we have at least one of each item in the right place for the triplet, and if so, the triplet is solvable.