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. 转: OLAP引擎——Kylin介绍

    本文转自:http://blog.csdn.net/yu616568/article/details/48103415 ,如有侵犯,立刻删除.  Kylin是ebay开发的一套OLAP系统,与Mond ...

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

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

  3. Celery分布式应用

    最近有应用需要部署到不同的服务器上运行,但是有没有PBS这样的调度系统,就想起来Python的调度神器 Celery.现在针对我的实际应用做一些记录. 1. 安装 因为我并不注重结果而是把命令拿到不同 ...

  4. 目标跟踪之ECO:Efficient Convolution Operators for Tracking

    一. 相关滤波算法总结 作者首先分析了 影响相关滤波算法效率 和 导致过拟合 的几个原因: 1)Model Size (模型大小) 包括两个方面: - 模型层数,对应多分辨率 Sample,比如多层 ...

  5. js 下获取子元素的方法

    笔记核心: firstElementChild只会获取元素节点对象,从名称就可以看出来,firstChild则可以获取文本节点对象(当然也可以获取元素节点对象),比如空格和换行都被当做文本节点. js ...

  6. Oracle:Create, alter, drop and add!

    Oacle 下对标下元素约束条件的添加与删除 插入元素 alter table table_name add add_name data_type; 添加约束条件 NOT NULL alter tab ...

  7. self = [super init] 最终解释

    答:      init 中调用super的 init方法来初始化自己所包含有的父类信息 1.内存分配      内存应该在[Class alloc]的时候就已经分配了,大小和类型应该由对应的Clas ...

  8. PING分组网间探测 ICMP协议

      1.Ping的基础知识 Ping是潜水艇人员的专用术语,表示回应的声纳脉冲,在网络中Ping 是一个十分好用的TCP/IP工具.它主要的功能是用来检测网络的连通情况和分析网络速度.是ICMP的一个 ...

  9. fpga加法进位链实现过程中的一个特点

    altera fpga 用quartus综合后会出现加法进位链一正一反的情况,所谓一正一反指的是假设某一级输入为a,b,进位值为c,则该级进位链逻辑应该为cout=ab+ac+bc,但实际为 cout ...

  10. 配置linux下面python的开发环境

    1.首先安装Python开发环境 http://blog.csdn.net/testcs_dn/article/details/51253365 2.安装django开发环境 http://blog. ...