forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neetcode-gh#1225 from Mahim1997/dev/1899_swift
1899. Merge Triplets to Form Target Triplet
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |