Consider a devotee wishing to give offerings to temples along a mountain range. The temples are located in a row at different heights. Each temple should receive at least one offering. If two adjacent temples are at different altitudes, then the temple that is higher up should receive more offerings than the one that is lower down. If two adjacent temples are at the same height, then their offerings relative to each other does not matter. Given the number of temples and the heights of the temples in order, find the minimum number of offerings to bring.
Input : 3 1 2 2 Output : 4 All temples must receive at-least one offering. Now, the second temple is at a higher altitude compared to the first one. Thus it receives one extra offering. The second temple and third temple are at the same height, so we do not need to modify the offerings. Offerings given are therefore: 1, 2, 1 giving a total of 4. Input : 6 1 4 3 6 2 1 Output : 10 We can distribute the offerings in the following way, 1, 2, 1, 3, 2, 1. The second temple has to receive more offerings than the first due to its height being higher. The fourth must receive more than the fifth, which in turn must receive more than the sixth. Thus the total becomes 10.
We notice that each temple can either be above, below, or at the same level as the temple next to it. The offerings required at each temple is equal to the maximum length of the chain of temples at lower height as shown in the image.
To follow the given rule, a temple must be offered at least x+1 where x is maximum of following two.
- Number of temples on left in increasing order.
- Number of temples on right in increasing order.
A naive method of solving this problem would be for each temple, go to the left until altitude increases, and do the same for the right.
Time complexity: O(n2)
Space complexity: O(1)
Dynamic Programming Approach
By using Dynamic Programming, we can improve the time complexity. In this method, we create a structure of length n which maintains the maximum decreasing chain to the left of each temple and the maximum decreasing chain to the right of each temple. We go through once from 0 to N setting the value of left for each temple. We then go from N to 0 setting the value of right for each temple. We then compare the two and pick the maximum for each temple.
Time complexity: O(n)
Space complexity: O(n)
This article is contributed by Aditya Kamath. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Paths requiring minimum number of jumps to reach end of array
- Length of longest increasing absolute even subsequence
- Count N digits numbers with sum divisible by K
- Maximize length of longest increasing prime subsequence from the given array
- Longest increasing subsequence which forms a subarray in the sorted representation of the array
- Maximum score assigned to a subsequence of numerically consecutive and distinct array elements
- Maximum subsequence sum possible by multiplying each element by its index
- Count unimodal and non-unimodal permutations of first N natural numbers
- Number from a range [L, R] having Kth minimum cost of conversion to 1 by given operations
- Generate a combination of minimum coins that sums to a given value
- Longest subsequence forming an Arithmetic Progression (AP)
- Maximize sum of remaining elements after every removal of the array half with greater sum
- Length of longest increasing prime subsequence from a given array
- Maximize cost to empty an array by removing contiguous subarrays of equal elements