The Divide an Array Into Subarrays With Minimum Cost I coding problem asks you to divide an array into 3 contiguous subarrays. The "cost" of a subarray is its first element. You want to minimize the total cost, which is the sum of the first elements of these 3 subarrays. Crucially, the first subarray must start at index 0.
American Express and other financial firms use this question to test basic Greedy interview patterns and sorting logic. It evaluation your ability to realize that since the first element is fixed (index 0), the problem simplifies to finding the two smallest elements in the rest of the array to serve as the "heads" of the remaining two subarrays.
This problem uses a Greedy approach with Sorting (or finding the top two minimums).
nums[0].nums[0] + nums[i] + nums[j], you simply need to find the two smallest values in the array nums[1...n-1].nums = [10, 3, 1, 15, 2]
nums[0] = 10.[3, 1, 15, 2].Read carefully! In "minimum cost" problems, check if any part of the cost is "fixed." If it is, focus entirely on optimizing the remaining variable parts. Finding the two smallest numbers in an array is an operation using two variables, or using sorting.