ADA算法知识(四)Divide-and-conquer algorithm

分治算法

Divide the problem into smaller sub-problems.
Governance - breaking down these smaller sub-problems one by one;
Combination - merge the solved sub-problems and finally get the solution of the "mother" problem

Like this question:

[The Coin Counterfeiter]

Give a divide-and-conquer algorithm to find the counterfeit coin in O(logn) weighings. You may assume that n is a power of 2.

So

Like give n as 16

16     8      4      2        1

four progress

Through O (1) operation, the problem of size n into a n/2 problem 

so T(n)=T(n/2)+O(1)

扫描二维码关注公众号,回复: 4725091 查看本文章

and divide and Conquer: by O (1) operation, the problem of size n into 2 n/2 problems.

so T(n)=2T(n/2)+O(1)

it is obvious that it is a binary search question

When solving the main problems, I will divide the main problems into two sub-problems, for example, dividing n piles of coins into two piles of 2/n piles. If the pile is lighter, the bad coins will be in that pile. After repeated operation, the light piles of coins will be picked out and divided into two piles of the same number of coins until the bad coins are found

猜你喜欢

转载自blog.csdn.net/qq_42615643/article/details/85124805