sanjar-notes / dsa

C++ basics, Data structures and algorithms.
https://sanjar-notes.github.io/dsa/
17 stars 9 forks source link

Dividing problem into "very small halves" for DnC may not always work #14

Open sanjarcode opened 2 years ago

sanjarcode commented 2 years ago

I first encountered the problem here. Dividing into halves doesn't work here because the minimum problem size is > 2 (4 in this case).

Questions:

  1. Can it be made to work by doing halfsies?
  2. If not, how to divide the problem then?

https://leetcode.com/submissions/detail/740514206/

sanjarcode commented 2 years ago

Also, it's important to solve non-classic problems that use DnC (e.g. just knowing merge sort as a way to do DnC is not good enough). My way is this - write two functions - helper and combine. combine does solution re-combination (the second half of DnC). The helper does just one thing - it solves partitions of the problem, and combines the solutions using combine.

sanjarcode commented 2 years ago

Closely related, https://github.com/sanjar-notes/dsa_with_cpp/issues/15