888. Fair Candy Swap。

本文详细解析了如何通过算法实现两个朋友之间的糖果公平交换。通过计算每个人糖果的总和并找到能够平衡差值的糖果,使得交换后双方的糖果数量相等。介绍了使用vector和set两种数据结构的实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Alice has, and B[j] is the size of the j-th bar of candy that Bob has.
Since they are friends, they would like to exchange one candy bar each so that after the exchange, they both have the same total amount of candy. (The total amount of candy a person has is the sum of the sizes of candy bars they have.)

Return an integer array ans where ans[0] is the size of the candy bar that Alice must exchange, and ans[1] is the size of the candy bar that Bob must exchange.

If there are multiple answers, you may return any one of them. It is guaranteed an answer exists.

Example 1:

Input: A = [1,1], B = [2,2]
Output: [1,2]

Example 2:

Input: A = [1,2], B = [2,3]
Output: [1,2]

Example 3:

Input: A = [2], B = [1,3]
Output: [2,3]

Example 4:

Input: A = [1,2,5], B = [2,4]
Output: [5,4]

原文链接:https://leetcode.com/problems/fair-candy-swap/


文中的Alice 和 Bob分别有不同数量的糖果,数组A代表Alice拥有的糖果,其中索引 i 上的元素A[i]代表第 i 种糖果有多少个,B也是如此。现在需要让这两个人分别从各自的糖果中取出一种与对方交换,然后必须满足交换之后两个人糖果总数相等。


按照题意,交换之后每个人的糖果数目是一样的,所以我们可以先分别计算出之前每个人的糖果总和,并且相加除以2得到最后每个人应该拥有的数目。

在这里插入图片描述

然后用原来A拥有的糖果总数减去平均的数目,会得到一个差值diff,这个差值就是A与B进行交换之后需要抵消的差值。

在这里插入图片描述

然后开始遍历A种的所有糖果,当发现A种的某一类糖果数量(也就是某一个元素数值)减去这个差值diff之后恰好等于B种的某一类糖果数量 x - diff = y,也就是说交换x和y之后正好能够弥补掉这个差值diff。所以这里的x和y就是这两个人各自需要交换的糖果,也就是所求的最终答案。

在这里插入图片描述

class Solution {
public:
    vector<int> fairCandySwap(vector<int>& A, vector<int>& B) {
        vector<int> res;
        int Asum=accumulate(A.begin(),A.end(),0);//计算出AB的总和
        int Bsum=accumulate(B.begin(),B.end(),0);

        int diff = (Asum - Bsum)/2;//计算出A与平均值之间的差距是多少,等同于下面的运算
        //int diff = Asum - (Asum + Bsum)/2;

        for(int i : A) {
            if(find(B.begin(),B.end(),i-diff)!=B.end()) {
                res.push_back(i);
                res.push_back(i-diff);
                break;
            }
        }

        return res;
    }
};

利用set会快上很多。

class Solution {
public:
    vector<int> fairCandySwap(vector<int>& A, vector<int>& B) {
        int sum_A=0;
        int sum_B=0;
        for(int i=0;i<A.size();i++)
            sum_A += A[i];
        for(int j=0;j<B.size();j++)
            sum_B += B[j];
        int diff = (sum_A-sum_B)/2;
        unordered_set<int> hashset(B.begin(),B.end());
        for (int i =0;i<A.size();i++)
        {
            if ((A[i]-diff)>=1)
            {
                auto elem = hashset.find(A[i]-diff);
                if (elem != hashset.end())
                {
                    vector<int> result {A[i], *elem};
                    return result;
                }
            }
        }
    }
};
### Linux Redis配置文件 `redis.conf` 中 Swap 设置说明 在Linux环境中,Redis配置文件`redis.conf`提供了多个选项来控制内存管理行为,其中包括与交换空间(swap)相关的间接影响因素。虽然Redis本身并没有直接针对swap的特定配置项[^1],但是有两个重要的参数可以间接影响到Redis对于物理内存和虚拟内存(即swap)之间的交互: #### maxmemory 和 maxmemory-policy 参数 - **maxmemory**:此参数用于设定Redis实例能够使用的最大内存量。一旦达到这个限制,根据所选策略执行相应的操作。这有助于防止Redis占用过多内存而导致系统频繁使用swap。 ```bash # Example of setting maximum memory limit to 2GB maxmemory 2gb ``` - **maxmemory-policy**:定义了当到达`maxmemory`上限时采取何种淘汰算法处理数据。不同的驱逐政策会影响性能以及哪些键可能会被移除以释放更多可用RAM而不是依赖于操作系统层面的swap机制。 ```bash # Possible values include noeviction, allkeys-lru, volatile-lru etc. maxmemory-policy allkeys-lru ``` 这些设置并不是直接作用于swap上的开关或阈值调整;而是通过合理规划Redis内部的数据存储量级及其缓存清除逻辑,在一定程度上减少因过度消耗实际物理内存而触发的操作系统级别的页面置换活动[^2]。 为了更好地管理和优化Redis服务下的资源分配情况,建议定期监控服务器的整体负载状况并依据业务需求适时修改上述提到的关键字眼所在行的内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值