Posts

Showing posts with the label Array

Maximum Value of an Ordered Triplet I - Two Solutions [JS]

Description   Solution 1: Prefix Max Summary: Anchor at  nums[j] . We want  nums[i]  and  nums[k]  to be as larger as possible, and  nums[j]  to be as small as possible to ensure the maximum value. Calculate the maximum value on the right of each index  i :  maxRight[i]  = maximum value from index  i  to  n - 1 Go through  nums  from left to right and take each element as  nums[j] . Keep track of the maximum value on the left. Get the maximum value on the left and right of  nums[j] . Record the maximum value. Time Complexity:  O(n) Space Complexity:  O(n) var maximumTripletValue = function ( nums ) { let n = nums.length, maxRight = [...nums]; for ( let i = n - 2 ; i >= 0 ; i--) { maxRight[i] = Math .max(nums[i], maxRight[i + 1 ]); } let maxLeft = 0 , ans = 0 ; for ( let j = 0 ; j < n; j++) { if (j > 0 && j < n - 1 ) { ans = Math .max(an...

Maximum Value of an Ordered Triplet II - Two Solutions [JS]

Description   Solution 1: Prefix Max Summary: Anchor at  nums[j] . We want  nums[i]  and  nums[k]  to be as larger as possible, and  nums[j]  to be as small as possible to ensure the maximum value. Calculate the maximum value on the right of each index  i :  maxRight[i]  = maximum value from index  i  to  n - 1 Go through  nums  from left to right and take each element as  nums[j] . Keep track of the maximum value on the left. Get the maximum value on the left and right of  nums[j] . Record the maximum value. Time Complexity:  O(n) Space Complexity:  O(n) var maximumTripletValue = function ( nums ) { let n = nums.length, maxRight = [...nums]; for ( let i = n - 2 ; i >= 0 ; i--) { maxRight[i] = Math .max(nums[i], maxRight[i + 1 ]); } let maxLeft = 0 , ans = 0 ; for ( let j = 0 ; j < n; j++) { if (j > 0 && j < n - 1 ) { ans = Math .max(an...