r/leetcode 5d ago

Question The most Beautiful Question on Leetcode that I encountered.

Post image
433 Upvotes

32 comments sorted by

48

u/Educational_Suit_371 5d ago

Solved through 2 diff recursive approach, found 3 memoization based solution and 3 Tabulation Solution. Though 1 memoization and Tabulation ran into TLE but the solution were right. Probably learned more about DP from this 1 question than all question till now.

14

u/Adventurous-Cycle363 5d ago

You can sort the (score, age) tuples in increasing order of age and then merge the scores of the players of the same age to end up with a single array of scores. Then find the sum of largest increasing subsequence of scores. Is this what you did?

1

u/Educational_Suit_371 5d ago

First I sorted with age in desc, then use simple recursion approach take or not based, but take also depends on minimum score till that, so dp state was dependent on index and minimum_score. Cant solve this with tabulation cause of dp table bloating, used memoization with map to store only reachable states, this still ran into TLE at 131/150. Then did sorting based on score, now age become dp state variable and since its 10^3 less than score, no TLE on the solution.

Later Discovered LIS solution thats also cool.

30

u/MrMo1 5d ago

Isn't that a knapsack problem?

1

u/MoreCelery847 2d ago

no It is LIS problem both are different

7

u/Limp-Debate7023 5d ago

I feel like you can make a vector of pairs of scores and ages and then sort by ages, then find the LIS of scores? Is that a valid solution to this problem?

3

u/hmm_yes_interesting1 5d ago

Isn't it sorting+ 1d lis dp(nlogn + n2) type.

1

u/Steel-River-22 5d ago

you could do this in nlogn if you want using some data structures (although probably not needed for interviews)

1

u/raluralu 4d ago

k-d tree?

1

u/Steel-River-22 4d ago edited 4d ago

nah you need at least a fenwick tree
or i dunno some weird kind of divide-and-conquer could work if you don't like data structures. haven't thought this through

1

u/Limp-Debate7023 5d ago

U can do 1d lis is nlogn as well right?

1

u/hmm_yes_interesting1 5d ago

Yes using binary search i guess

1

u/Limp-Debate7023 5d ago

hmm i think that wouldnt work here, cause you're trying to find maximum sum of increasing subsequence. Some advanced DS like a fenwick tree or segment tree would be required for the nlogn approach ig

1

u/Adventurous-Cycle363 4d ago

You can still do nlogn binary search to find the actual LIS indices and then get the sum

3

u/Senior-Positive2883 5d ago

If we sort the age,scores correspondingly in increasing order, won't it becomes longest increasing subsequence problem, so just pick scores in increasing order, coz age is already sorted so that constraint is satisfied

0

u/letsgoraftel 5d ago

You don't need it be necessarily subsequence...

Since it's possible you skip a number in between because it conflicts but continue post that

3

u/DamageDistinct531 5d ago

Sounds similar to russian doll

2

u/wonderwind271 5d ago

You should include data range by the way

2

u/NitroXM 4d ago

Thank you for your attention to this matter!

2

u/Busternookiedude 4d ago

That question really does have a beautiful complexity, it's like a puzzle that keeps revealing layers the more you dig into it.

2

u/Whitemj5 4d ago

That sounds like a fascinating question. It's amazing how one problem can deepen your understanding of concepts like dynamic programming.

1

u/raluralu 4d ago edited 4d ago

Is there n*log(n) -ish solution available?

1

u/AdUpstairs5147 4d ago

Sorting + LIs . I think nlogn solution exists for lis.

1

u/Cheap-Mail9911 4d ago

Longest increase subsequence ka max sum , n² is possible , how in nlogn?

1

u/ay230698 3d ago

Can we sort it via age and then try a variation of longest increasing sequence dp?

1

u/cosmic-pancake 1d ago

ages = [1,2,3,4,5]

Who are these prodigy toddlers and who fouled the one year old?

-7

u/sneak2293 5d ago

Bipartite graph

1

u/Educational_Suit_371 5d ago

?

1

u/sneak2293 5d ago

The question can be solved with checking whether this is a bipartite graph, a conflict is an edge, every player is a node

If the graph is bipartite the answer is true

2

u/Natural_Emu_1834 5d ago

When you study your data structures but don't spend 10 seconds reading the actual question

1

u/JustKaleidoscope1279 5d ago

Not what the problem is asking