Similar Problems
Similar Problems not available
Merge Triplets To Form Target Triplet - Leetcode Solution
Companies:
LeetCode: Merge Triplets To Form Target Triplet Leetcode Solution
Difficulty: Medium
Problem Statement:
You are given an array of the form target = [x, y, z] and an array of triplets triplets, where each triplet = [ai, bi, ci] describes the i-th triplet. You can select triplets and concatenate their elements (in the same order) if the elements in the concatenated result match the target.
Return true if it is possible to concatenate triplets from the array triplets to form the target, or false otherwise.
Solution:
The solution to the Merge Triplets To Form Target Triplet problem is a straightforward implementation problem. We need to check whether it is possible to concatenate the triplets from the array triplets to form the target array.
The first step in solving this problem is to sort the triplets array. Then, we can iterate over the sorted triplets array and check whether we can merge the current triplet with the previous triplet(s) to form the target array.
We can define a helper function merge_triplets
that takes two triplets and returns the merged triplet if it is possible to merge the two triplets, otherwise it returns None. We can use this helper function to merge the sorted triplets array and form the target array.
The merge_triplets
function checks whether it is possible to merge two triplets by comparing their first, second, and third elements with the corresponding elements in the target array. If all three elements match, it returns the merged triplet, otherwise it returns None.
Algorithm:
- Initialize an empty list
triplets_matched
to store the triplets that match the target. - Sort the triplets array.
- Iterate over the sorted triplets array:
a. If the current triplet matches the target, append it to the
triplets_matched
list. b. Otherwise, try to merge the current triplet with the previous triplet(s) that match the target by calling themerge_triplets
function. c. If the merged triplet matches the target, append it to thetriplets_matched
list. d. Otherwise, continue the iteration. - If the length of the
triplets_matched
list is equal to 3, return True, otherwise return False.
Code:
from typing import List
def merge_triplets(t1: List[int], t2: List[int]) -> List[int]:
"""Merge two triplets if possible, otherwise return None."""
x1, y1, z1 = t1
x2, y2, z2 = t2
if x2 <= x1 and y2 <= y1 and z2 <= z1:
return [x1, y1, z1]
elif x1 <= x2 and y1 <= y2 and z1 <= z2:
return [x2, y2, z2]
return None
def merge_triplets_to_form_target_triplet(triplets: List[List[int]], target: List[int]) -> bool:
"""Merge triplets to form target triplet."""
triplets_matched = []
triplets.sort()
for triplet in triplets:
if triplet == target:
triplets_matched.append(triplet)
elif len(triplets_matched) > 0:
prev_matched = triplets_matched[-1]
merged = merge_triplets(prev_matched, triplet)
if merged == target:
triplets_matched.append(merged)
return len(triplets_matched) == 3
Time Complexity:
The time complexity of the merge_triplets_to_form_target_triplet
function is O(n^2), where n is the length of the triplets array. The function calls the merge_triplets
function for each pair of triplets, resulting in an O(n^2) time complexity.
Space Complexity:
The space complexity of the merge_triplets_to_form_target_triplet
function is O(1) because it only uses a constant amount of additional space. The merge_triplets
function also uses a constant amount of additional space. Therefore, the overall space complexity of the solution is O(1).
Merge Triplets To Form Target Triplet Solution Code
1