Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1225 from Mahim1997/dev/1899_swift
Browse files Browse the repository at this point in the history
1899. Merge Triplets to Form Target Triplet
  • Loading branch information
Ahmad-A0 authored Oct 8, 2022
2 parents 96e2423 + 15f1aa1 commit 32fa8bc
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions swift/1899-Merge-Triplets-To-Form-Target-Triplet.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
func mergeTriplets(_ triplets: [[Int]], _ target: [Int]) -> Bool {
var doesOneValidTripletExist: Bool = false
var maxFromTriplets: [Int] = [0, 0, 0] // 1 <= elements <= 1000

for triplet in triplets {
// Ignore those triplets which are not within range
var isWithinRange: Bool = true
for i in 0..<3 {
if triplet[i] > target[i] {
isWithinRange = false
break
}
}

guard isWithinRange else { continue }
doesOneValidTripletExist = true;

// Max triplet computation
for i in 0..<3 {
maxFromTriplets[i] = max(maxFromTriplets[i], triplet[i])
}
}

return doesOneValidTripletExist && (maxFromTriplets == target)
}
}

0 comments on commit 32fa8bc

Please sign in to comment.