leetcode888
class Solution {
public:
int Binary_Search(vector<int> x, int N, int keyword)
{
int low = , high = N - , mid;
while (low <= high)
{
mid = (low + high) / ;
if (x[mid] == keyword)
return mid;
if (x[mid] < keyword)
low = mid + ;
else
high = mid - ;
}
return -;
}
vector<int> fairCandySwap(vector<int>& A, vector<int>& B) {
int sumA = ;
for (auto a : A)
{
sumA += a;
}
int sumB = ;
for (auto b : B)
{
sumB += b;
}
sort(A.begin(), A.end());
sort(B.begin(), B.end());
int sum = sumA + sumB;//8 = 3 + 5
int mid = sum / ;//4 = 8 / 2
vector<int> V;
if (sumA == sumB)
{
V.push_back();
V.push_back();
}
else if (sumA < sumB)
{
int diff = mid - sumA;//1 = 4 - 3
for (auto a : A)
{
int attemptB = a + diff;//b =a + diff
int positionB = Binary_Search(B, B.size(), attemptB);
if (positionB != -)
{
V.push_back(a);
V.push_back(B[positionB]);
break;
}
}
}
else//sumA>sumB
{
int diff = mid - sumB;
for (auto b : B)
{
int attemptA = b + diff;
int positionA = Binary_Search(A, A.size(), attemptA);
if (positionA != -)
{
V.push_back(A[positionA]);
V.push_back(b);
break;
}
}
}
return V;
}
};
leetcode888的更多相关文章
- [Swift]LeetCode888. 公平的糖果交换 | Fair Candy Swap
Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Ali ...
- Leetcode888.Fair Candy Swap公平的糖果交换
爱丽丝和鲍勃有不同大小的糖果棒:A[i] 是爱丽丝拥有的第 i 块糖的大小,B[j] 是鲍勃拥有的第 j 块糖的大小. 因为他们是朋友,所以他们想交换一个糖果棒,这样交换后,他们都有相同的糖果总量.( ...
随机推荐
- VMware Workstation/Fusion 14/15 密钥
VMware WorkStation 14 CG54H-D8D0H-H8DHY-C6X7X-N2KG6 ZC3WK-AFXEK-488JP-A7MQX-XL8YF AC5XK-0ZD4H-088HP- ...
- 使用 minio 搭建私有对象存储云。aws-php-sdk 操作object
How to use AWS SDK for PHP with Minio Server aws-sdk-php is the official AWS SDK for the PHP program ...
- 在Ajax.ActionLink的OnBegin,onComplete等事件中使用this【解决办法】
方法就是修改这个文件[jquery.unobtrusive-ajax.js] options.data.push({ name: "X-Requested-With", value ...
- CSS命名规范参考及书写注意事项
CSS书写顺序 *{ /*显示属性*/ display position float clear cursor … /*盒模型*/ margin padding width height /*排版*/ ...
- ACM学习历程—HDU5701 中位数计数(中位数 && 计数排序)
http://acm.hdu.edu.cn/showproblem.php?pid=5701 这是这次百度之星初赛2B的第六题.之前白山云做过类似的题,省赛完回来,我看了一下大概就有这样的思路:首先枚 ...
- Windows 7 中的 God Mode
Windows 7系统中隐藏了一个秘密的“God Mode”,字面上译为“上帝模式”,但似乎叫它“万能模式”更贴切一些.God Mode其实就是一个简单的文件夹窗口,但包含了几乎所有Windows 7 ...
- linux 本地账号密码无法登陆(shell可以登录),一直返回 登陆的login界面
今天我在我虚拟机测试的时候遇到了一个问题.登陆centos一直是返回login,账号和密码没错,我也换了两个用户. 1.问题描述 我正常的输入用户名和密码 错误提示截图:返回登陆界面,我重新试了另外的 ...
- Arcmap10.1下安装ArcBrutile0.2.2 (Win7)(转)
前阵子换了高级新电脑,用的win7旗舰版装了Arcgis10.1,一直没试过ArcBrutile0.2.2能不能用,今天想用的时候发现自己竟然忘记怎么加载这个工具了!!! 网上搜了一下,度娘今天不 ...
- 半导体巨头青睐物联网领域 众强联手打造MCU生态系统
随着万物互联的时代到来,众多半导体巨头纷纷转战物联网领域.早在十年前,意法半导体曾将STM32推向市场,意法半导体对32位MCU在物联网方面的应用在两年前就已展开攻势. 4月25日,历经两届盛况的ST ...
- python一个简单的web服务器和客户端
服务器: 当客户联系时创建一个连接套接字 从这个连接接收HTTP请求(*) 解释该请求所请求的特定文件 从服务器的文件系统获取该文件 并发送文件内容 ...