Skip to content

Commit aab08b2

Browse files
committed
feat: add solutions to lc problem: No.1899
No.1899.Merge Triplets to Form Target Triplet
1 parent e81ecb2 commit aab08b2

File tree

7 files changed

+207
-73
lines changed

7 files changed

+207
-73
lines changed

solution/1800-1899/1899.Merge Triplets to Form Target Triplet/README.md

+79-25
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@
1212

1313
<ul>
1414
<li>选出两个下标(下标 <strong>从 0 开始</strong> 计数)<code>i</code> 和 <code>j</code>(<code>i != j</code>),并 <strong>更新</strong> <code>triplets[j]</code> 为 <code>[max(a<sub>i</sub>, a<sub>j</sub>), max(b<sub>i</sub>, b<sub>j</sub>), max(c<sub>i</sub>, c<sub>j</sub>)]</code> 。
15-
1615
<ul>
1716
<li>例如,<code>triplets[i] = [2, 5, 3]</code> 且 <code>triplets[j] = [1, 7, 5]</code>,<code>triplets[j]</code> 将会更新为 <code>[max(2, 1), max(5, 7), max(3, 5)] = [2, 7, 5]</code> 。</li>
1817
</ul>
1918
</li>
20-
2119
</ul>
2220

2321
<p>如果通过以上操作我们可以使得目标 <strong>三元组</strong> <code>target</code> 成为 <code>triplets</code> 的一个 <strong>元素</strong> ,则返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
@@ -75,6 +73,16 @@
7573

7674
<!-- 这里可写通用的实现逻辑 -->
7775

76+
**方法一:贪心**
77+
78+
我们记 $target = [x, y, z]$,初始时 $d = e = f = 0$,表示当前的 $a, b, c$ 的最大值。
79+
80+
我们遍历数组 $triplets$,对于每个三元组 $[a, b, c]$,如果 $a \le x, b \le y, c \le z$,则将 $d, e, f$ 分别更新为 $max(d, a), max(e, b), max(f, c)$。
81+
82+
最后判断 $[d, e, f]$ 是否等于 $target$ 即可。
83+
84+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $triplets$ 的长度。
85+
7886
<!-- tabs:start -->
7987

8088
### **Python3**
@@ -84,14 +92,14 @@
8492
```python
8593
class Solution:
8694
def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:
87-
maxA = maxB = maxC = 0
88-
tA, tB, tC = target
95+
x, y, z = target
96+
d = e = f = 0
8997
for a, b, c in triplets:
90-
if a <= tA and b <= tB and c <= tC:
91-
maxA = max(maxA, a)
92-
maxB = max(maxB, b)
93-
maxC = max(maxC, c)
94-
return (maxA, maxB, maxC) == (tA, tB, tC)
98+
if a <= x and b <= y and c <= z:
99+
d = max(d, a)
100+
e = max(e, b)
101+
f = max(f, c)
102+
return [d, e, f] == target
95103
```
96104

97105
### **Java**
@@ -101,35 +109,81 @@ class Solution:
101109
```java
102110
class Solution {
103111
public boolean mergeTriplets(int[][] triplets, int[] target) {
104-
int maxA = 0, maxB = 0, maxC = 0;
105-
for (int[] triplet : triplets) {
106-
int a = triplet[0], b = triplet[1], c = triplet[2];
107-
if (a <= target[0] && b <= target[1] && c <= target[2]) {
108-
maxA = Math.max(maxA, a);
109-
maxB = Math.max(maxB, b);
110-
maxC = Math.max(maxC, c);
112+
int x = target[0], y = target[1], z = target[2];
113+
int d = 0, e = 0, f = 0;
114+
for (var t : triplets) {
115+
int a = t[0], b = t[1], c = t[2];
116+
if (a <= x && b <= y && c <= z) {
117+
d = Math.max(d, a);
118+
e = Math.max(e, b);
119+
f = Math.max(f, c);
111120
}
112121
}
113-
return maxA == target[0] && maxB == target[1] && maxC == target[2];
122+
return d == x && e == y && f == z;
114123
}
115124
}
116125
```
117126

127+
### **C++**
128+
129+
```cpp
130+
class Solution {
131+
public:
132+
bool mergeTriplets(vector<vector<int>>& triplets, vector<int>& target) {
133+
int x = target[0], y = target[1], z = target[2];
134+
int d = 0, e = 0, f = 0;
135+
for (auto& t : triplets) {
136+
int a = t[0], b = t[1], c = t[2];
137+
if (a <= x && b <= y && c <= z) {
138+
d = max(d, a);
139+
e = max(e, b);
140+
f = max(f, c);
141+
}
142+
}
143+
return d == x && e == y && f == z;
144+
}
145+
};
146+
```
147+
148+
### **Go**
149+
150+
```go
151+
func mergeTriplets(triplets [][]int, target []int) bool {
152+
x, y, z := target[0], target[1], target[2]
153+
d, e, f := 0, 0, 0
154+
for _, t := range triplets {
155+
a, b, c := t[0], t[1], t[2]
156+
if a <= x && b <= y && c <= z {
157+
d = max(d, a)
158+
e = max(e, b)
159+
f = max(f, c)
160+
}
161+
}
162+
return d == x && e == y && f == z
163+
}
164+
165+
func max(a, b int) int {
166+
if a > b {
167+
return a
168+
}
169+
return b
170+
}
171+
```
172+
118173
### **TypeScript**
119174

