Optimal Material Blend
Livspace designers meticulously select materials for their projects, balancing aesthetics with practical requirements like durability. For certain custom composite elements, two base materials are blended. The combined durability of these blended materials is simply the sum of their individual durability scores. To meet precise engineering specifications, it's crucial to identify two available materials whose combined durability score is as close as possible to a predetermined targetDurability.
Your task is to implement a function findOptimalMaterialBlend that takes an array of durabilityScores for available materials and a targetDurability as input. The function should identify two distinct materials (i.e., from different original positions in the input array) whose sum of scores has the minimum absolute difference from the targetDurability.
[min(idx1, idx2), max(idx1, idx2)]).[index1, index2] must always have index1 < index2.Function Signature:
function findOptimalMaterialBlend(durabilityScores: number[], targetDurability: number): number[]
Input Parameters:
durabilityScores: An array of numbers, where durabilityScores[i] represents the durability score of the i-th material.targetDurability: A number representing the desired total durability score.Return Value:
number[] of length 2, [index1, index2], representing the 0-based original indices of the two materials that form the optimal blend. index1 will always be less than index2.Examples
Example 1
Input:
durabilityScores = [10, 20, 30, 40], targetDurability = 55
Output:
[0, 3]
Note:
Original indices: 10(0), 20(1), 30(2), 40(3). Sorted (score, original_index) pairs: [(10,0), (20,1), (30,2), (40,3)].
Example 2
Input:
durabilityScores = [5, 15, 25, 35], targetDurability = 30
Output:
[0, 2]
Note:
Original indices: 5(0), 15(1), 25(2), 35(3). Sorted (score, original_index) pairs: [(5,0), (15,1), (25,2), (35,3)].
Example 3
Input:
durabilityScores = [100, 50, 200, 150], targetDurability = 270
Output:
[0, 3]
Note:
Original indices: 100(0), 50(1), 200(2), 150(3). Sorted (score, original_index) pairs: [(50,1), (100,0), (150,3), (200,2)].
Constraints
2 <= durabilityScores.length <= 10^51 <= durabilityScores[i] <= 10^91 <= targetDurability <= 2 * 10^9durabilityScores are unique (no two materials have the same score).O(N log N) time and O(N) space complexities.Loading editor...