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

本文探讨了分治算法在解决复杂问题中的应用,以硬币伪造者问题为例,介绍了如何通过将问题分解为更小的子问题,然后逐一治理并最终合并解决方案来找到伪造硬币的方法。文章详细解释了在O(logn)次称重中找出伪造硬币的分治算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分治算法

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)

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值