120175
```ts
121176
function mergeTriplets(triplets: number[][], target: number[]): boolean {
122-
let [x, y, z] = target; // 目标值
123-
let [i, j, k] = [0, 0, 0]; // 最大值
124-
for (let triplet of triplets) {
125-
let [a, b, c] = triplet; // 当前值
177+
const [x, y, z] = target;
178+
let [d, e, f] = [0, 0, 0];
179+
for (const [a, b, c] of triplets) {
126180
if (a <= x && b <= y && c <= z) {
127-
i = Math.max(i, a);
128-
j = Math.max(j, b);
129-
k = Math.max(k, c);
181+
d = Math.max(d, a);
182+
e = Math.max(e, b);
183+
f = Math.max(f, c);
130184
}
131185
}
132-
return i == x && j == y && k == z;
186+
return d === x && e === y && f === z;
133187
}
134188
```
135189

solution/1800-1899/1899.Merge Triplets to Form Target Triplet/README_EN.md

+69-25
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010

1111
<ul>
1212
<li>Choose two indices (<strong>0-indexed</strong>) <code>i</code> and <code>j</code> (<code>i != j</code>) and <strong>update</strong> <code>triplets[j]</code> to become <code>[max(a<sub>i</sub>, a<sub>j</sub>), max(b<sub>i</sub>, b<sub>j</sub>), max(c<sub>i</sub>, c<sub>j</sub>)]</code>.
13-
1413
<ul>
1514
<li>For example, if <code>triplets[i] = [2, 5, 3]</code> and <code>triplets[j] = [1, 7, 5]</code>, <code>triplets[j]</code> will be updated to <code>[max(2, 1), max(5, 7), max(3, 5)] = [2, 7, 5]</code>.</li>
1615
</ul>
1716
</li>
18-
1917
</ul>
2018

2119
<p>Return <code>true</code> <em>if it is possible to obtain the </em><code>target</code><em> <strong>triplet</strong> </em><code>[x, y, z]</code><em> as an<strong> element</strong> of </em><code>triplets</code><em>, or </em><code>false</code><em> otherwise</em>.</p>
@@ -68,50 +66,96 @@ The target triplet [5,5,5] is now an element of triplets.
6866
```python
6967
class Solution:
7068
def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:
71-
maxA = maxB = maxC = 0
72-
tA, tB, tC = target
69+
x, y, z = target
70+
d = e = f = 0
7371
for a, b, c in triplets:
74-
if a <= tA and b <= tB and c <= tC:
75-
maxA = max(maxA, a)
76-
maxB = max(maxB, b)
77-
maxC = max(maxC, c)
78-
return (maxA, maxB, maxC) == (tA, tB, tC)
72+
if a <= x and b <= y and c <= z:
73+
d = max(d, a)
74+
e = max(e, b)
75+
f = max(f, c)
76+
return [d, e, f] == target
7977
```
8078

8179
### **Java**
8280

8381
```java
8482
class Solution {
8583
public boolean mergeTriplets(int[][] triplets, int[] target) {
86-
int maxA = 0, maxB = 0, maxC = 0;
87-
for (int[] triplet : triplets) {
88-
int a = triplet[0], b = triplet[1], c = triplet[2];
89-
if (a <= target[0] && b <= target[1] && c <= target[2]) {
90-
maxA = Math.max(maxA, a);
91-
maxB = Math.max(maxB, b);
92-
maxC = Math.max(maxC, c);
84+
int x = target[0], y = target[1], z = target[2];
85+
int d = 0, e = 0, f = 0;
86+
for (var t : triplets) {
87+
int a = t[0], b = t[1], c = t[2];
88+
if (a <= x && b <= y && c <= z) {
89+
d = Math.max(d, a);
90+
e = Math.max(e, b);
91+
f = Math.max(f, c);
92+
}
93+
}
94+
return d == x && e == y && f == z;
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
bool mergeTriplets(vector<vector<int>>& triplets, vector<int>& target) {
105+
int x = target[0], y = target[1], z = target[2];
106+
int d = 0, e = 0, f = 0;
107+
for (auto& t : triplets) {
108+
int a = t[0], b = t[1], c = t[2];
109+
if (a <= x && b <= y && c <= z) {
110+
d = max(d, a);
111+
e = max(e, b);
112+
f = max(f, c);
93113
}
94114
}
95-
return maxA == target[0] && maxB == target[1] && maxC == target[2];
115+
return d == x && e == y && f == z;
96116
}
117+
};
118+
```
119+
120+
### **Go**
121+
122+
```go
123+
func mergeTriplets(triplets [][]int, target []int) bool {
124+
x, y, z := target[0], target[1], target[2]
125+
d, e, f := 0, 0, 0
126+
for _, t := range triplets {
127+
a, b, c := t[0], t[1], t[2]
128+
if a <= x && b <= y && c <= z {
129+
d = max(d, a)
130+
e = max(e, b)
131+
f = max(f, c)
132+
}
133+
}
134+
return d == x && e == y && f == z
135+
}
136+
137+
func max(a, b int) int {
138+
if a > b {
139+
return a
140+
}
141+
return b
97142
}
98143
```
99144

