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的更多相关文章

  1. [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 ...

  2. Leetcode888.Fair Candy Swap公平的糖果交换

    爱丽丝和鲍勃有不同大小的糖果棒:A[i] 是爱丽丝拥有的第 i 块糖的大小,B[j] 是鲍勃拥有的第 j 块糖的大小. 因为他们是朋友,所以他们想交换一个糖果棒,这样交换后,他们都有相同的糖果总量.( ...

随机推荐

  1. 【河南省第十届ACM 省赛 A-谍报分析】

    题目描述 “八一三”淞沪抗战爆发后,*几次准备去上海前线视察和指挥作战.但都因为宁沪之间的铁路和公路遭到了敌军的严密封锁,狂轰滥炸,一直未能成行. 特科组织,其主要任务是保卫的安全,了解和掌握敌方的动 ...

  2. LeetCode OJ:Jump Game(跳跃游戏)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. 地图之CoreLocation

    1. 在Info.plist文件中添加下面两句话 NSLocationAlwaysUsageDescription —> 确定定位吗?亲  (或者改参数类型为BOOL类型 值为Y) 请求的授权, ...

  4. How your script code be coverted into arm code and running on ios.

    Your script code is compiled into DLLs (assemblies) by the editor. When you build for iOS, these ass ...

  5. Visual Studio和Sublime Text全系列激活码

    自己经常用,做个备份 VS2012 Microsoft Visual Studio Ultimate 2012 旗舰版 有效注册密钥: YKCW6-BPFPF-BT8C9-7DCTH-QXGWC Mi ...

  6. (六)HTML5立方体动画设置

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. SSH实现远程控制

    SSH(Secure Shell)是一种能够提供安全远程登录会话的协议,使用ssh可以在远程linux中执行命令. sshd服务提供两种安全验证的方法: (1)基于口令的安全验证:经过验证帐号与密码即 ...

  8. 十八、python沉淀之路--生成器

    一.生成器 生成器总结:语法上和函数类似:生成器函数和常规函数几乎是一样的.他们都是使用def语句进行定义,差别在于生成器使用yield语句返回一个值,而常规函数使用return语句返回一个值.自动实 ...

  9. Linux 系统 LVM(Logical Volume Manager)逻辑卷管理

    一.前言 每个Linux使用者在安装Linux时都会遇到这样的困境:在为系统分区时,如何精确评估和分配各个硬盘分区的容量,因为系统管理员不但要考虑到 当前某个分区需要的容量,还要预见该分区以后可能需要 ...

  10. (转)android - anim translate中 fromXDelta、toXDelta、fromYDelta、toXDelta属性

    2012-03-23 15:51 16144人阅读 评论(5) 收藏 举报 android <set xmlns:android="http://schemas.android.com ...