Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Happy Line cf -549G
- We have an array of persons (we will call it persons, persons[n - 1] — the head, persons[0] — the tail). For each person we will calculate this number val = money of a person — (number of steps to reach the head of the queue). Next we sort persons by val.
- Suppose there is an answer ans. ( ans[0] — the tail, ans[n - 1] — the head). ans[i].money — how much money is left with i th person in ans.
- persons[n - 1].val ≥ ans[n - 1].money, so we can transform the answer so that persons[n - 1] is in the head.
- persons[n - 2].val + 1 ≥ ans[n - 2].money, so again we can transform the answer. Why val + 1? Because we need to get to n - 2, no to n - 1.
- Then persons[i].val + (n - i - 1) ≥ ans[i].money. So you can put persons[i] in the ans on ith position with money persons[i].val + (n - i - 1).
- Excuse me for my mistakes.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement