Skip to content

Commit b00313b

Browse files
feat: add Merge Triplets to Form Target Triplet problem
- Greedy algorithm implementation - Multiple approaches: Early termination, Iterative, While loop - O(n) time, O(1) space complexity - Comprehensive explanations and edge cases
1 parent 0c3e53f commit b00313b

File tree

2 files changed

+246
-0
lines changed

2 files changed

+246
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Merge Triplets to Form Target Triplet
2+
3+
## Problem Statement
4+
5+
A triplet is an array of three integers. You are given a 2D integer array `triplets`, where `triplets[i] = [ai, bi, ci]` describes the `ith` triplet. You are also given an integer array `target = [x, y, z]` that describes the triplet you want to obtain.
6+
7+
To obtain the `target`, you may apply the following operation any number of times (possibly zero):
8+
9+
- Choose two indices (0-indexed) `i` and `j` (`i != j`) and set `triplets[i][k] = max(triplets[i][k], triplets[j][k])` for all `k` in `[0, 1, 2]`.
10+
11+
Return `true` if it is possible to obtain the `target` triplet, or `false` otherwise.
12+
13+
## Examples
14+
15+
**Example 1:**
16+
```
17+
Input: triplets = [[2,5,3],[1,8,4],[1,7,5]], target = [2,7,5]
18+
Output: true
19+
Explanation: Perform the following operations:
20+
- Choose the first and third triplets [[2,5,3],[1,8,4],[1,7,5]]. Update the third triplet to be [[2,5,3],[1,8,4],[2,7,5]]
21+
- Choose the third and second triplets [[2,5,3],[1,8,4],[2,7,5]]. Update the second triplet to be [[2,5,3],[2,8,5],[2,7,5]]
22+
- Choose the second and first triplets [[2,5,3],[2,8,5],[2,7,5]]. Update the first triplet to be [[2,7,5],[2,8,5],[2,7,5]]
23+
The target triplet [2,7,5] is now [[2,7,5],[2,8,5],[2,7,5]].
24+
```
25+
26+
## Approach
27+
28+
### Method 1: Greedy Algorithm (Recommended)
29+
1. Check if any triplet has values greater than target
30+
2. Check if we can form target by combining triplets
31+
3. Use greedy approach to find valid triplets
32+
4. Most efficient approach
33+
34+
**Time Complexity:** O(n) - Single pass
35+
**Space Complexity:** O(1) - Three variables
36+
37+
### Method 2: Brute Force
38+
1. Try all possible combinations of triplets
39+
2. Check if any combination can form target
40+
3. Less efficient than greedy approach
41+
42+
**Time Complexity:** O(2^n) - Exponential
43+
**Space Complexity:** O(n) - Recursion stack
44+
45+
## Algorithm
46+
47+
```
48+
1. Initialize found = [false, false, false]
49+
2. For each triplet in triplets:
50+
a. If any value > target: skip this triplet
51+
b. For each position i:
52+
c. If triplet[i] == target[i]: found[i] = true
53+
3. Return found[0] && found[1] && found[2]
54+
```
55+
56+
## Key Insights
57+
58+
- **Greedy Choice**: Only use triplets that don't exceed target values
59+
- **Local Optimum**: Check if each position can be achieved
60+
- **Global Optimum**: Can form the target triplet
61+
- **Space Optimization**: Use only three variables
62+
63+
## Alternative Approaches
64+
65+
1. **Brute Force**: Try all possible combinations
66+
2. **Dynamic Programming**: Use DP to track achievable values
67+
3. **Backtracking**: Use backtracking to explore all possibilities
68+
69+
## Edge Cases
70+
71+
- Empty triplets: Return false
72+
- Single triplet: Check if it matches target
73+
- All triplets exceed target: Return false
74+
- Target is [0,0,0]: Handle appropriately
75+
76+
## Applications
77+
78+
- Greedy algorithms
79+
- Array manipulation
80+
- Algorithm design patterns
81+
- Interview preparation
82+
- System design
83+
84+
## Optimization Opportunities
85+
86+
- **Greedy Algorithm**: Most efficient approach
87+
- **Space Optimization**: O(1) space complexity
88+
- **Single Pass**: O(n) time complexity
89+
- **No Extra Space**: Use only three variables
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/**
2+
* Time Complexity: O(n) - Single pass
3+
* Space Complexity: O(1) - Three variables
4+
*/
5+
class Solution {
6+
public boolean mergeTriplets(int[][] triplets, int[] target) {
7+
boolean[] found = new boolean[3];
8+
9+
for (int[] triplet : triplets) {
10+
// Skip triplets that have values greater than target
11+
if (triplet[0] > target[0] || triplet[1] > target[1] || triplet[2] > target[2]) {
12+
continue;
13+
}
14+
15+
// Check if this triplet can contribute to target
16+
for (int i = 0; i < 3; i++) {
17+
if (triplet[i] == target[i]) {
18+
found[i] = true;
19+
}
20+
}
21+
}
22+
23+
return found[0] && found[1] && found[2];
24+
}
25+
}
26+
27+
// Alternative approach using separate variables
28+
class SolutionSeparateVariables {
29+
public boolean mergeTriplets(int[][] triplets, int[] target) {
30+
boolean found0 = false;
31+
boolean found1 = false;
32+
boolean found2 = false;
33+
34+
for (int[] triplet : triplets) {
35+
if (triplet[0] > target[0] || triplet[1] > target[1] || triplet[2] > target[2]) {
36+
continue;
37+
}
38+
39+
if (triplet[0] == target[0]) found0 = true;
40+
if (triplet[1] == target[1]) found1 = true;
41+
if (triplet[2] == target[2]) found2 = true;
42+
}
43+
44+
return found0 && found1 && found2;
45+
}
46+
}
47+
48+
// Alternative approach using early termination
49+
class SolutionEarlyTermination {
50+
public boolean mergeTriplets(int[][] triplets, int[] target) {
51+
boolean[] found = new boolean[3];
52+
53+
for (int[] triplet : triplets) {
54+
if (triplet[0] > target[0] || triplet[1] > target[1] || triplet[2] > target[2]) {
55+
continue;
56+
}
57+
58+
for (int i = 0; i < 3; i++) {
59+
if (triplet[i] == target[i]) {
60+
found[i] = true;
61+
}
62+
}
63+
64+
// Early termination if all positions found
65+
if (found[0] && found[1] && found[2]) {
66+
return true;
67+
}
68+
}
69+
70+
return found[0] && found[1] && found[2];
71+
}
72+
}
73+
74+
// Alternative approach using iterative
75+
class SolutionIterative {
76+
public boolean mergeTriplets(int[][] triplets, int[] target) {
77+
boolean[] found = new boolean[3];
78+
79+
for (int i = 0; i < triplets.length; i++) {
80+
int[] triplet = triplets[i];
81+
82+
if (triplet[0] > target[0] || triplet[1] > target[1] || triplet[2] > target[2]) {
83+
continue;
84+
}
85+
86+
for (int j = 0; j < 3; j++) {
87+
if (triplet[j] == target[j]) {
88+
found[j] = true;
89+
}
90+
}
91+
}
92+
93+
return found[0] && found[1] && found[2];
94+
}
95+
}
96+
97+
// Alternative approach using while loop
98+
class SolutionWhileLoop {
99+
public boolean mergeTriplets(int[][] triplets, int[] target) {
100+
boolean[] found = new boolean[3];
101+
int i = 0;
102+
103+
while (i < triplets.length) {
104+
int[] triplet = triplets[i];
105+
106+
if (triplet[0] > target[0] || triplet[1] > target[1] || triplet[2] > target[2]) {
107+
i++;
108+
continue;
109+
}
110+
111+
for (int j = 0; j < 3; j++) {
112+
if (triplet[j] == target[j]) {
113+
found[j] = true;
114+
}
115+
}
116+
117+
i++;
118+
}
119+
120+
return found[0] && found[1] && found[2];
121+
}
122+
}
123+
124+
// Alternative approach using enhanced for loop
125+
class SolutionEnhancedForLoop {
126+
public boolean mergeTriplets(int[][] triplets, int[] target) {
127+
boolean[] found = new boolean[3];
128+
129+
for (int[] triplet : triplets) {
130+
if (triplet[0] > target[0] || triplet[1] > target[1] || triplet[2] > target[2]) {
131+
continue;
132+
}
133+
134+
for (int i = 0; i < 3; i++) {
135+
if (triplet[i] == target[i]) {
136+
found[i] = true;
137+
}
138+
}
139+
}
140+
141+
return found[0] && found[1] && found[2];
142+
}
143+
}
144+
145+
// More concise version
146+
class SolutionConcise {
147+
public boolean mergeTriplets(int[][] triplets, int[] target) {
148+
boolean[] found = new boolean[3];
149+
for (int[] triplet : triplets) {
150+
if (triplet[0] > target[0] || triplet[1] > target[1] || triplet[2] > target[2]) continue;
151+
for (int i = 0; i < 3; i++) {
152+
if (triplet[i] == target[i]) found[i] = true;
153+
}
154+
}
155+
return found[0] && found[1] && found[2];
156+
}
157+
}

0 commit comments

Comments
 (0)