There are simpler linear time solution available done with DP or Greedy.
Type your question above this line.
Course: https://www.educative.io/collection/5668639101419520/5633779737559040
Lesson: https://www.educative.io/collection/page/5668639101419520/5633779737559040/5643365030821888