zoukankan      html  css  js  c++  java
  • 1529. 绝对差不超过限制的三元子数组

    1529. 绝对差不超过限制的三元子数组

    中文English

    给定一个递增的整数数组nums,和一个表示限制的整数limit,请你返回满足条件的三元子数组的个数,使得该子数组中的任意两个元素之间的绝对差小于或者等于limit

    如果不存在满足条件的子数组,则返回 0 。

    样例

    样例 1:

    输入:[1, 2, 3, 4], 3
    输出:4
    解释:可选方案有(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)。因此,满足条件的三元组有4个。
    

    样例 2:

    输入:[1, 10, 20, 30, 50], 19
    输出:1
    解释:唯一可行的三元组是(1, 10, 20),所以答案为1。
    

    挑战

    你可以只用O(n)的时间复杂度解决这个问题吗?

    注意事项

    数据范围:1 ≤ len(nums) ≤ 1e4,1 ≤ limit ≤ 1e6,0 ≤ nums[i] ≤ 1e6
    由于答案可能很大,请返回它对99997867取余后的结果。

     
     
    输入测试数据 (每行一个参数)如何理解测试数据?
    class Solution:
        """
        @param nums: the given array
        @param limit: the limit on the absolute difference of the subarray
        @return: Find the number of triplet subarray with the absolute difference less than or equal to limit
        """
        """
        大致思路:
        1.求出A和C之间的存在的最多元素,然后求组合,累计
        """
        def tripletSubarray(self, nums, limit):
            # write your code here
    
            total = 0
            l = len(nums)
            for i in range(2,l):
                for j in range(i - 1):
                    if nums[i] - nums[j] <= limit:
                        total += i - j - 1
            
            return total
  • 相关阅读:
    python 异常处理
    python 文件
    python 面向对象程序设计基础
    python 数据结构与算法
    python中的基本数据结构
    关于python程序设计流程(未完结)
    python的字典(dict)的键值对存储规则
    python中字符串的方法及注释
    Element.requestFullscreen()方法
    oncontextmenu 事件
  • 原文地址:https://www.cnblogs.com/yunxintryyoubest/p/13138409.html
Copyright © 2011-2022 走看看