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. ubuntu 交叉编译arm linux 内核小例子

    下载arm-linux-gcc 4.2.3http://code.google.com/p/princess-alist/downloads/detail?name=arm-linux-gcc-4.3 ...

  2. dubbo-monitor安装、 监控中心 配置过程

    简单介绍下monitor: Simple Monitor挂掉不会影响到Consumer和Provider之间的调用,所以用于生产环境不会有风险. 配置好了之后可以结合admin管理后台使用,可以清晰的 ...

  3. 原创:Angular新手容易碰到的坑,随时更新,欢迎订阅

    在Angular群里回答新手问题一段时间了,有一些Angular方面的坑留在这里备查,希望能对各位有所帮助.这个文章将来会随时更新,不会单独开新章,欢迎各位订阅. Q1. <div ng-inc ...

  4. Kotlin For Gank.io (干货集中营Kotlin实现)

    介绍 Kotlin,现在如火如荼,所以花了一点时间把之前的项目用Kotlin重构一下 原项目地址:https://github.com/onlyloveyd/GankIOClient 对应Kotlin ...

  5. Golang 编译成 DLL 文件

    golang 编译 dll 过程中需要用到 gcc,所以先安装 MinGW. windows 64 位系统应下载 MinGW 的 64 位版本: https://sourceforge.net/pro ...

  6. 伪元素:placeholder-shown&&:focus-within

    :placeholder-shown 另外,划重点,这个伪类是仍处于实验室的方案.也就是未纳入标准,当然我们的目的是探寻有意思的 CSS . 当 input 类型标签使用了 placeholder 属 ...

  7. Chrome 的书签太多如何分类整理比较好

    对于Chrome书签太多,如何进行分类整理,下面给出几种方法~ 工具/原料 电脑 Chrome浏览器 delicious.Diigo等 方法/步骤1 1.把书签中常用网站只保存LOGO,放在书签栏最前 ...

  8. netcat 瑞士军刀

    netcat被誉为网络安全界的‘瑞士军刀’,一个简单而有用的工具,透过使用TCP或UDP协议的网络连接去读写数据.它被设计成一个稳定的后门工具,能够直接由其它程序和脚本轻松驱动.同时,它也是一个功能强 ...

  9. Centos7下命令笔记-ls

    ls命令大概是linux下最常用的命令之一,ls是list的缩写.因为linux目录或者文件记录的信息实在太多,所以默认ls只显示非隐藏的目录以及文件名.ls直接执行不加参数时显示本目录下的档案名. ...

  10. 洛谷 P1561 [USACO12JAN]爬山Mountain Climbing

    传送门 题目大意: n头牛,上山时间为u(i),下山为d(i). 要求每一时刻最多只有一头牛上山,一头牛下山. 问每头牛都上下山后花费最少时间. 题解:贪心 推了推样例,发现上山时间一定,那找个下山最 ...