Skip to content

Commit 32fa8bc

Browse files
authored
Merge pull request neetcode-gh#1225 from Mahim1997/dev/1899_swift
1899. Merge Triplets to Form Target Triplet
2 parents 96e2423 + 15f1aa1 commit 32fa8bc

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
func mergeTriplets(_ triplets: [[Int]], _ target: [Int]) -> Bool {
3+
var doesOneValidTripletExist: Bool = false
4+
var maxFromTriplets: [Int] = [0, 0, 0] // 1 <= elements <= 1000
5+
6+
for triplet in triplets {
7+
// Ignore those triplets which are not within range
8+
var isWithinRange: Bool = true
9+
for i in 0..<3 {
10+
if triplet[i] > target[i] {
11+
isWithinRange = false
12+
break
13+
}
14+
}
15+
16+
guard isWithinRange else { continue }
17+
doesOneValidTripletExist = true;
18+
19+
// Max triplet computation
20+
for i in 0..<3 {
21+
maxFromTriplets[i] = max(maxFromTriplets[i], triplet[i])
22+
}
23+
}
24+
25+
return doesOneValidTripletExist && (maxFromTriplets == target)
26+
}
27+
}

0 commit comments

Comments
 (0)