列表

详情


1865. 找出和为指定值的下标对

给你两个整数数组 nums1nums2 ,请你实现一个支持下述两类查询的数据结构:

  1. 累加 ,将一个正整数加到 nums2 中指定下标对应元素上。
  2. 计数 ,统计满足 nums1[i] + nums2[j] 等于指定值的下标对 (i, j) 数目(0 <= i < nums1.length0 <= j < nums2.length)。

实现 FindSumPairs 类:

 

示例:

输入:
["FindSumPairs", "count", "add", "count", "count", "add", "add", "count"]
[[[1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]], [7], [3, 2], [8], [4], [0, 1], [1, 1], [7]]
输出:
[null, 8, null, 2, 1, null, null, 11]

解释:
FindSumPairs findSumPairs = new FindSumPairs([1, 1, 2, 2, 2, 3], [1, 4, 5, 2, 5, 4]);
findSumPairs.count(7);  // 返回 8 ; 下标对 (2,2), (3,2), (4,2), (2,4), (3,4), (4,4) 满足 2 + 5 = 7 ,下标对 (5,1), (5,5) 满足 3 + 4 = 7
findSumPairs.add(3, 2); // 此时 nums2 = [1,4,5,4,5,4]
findSumPairs.count(8);  // 返回 2 ;下标对 (5,2), (5,4) 满足 3 + 5 = 8
findSumPairs.count(4);  // 返回 1 ;下标对 (5,0) 满足 3 + 1 = 4
findSumPairs.add(0, 1); // 此时 nums2 = [2,4,5,4,5,4]
findSumPairs.add(1, 1); // 此时 nums2 = [2,5,5,4,5,4]
findSumPairs.count(7);  // 返回 11 ;下标对 (2,1), (2,2), (2,4), (3,1), (3,2), (3,4), (4,1), (4,2), (4,4) 满足 2 + 5 = 7 ,下标对 (5,3), (5,5) 满足 3 + 4 = 7

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class FindSumPairs { public: FindSumPairs(vector<int>& nums1, vector<int>& nums2) { } void add(int index, int val) { } int count(int tot) { } }; /** * Your FindSumPairs object will be instantiated and called as such: * FindSumPairs* obj = new FindSumPairs(nums1, nums2); * obj->add(index,val); * int param_2 = obj->count(tot); */

java 解法, 执行用时: 155 ms, 内存消耗: 73 MB, 提交时间: 2023-03-09 17:31:47

class FindSumPairs {
	int[] nums1;
	int[] nums2;
	Map<Integer, Integer> hash;

	public FindSumPairs(int[] nums1, int[] nums2) {
		this.nums1 = nums1;
		this.nums2 = nums2;
		hash = new HashMap<>();
		// hash num1 会超时
		for (int i = 0; i < nums2.length; i++) {
			int num = nums2[i];
			hash.put(num, hash.getOrDefault(num, 0) + 1);
		}
	}

	public void add(int index, int val) {
		hash.put(nums2[index], hash.get(nums2[index]) - 1);
		hash.put(nums2[index] + val, hash.getOrDefault(nums2[index]+val, 0) + 1);
		nums2[index] += val;
	}

	public int count(int tot) {
		int ans = 0;
		for (int i = 0; i < nums1.length; i++) {
			int dif = tot - nums1[i];
			ans += hash.getOrDefault(dif, 0);
		}
		return ans;
	}
}

/**
 * Your FindSumPairs object will be instantiated and called as such:
 * FindSumPairs obj = new FindSumPairs(nums1, nums2);
 * obj.add(index,val);
 * int param_2 = obj.count(tot);
 */

golang 解法, 执行用时: 228 ms, 内存消耗: 28.5 MB, 提交时间: 2023-03-09 17:26:25

type FindSumPairs struct{}

var (
	x, y []int
	cnt  map[int]int
)

func Constructor(nums1, nums2 []int) (_ FindSumPairs) {
	cnt = map[int]int{}
	for _, v := range nums2 {
		cnt[v]++
	}
	x, y = nums1, nums2
	return
}

func (FindSumPairs) Add(i, val int) {
	cur := y[i]
	cnt[cur]--
	cnt[cur+val]++
	y[i] += val
}

func (FindSumPairs) Count(tot int) (ans int) {
	for _, v := range x {
		ans += cnt[tot-v]
	}
	return
}


/**
 * Your FindSumPairs object will be instantiated and called as such:
 * obj := Constructor(nums1, nums2);
 * obj.Add(index,val);
 * param_2 := obj.Count(tot);
 */

cpp 解法, 执行用时: 244 ms, 内存消耗: 72 MB, 提交时间: 2023-03-09 17:25:05

class FindSumPairs {
private:
    vector<int> nums1, nums2;
    unordered_map<int, int> cnt;

public:
    FindSumPairs(vector<int>& nums1, vector<int>& nums2) {
        this->nums1 = nums1;
        this->nums2 = nums2;
        for (int num: nums2) {
            ++cnt[num];
        }
    }
    
    void add(int index, int val) {
        --cnt[nums2[index]];
        nums2[index] += val;
        ++cnt[nums2[index]];
    }
    
    int count(int tot) {
        int ans = 0;
        for (int num: nums1) {
            int rest = tot - num;
            if (cnt.count(rest)) {
                ans += cnt[rest];
            }
        }
        return ans;
    }
};

/**
 * Your FindSumPairs object will be instantiated and called as such:
 * FindSumPairs* obj = new FindSumPairs(nums1, nums2);
 * obj->add(index,val);
 * int param_2 = obj->count(tot);
 */

python3 解法, 执行用时: 608 ms, 内存消耗: 45.1 MB, 提交时间: 2023-03-09 17:24:03

class FindSumPairs:

    def __init__(self, nums1: List[int], nums2: List[int]):
        self.nums1 = nums1
        self.nums2 = nums2
        self.cnt = collections.Counter(nums2)
        

    def add(self, index: int, val: int) -> None:
        self.cnt[self.nums2[index]] -= 1
        self.nums2[index] += val
        self.cnt[self.nums2[index]] += 1


    def count(self, tot: int) -> int:
        ans = 0
        for num in self.nums1:
            if tot - num in self.cnt:
                ans += self.cnt[tot-num]
            
        return ans
        


# Your FindSumPairs object will be instantiated and called as such:
# obj = FindSumPairs(nums1, nums2)
# obj.add(index,val)
# param_2 = obj.count(tot)

python3 解法, 执行用时: 708 ms, 内存消耗: 44.9 MB, 提交时间: 2023-03-09 17:21:38

class FindSumPairs:

    def __init__(self, nums1: List[int], nums2: List[int]):
        self.nums1 = nums1
        self.nums2 = nums2
        self.cnt = collections.Counter(nums2)
        

    def add(self, index: int, val: int) -> None:
        _nums2, _cnt = self.nums2, self.cnt

        _cnt[_nums2[index]] -= 1
        _nums2[index] += val
        _cnt[_nums2[index]] += 1


    def count(self, tot: int) -> int:
        _nums1, _cnt = self.nums1, self.cnt

        ans = 0
        for num in _nums1:
            if (rest := tot - num) in _cnt:
                ans += _cnt[rest]
        return ans
        


# Your FindSumPairs object will be instantiated and called as such:
# obj = FindSumPairs(nums1, nums2)
# obj.add(index,val)
# param_2 = obj.count(tot)

上一题