I found one greedy algorithm problem, where we have to find min no. of jumps required to make persons sit together.
Here is a link to the article for reference.
Question Description from article:
Title:
Minimum jumps required to make a group of persons sit together.Description:
Given a string S of length N consisting of ‘x’ and ‘.’. The given string represents a row of seats where ‘x’ and ‘.’ represent occupied and unoccupied seats respectively. The task is to minimize the total number of hops or jumps to make all the occupants sit together i.e., next to each other, without having any vacant seat between them.Examples:
Input: S = “. . . . x . . x x . . . x . .”
Output: 5
Explanation: Make the person at 5th seat jump 2 places to the 7th seat. Make the person at 13th seat jump 3 places to the 10th seat.Therefore, total number of jumps required = 2 + 3 = 5.Input: S = “x x x . . . . . . . . x x x x x x”
Output: 24
Explanation: Move the occupants from 1st, 2nd and 3rd position to the 9th, 10th, 11th positions respectively. Therefore, the total number of jumps required = (11 – 3) + (10 – 2) + (9 – 3) = 24.
Approach:
The idea is to use a Greedy Approach to solve this problem. Observe that it is always optimal to shift the elements towards the median element among the persons or the center person among all the persons present. The number of jumps will always be minimum when we shift points to the median.
The greedy solution says to shift the elements towards the median. But no proof of this has been found. Does there exist some mathematical proof of using this approach?