LintCode 1187: K-diff Pairs in an Array (同向双指针)

本文介绍了一种使用同向双指针算法解决K-diffPairsinanArray问题的方法,该问题要求在给定数组中找到所有唯一k差对的数量。通过排序和使用双指针技巧,可以有效地解决此问题。
  1. K-diff Pairs in an Array
    Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.

Example
Example 1:

Input: [3, 1, 4, 1, 5], k = 2
Output: 2
Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5).
Although we have two 1s in the input, we should only return the number of unique pairs.
Example 2:

Input:[1, 2, 3, 4, 5], k = 1
Output: 4
Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
Example 3:

Input: [1, 3, 1, 5, 4], k = 0
Output: 1
Explanation: There is one 0-diff pair in the array, (1, 1).
Notice
1.The pairs (i, j) and (j, i) count as the same pair.
2.The length of the array won’t exceed 10,000.
3.All the integers in the given input belong to the range: [-1e7, 1e7].

思路:同向双指针。记得先排序。
注意:
1)感觉好像两个数之和等于某个固定数就用反向双指针。
如果两个数之差等于某个固定数就用同向双指针。

代码如下:

class Solution {
public:
    /**
     * @param nums: an array of integers
     * @param k: an integer
     * @return: the number of unique k-diff pairs
     */
    int findPairs(vector<int> &nums, int k) {
        
        int result = 0;
        int len = nums.size();
        if (len <= 1) return 0;
        
        sort(nums.begin(),  nums.end());
        int p1 = 0, p2 = 1; // p2 is ahead of p1
        set<pair<int, int>> s;
        if (k < 0) k = -k;  //important!
        
        while(p1 < p2) {
            int diff = nums[p2] - nums[p1]; 
            if (diff == k) {
                s.insert(make_pair(min(nums[p1], nums[p2]), max(nums[p1], nums[p2])));
                p1++; p2++;
            } else if (diff < k) {
                p2++;
            } else {
                p1++;
            }
        }
        
        return s.size();
    }
};
请使用c++解决以下问题: ## 题目描述 给定长度为 $n$ 的正整数序列 $a$,求 $g(a)$ 的值: $$ g(a)=\max_{a'}\{f(a')\}\\ f(a)=\sum_{i=2}^{n} |a_{i}-a_{i-1}| $$ 其中,$a'$ 是序列 $a$ 经过任意重排后得到的任意一个序列。 但是这实在是一个三岁小宝宝都会的简单题,因此你想对 $m$ 个序列都求出上述式子的最大值,这 $m$ 个序列满足以下条件。 - 第 $i$ 个序列 $p_i$ 长度为 $i$。 - 第一个序列 $p_1$ 为 $[b'_1=b_1]$。 - 第 $i$($2\leq i\leq m$)个序列 $p_i$ 满足:对于任意满足 $1\leq j<i$ 的正整数 $j$ 满足 $p_{i,j}=p_{i-1,j}$,且 $p_{i,i} = b'_i=b_i\oplus (T\cdot f(p_{i-1}))$。 其中,$\oplus$ 表示二进制按位异或。 ## 输入格式 第一行两个非负整数 $m$ 和 $T$,分别表示你需要求出重排后 $f$ 最大值的序列个数以及生成序列参数。 第二行 $n$ 个非负整数表示 $b_1,\dots,b_m$。 **注意:【数据范围】一节仅对 $b'_i$ 的范围做出了保证,没有对 $b_i$ 的范围做出保证。** ## 输出格式 共一行一个非负整数,为 $\oplus_{i=1}^{m} g(p_i)$。 ## 输入输出样例 #1 ### 输入 #1 ``` 5 0 1 2 3 4 5 ``` ### 输出 #1 ``` 14 ``` ## 输入输出样例 #2 ### 输入 #2 ``` 8 0 5 2 7 1 4 3 8 6 ``` ### 输出 #2 ``` 2 ``` ## 说明/提示 **【样例解释 #1】** $g(p_1),g(p_2),\dots,g(p_m)$ 分别为 $0,1,3,7,11$。 令 $p'_i$ 为 $p_i$ 重排后**任意**满足 $f(p'_i)$ 最大的序列。 $p_1=[1]$,$p'_1=[1]$,$f(p'_1)=0$。 $p_2=[1,2]$,$p'_2=[1,2]$,$f(p'_2)=|1-2|=1$。 $p_3=[1,2,3]$,$p'_3=[1,3,2]$,$f(p'_3)=|1-3|+|3-2|=3$。 $p_4=[1,2,3,4]$,$p'_4=[3,1,4,2]$,$f(p'_4)=|3-1|+|1-4|+|4-2|=7$。 $p_5=[1,2,3,4,5]$,$p'_5=[4,2,5,1,3]$,$f(p'_5)=|4-2|+|2-5|+|5-1|+|1-3|=11$。 该样例满足测试点 $1$ 的限制。 **【样例解释 #2】** $g(p_1),g(p_2),\dots,g(p_m)$ 分别为 $0,3,8,15,17,19,27,31$。 该样例满足测试点 $3$ 的限制。 **【数据范围】** 对于全部测试点:$1\leq m\leq 3\times 10^6$,$1\leq b'_i\leq 10^9$,$T\in \{0,1\}$。 | 测试点编号 | $m\leq$ | $T=$ | 特殊性质 | | :---------: | :------------: | :--: | :------: | | $1$ | $8$ | $0$ | AB | | $2$ | $100$ | $0$ | AB | | $3$ | $10^3$ | $0$ | AB | | $4$ | $2\times 10^5$ | $0$ | AB | | $5$ | $2\times 10^5$ | $0$ | A | | $6$ | $2\times 10^5$ | $1$ | AB | | $7$ | $2\times 10^5$ | $1$ | A | | $8$ | $2\times 10^5$ | $1$ | 无 | | $9$ | $10^6$ | $1$ | 无 | | $10$ | $3\times 10^6$ | $1$ | 无 | 特殊性质 A:$b'_i\leq m$($1\leq i\leq m$)。 特殊性质 B:$b'_i\neq b'_j$($1\leq i<j\leq m$)。
10-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值