解决leetcode第3943题递增后的数对数量

3943.递增后的数对数量

难度:困难

问题描述:

给你两个整数数组nums1和nums2,以及一个二维整数数组queries。

每个queriesi都属于以下两种类型之一:

1,x,y,val:将nums2x..y中的每个元素都增加val。

2,tot:计算满足nums1j+nums2k==tot的数对(j,k)的数量。

返回一个整数数组answer,其中answerj表示第j个类型2查询的数对数量。

示例1:

输入:nums1=1,2,nums2=3,4,queries=\[2,5,1,0,0,2,2,5]

输出:2,1

解释:

queries0=2,5:有效数对为nums10+nums21=1+4=5和nums11+nums20=2+3=5。

queries1=1,0,0,2:将nums20增加2,得到nums2=5,4

queries2=2,5:有效数对为nums10+nums21=1+4=5。

因此,answer=2,1

示例2:

输入:nums1=1,1,nums2=2,2,3,queries=\[2,4,1,0,1,1,2,4]

输出:2,6

解释:

queries0=2,4:有效数对为nums10+nums22=1+3和nums11+nums22=1+3。

queries1=1,0,1,1:将nums20和nums21各增加1,得到nums2=3,3,3

queries2=2,4:nums1=1,1中的每个元素都可以与nums2=3,3,3中的每个元素配对,因为1+3=4,总共有2×3=6个数对。

因此,answer=2,6

示例3:

输入:nums1=2,5,8,4,nums2=1,3,8,queries=\[2,9,1,1,2,1,2,10]

输出:1,0

解释:

queries0=2,9:唯一有效数对为nums12+nums20=8+1=9。

queries1=1,1,2,1:将nums21和nums22各增加1,得到nums2=1,4,9

queries2=2,10:没有数对的和为10。

因此,answer=1,0

提示:

1<=nums1.length<=5

1<=nums2.length<=5*10**4

1<=nums1i,nums2i<=10**5

1<=queries.length<=5*10**4

queriesi.length==2 or 4

queriesi==1,x,y,val,或

queriesi==2,tot

0<=x<=y<nums2.length

1<=val<=10**5

1<=tot<=10**9

问题分析:

对于本题来说,从查询序列中取出每一个查询,按要求进行处理即可。对于第一种类型的查询1,x,y,val,将nums2x..y中的每个元素都增加val,这通过函数increase_the_value_of_each_element_within_the_specified_range_by_val(num2,querie)实现,对于第二种类型的查询2,tot,计算满足nums1j+nums2k==tot的数对(j,k)的数量,这通过函数get_ordered_pair_that_meets_the_conditions(num1,num2,tot)实现,在主程序中,只需要依次对查询序列queries中的每个查询,调用相应的处理函数进行处理,最后将数对数量的列表输出即解决了问题。

程序如下:

python 复制代码
#计算满足 nums1[j] + nums2[k] == tot 的数对 (j, k)的数量并返回
def get_ordered_pair_that_meets_the_conditions(num1,num2,tot):
    m=len(num1)
    n=len(num2)
    a=[]
    for i in range(m):
        for j in range(n):
            if num1[i]+num2[j]==tot:
                a.append([i,j])
    return len(a)

#将nums2[x..y] 中的每个元素都增加val
def increase_the_value_of_each_element_within_the_specified_range_by_val(num2,querie):
    n=len(num2)
    x=querie[1]
    y=querie[2]
    val=querie[3]
    for i in range(x,y+1):
        num2[i]=num2[i]+val
    return num2

#主程序
num1=eval(input("enter the first num1="))
num2=eval(input("enter the second num2="))
queries=eval(input("enter the second queries="))
a=[]
for i in queries:
    b=i[0]
    if b==1:
        num2=increase_the_value_of_each_element_within_the_specified_range_by_val(num2,i)
    else:
        c=get_ordered_pair_that_meets_the_conditions(num1,num2,i[1])
        a.append(c)
print(a)

运行实例一

enter the first num1=1,2,3,4

enter the second num2=2,3,2,2

enter the second queries=\[1,0,2,2,2,5,1,2,3,1,2,6]

3, 4

运行实例二

enter the first num1=1,1,2,2,3

enter the second num2=2,2,3,2,2

enter the second queries=\[2,5,1,1,3,1,2,7]

6, 1

相关推荐
AI科技星13 分钟前
《01无穷全域信息场论:算子G与宇宙本体高维完备公理大典》
人工智能·python·算法·金融·乖乖数学·全域数学
Tim_1028 分钟前
【C++】013、空类占多少字节?
算法
Ricky_Theseus32 分钟前
并查集:连通性问题
算法
Tairitsu_H1 小时前
[LC优选算法#18] 前缀和 | 除⾃⾝以外数组的乘积 | 和为K的⼦数组 | 和可被K整除的⼦数组
算法·前缀和·哈希算法
2601_962341302 小时前
计算机毕业设计之jsp考研在线复习平台
java·大数据·开发语言·hadoop·python·考研·课程设计
凯瑟琳.奥古斯特2 小时前
力扣1008:前序重建BST
开发语言·c++·算法·leetcode·职场和发展
qq_383189622 小时前
07-运放运用电路-程控增益放大器
算法
云烟成雨TD2 小时前
LangFlow 1.x 系列【7】工作流创建与部署指南
人工智能·python·agent
月疯2 小时前
np.where()[0]的用法
开发语言·python·numpy
aqiu1111112 小时前
【算法日记 13】LeetCode 49. 字母异位词分组:哈希表的进阶“降维打击”
算法·leetcode·散列表