100145
### **TypeScript**
101146

102147
```ts
103148
function mergeTriplets(triplets: number[][], target: number[]): boolean {
104-
let [x, y, z] = target; // 目标值
105-
let [i, j, k] = [0, 0, 0]; // 最大值
106-
for (let triplet of triplets) {
107-
let [a, b, c] = triplet; // 当前值
149+
const [x, y, z] = target;
150+
let [d, e, f] = [0, 0, 0];
151+
for (const [a, b, c] of triplets) {
108152
if (a <= x && b <= y && c <= z) {
109-
i = Math.max(i, a);
110-
j = Math.max(j, b);
111-
k = Math.max(k, c);
153+
d = Math.max(d, a);
154+
e = Math.max(e, b);
155+
f = Math.max(f, c);
112156
}
113157
}
114-
return i == x && j == y && k == z;
158+
return d === x && e === y && f === z;
115159
}
116160
```
117161

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
bool mergeTriplets(vector<vector<int>>& triplets, vector<int>& target) {
4+
int x = target[0], y = target[1], z = target[2];
5+
int d = 0, e = 0, f = 0;
6+
for (auto& t : triplets) {
7+
int a = t[0], b = t[1], c = t[2];
8+
if (a <= x && b <= y && c <= z) {
9+
d = max(d, a);
10+
e = max(e, b);
11+
f = max(f, c);
12+
}
13+
}
14+
return d == x && e == y && f == z;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func mergeTriplets(triplets [][]int, target []int) bool {
2+
x, y, z := target[0], target[1], target[2]
3+
d, e, f := 0, 0, 0
4+
for _, t := range triplets {
5+
a, b, c := t[0], t[1], t[2]
6+
if a <= x && b <= y && c <= z {
7+
d = max(d, a)
8+
e = max(e, b)
9+
f = max(f, c)
10+
}
11+
}
12+
return d == x && e == y && f == z
13+
}
14+
15+
func max(a, b int) int {
16+
if a > b {
17+
return a
18+
}
19+
return b
20+
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
class Solution {
22
public boolean mergeTriplets(int[][] triplets, int[] target) {
3-
int maxA = 0, maxB = 0, maxC = 0;
4-
for (int[] triplet : triplets) {
5-
int a = triplet[0], b = triplet[1], c = triplet[2];
6-
if (a <= target[0] && b <= target[1] && c <= target[2]) {
7-
maxA = Math.max(maxA, a);
8-
maxB = Math.max(maxB, b);
9-
maxC = Math.max(maxC, c);
3+
int x = target[0], y = target[1], z = target[2];
4+
int d = 0, e = 0, f = 0;
5+
for (var t : triplets) {
6+
int a = t[0], b = t[1], c = t[2];
7+
if (a <= x && b <= y && c <= z) {
8+
d = Math.max(d, a);
9+
e = Math.max(e, b);
10+
f = Math.max(f, c);
1011
}
1112
}
12-
return maxA == target[0] && maxB == target[1] && maxC == target[2];
13+
return d == x && e == y && f == z;
1314
}
1415
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Solution:
22
def mergeTriplets(self, triplets: List[List[int]], target: List[int]) -> bool:
3-
maxA = maxB = maxC = 0
4-
tA, tB, tC = target
3+
x, y, z = target
4+
d = e = f = 0
55
for a, b, c in triplets:
6-
if a <= tA and b <= tB and c <= tC:
7-
maxA = max(maxA, a)
8-
maxB = max(maxB, b)
9-
maxC = max(maxC, c)
10-
return (maxA, maxB, maxC) == (tA, tB, tC)
6+
if a <= x and b <= y and c <= z:
7+
d = max(d, a)
8+
e = max(e, b)
9+
f = max(f, c)
10+
return [d, e, f] == target
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
function mergeTriplets(triplets: number[][], target: number[]): boolean {
2-
let [x, y, z] = target; // 目标值
3-
let [i, j, k] = [0, 0, 0]; // 最大值
4-
for (let triplet of triplets) {
5-
let [a, b, c] = triplet; // 当前值
2+
const [x, y, z] = target;
3+
let [d, e, f] = [0, 0, 0];
4+
for (const [a, b, c] of triplets) {
65
if (a <= x && b <= y && c <= z) {
7-
i = Math.max(i, a);
8-
j = Math.max(j, b);
9-
k = Math.max(k, c);
6+
d = Math.max(d, a);
7+
e = Math.max(e, b);
8+
f = Math.max(f, c);
109
}
1110
}
12-
return i == x && j == y && k == z;
11+
return d === x && e === y && f === z;
1312
}

0 commit comments

Comments
 (0)