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 块糖的大小. 因为他们是朋友,所以他们想交换一个糖果棒,这样交换后,他们都有相同的糖果总量.( ...
随机推荐
- MongoCola使用教程 1 - MongoDB的基本操作和聚合功能---Mongdb客户端软件操作说明
前言 在开始正文之前,感谢博客园的Nosql爱好者对于MongoCola工具的试用(使用).特别感谢 呆呆 这位朋友的Bug报告,让我纠正了一个很严重的Bug.同时也感谢以前的多个网友在博客留言中给我 ...
- react-hooks: CSSProperties
1.定义变量 const divStyle: React.CSSProperties = { width: "11rem", height: "7rem", b ...
- Java 使用 long 出现空指针异常
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545} p.p2 {margin: ...
- 如何限制mongodb数据库内存占用量方法
多实例下: 可以通过绑定cpu,来防止多实例相互干扰. mongodb的内存也可以限制主,防止全部内存都被一个实例占据. ulimit -s 4096 && ulimit -m 314 ...
- linux rhel7下安装python
1.查看是否已经安装Python Centos7默认安装了python2.7.5 因为一些命令要用它比如yum 它使用的是python2.7.5. 使用python -V命令查看一下是否安装Pytho ...
- C# 操作嵌入的资源
被嵌入的程序执行文件 namespace AppTest { class Program { static void Main(string[] args) { Console.WriteLine(& ...
- Git克隆、修改、更新项目,及查看项目地址命令
第一步:在本地新建一个文件夹,作为本地仓库,如“texzt”,直接打开该文件夹,并单击右键,选择git bash here 则可以直接进入到该文件夹目录下. 第二步:将本地仓库初始化,命令:git i ...
- 利用git bash和git gui向git远程仓库提交文件
1.首先在该文件夹下git init 2.然后在github下面创建一个新仓库去存储你的代码 3.然后利用add添加远程仓库 4.然后点击stage changed 5.最后点击长传 参考链接:htt ...
- phpredis的使用
phpredis的具体使用方法可以参照:https://github.com/phpredis/phpredis
- 探秘VB.net中的shared与static
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/huyuyang6688/article/details/28230345 简单了解了一 ...