石头配对两种方案

Problem 2: Find stone pair(s) There are N number of stones, labeled as [0, N-1]. We know the weight of each of those stones. We want to find ONE stone pair, i.e. 2 stones, whose weight difference is D. Question A Formally describe the problem to clearly define the problem without ambiguity. Something like: write a function F that … the inputs of the function are … the output of the function are …. This question might be harder than you thought. Please think carefully. Question B Write the function you described in your answer to question A. What is the space complexity of your algorithm? What is the time complexity? Can you achieve O(N) for both time complexity and space complexity? If yes, please implement it. Write a test program to test your function. Please list all corner cases you want to test How to verify the function can process these corner cases correctly? Can you do it in a more systematic way? Question C (optional) Same as the original question B, but this time we want to find ALL stone pairs whose weight difference is D. Please note that pair (1, 4) and pair (4, 1) are considered as the same pair, so only need to return one. Your algorithm’s time complexity should be O(max(R, N)), R is the number of result pairs, N is the number of stones.

问题A 定义一个函数F,输入是一个整数列表weights和一个整数D,weights列表中的每个元素代表每块石头的重量,
D是两块石头的重量差。函

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值