leetcode - 2616. Minimize the Maximum Difference of Pairs

这篇具有很好参考价值的文章主要介绍了leetcode - 2616. Minimize the Maximum Difference of Pairs。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Description

You are given a 0-indexed integer array nums and an integer p. Find p pairs of indices of nums such that the maximum difference amongst all the pairs is minimized. Also, ensure no index appears more than once amongst the p pairs.

Note that for a pair of elements at the index i and j, the difference of this pair is |nums[i] - nums[j]|, where |x| represents the absolute value of x.

Return the minimum maximum difference among all p pairs. We define the maximum of an empty set to be zero.

Example 1:

Input: nums = [10,1,2,7,1,3], p = 2
Output: 1
Explanation: The first pair is formed from the indices 1 and 4, and the second pair is formed from the indices 2 and 5. 
The maximum difference is max(|nums[1] - nums[4]|, |nums[2] - nums[5]|) = max(0, 1) = 1. Therefore, we return 1.

Example 2:

Input: nums = [4,2,1,2], p = 1
Output: 0
Explanation: Let the indices 1 and 3 form a pair. The difference of that pair is |2 - 2| = 0, which is the minimum we can attain.

Constraints:

1 <= nums.length <= 10^5
0 <= nums[i] <= 10^9
0 <= p <= (nums.length)/2

Solution

Think reversely, try to find a boundary, and see how many pairs we have that the distance is below or equals to boundary. If the number is larger than p, then we could shrink the boundary.

Use binary search, left = 0, right = max(nums) - min(nums).

Time complexity: o ( log ⁡ ( max ⁡ ( n u m s ) − min ⁡ ( n u m s ) ∗ n ) o(\log(\max(nums) - \min(nums) * n) o(log(max(nums)min(nums)n)
Space complexity: o ( 1 ) o(1) o(1)文章来源地址https://www.toymoban.com/news/detail-636786.html

Code

class Solution:
    def minimizeMax(self, nums: List[int], p: int) -> int:
        nums.sort()
        left, right = 0, nums[-1] - nums[0]
        while left < right:
            mid = (left + right) >> 1
            # find how many pairs that are smaller than mid
            index = 0
            k = 0
            while index < len(nums) - 1:
                if abs(nums[index] - nums[index + 1]) <= mid:
                    k += 1
                    index += 2
                else:
                    index += 1
            if k >= p:
                right = mid
            else:
                left = mid + 1
        return (left + right) >> 1

到了这里,关于leetcode - 2616. Minimize the Maximum Difference of Pairs的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【异常】The field file exceeds its maximum permitted size of 1048576 bytes.

    本项目是个Springboot 项目,功能是要做一个文件上传,在测试时发现报错,上传的是一个 word 文件,大小是 1.25MB,报错内容如下: Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes. 详细报错内容如下图

    2024年03月15日
    浏览(44)
  • LeetCode //C - 2130. Maximum Twin Sum of a Linked List

    In a linked list of size n, where n is even, the i t h i^{th} i t h node (0-indexed) of the linked list is known as the twin of the ( n − 1 − i ) t h (n-1-i)^{th} ( n − 1 − i ) t h node, if 0 = i = (n / 2) - 1. For example, if n = 4, then node 0 is the twin of node 3, and node 1 is the twin of node 2. These are the only nodes with twins for n = 4. Th

    2024年01月17日
    浏览(65)
  • LeetCode //C - 1161. Maximum Level Sum of a Binary Tree

    Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on. Return the smallest level x such that the sum of all the values of nodes at level x is maximal.   Example 1: Input: root = [1,7,0,7,-8,null,null] Output: 2 **Explanation: ** Level 1 sum = 1. Level 2 sum = 7 + 0 = 7. Level 3 sum = 7 + -8 = -1. So we return

    2024年01月23日
    浏览(53)
  • The maximum length of cell contents (text) is 32,767 charactersExcel导出单元格长度超长

    一、问题描述 导出excel接口报错,错误信息如下: ava.lang.IllegalArgumentException: The maximum length of cell contents (text) is 32767 characters at org.apache.poi.ss.usermodel.CellBase.checkLength(CellBase.java:309) at org.apache.poi.ss.usermodel.CellBase.setCellValue(CellBase.java:290) 二、定位问题 从错误信息查看源码,定位

    2024年02月16日
    浏览(40)
  • leetcode - 1751. Maximum Number of Events That Can Be Attended II

    You are given an array of events where events[i] = [startDayi, endDayi, valuei]. The ith event starts at startDayi and ends at endDayi, and if you attend this event, you will receive a value of valuei. You are also given an integer k which represents the maximum number of events you can attend. You can only attend one event at a time. If you choose to attend

    2024年02月16日
    浏览(78)
  • java.net.ConnectException: [NACOS HTTP-POST] The maximum number of tolerable server reconnection

    当使用nacos作为注册中心使用的时候,启动项目,正常启动, 但是控制台一直打印报错,报错如下: 出现此错误的原因为在你的项目中,pom.xml文件中使用了spring-cloud-starter-alibaba-nacos-config依赖 第一种方法: 将上述依赖注释掉 第二种方法: 创建一个boostrap.yml的文件 如果经过

    2024年02月11日
    浏览(50)
  • LeetCode 2496. Maximum Value of a String in an Array【字符串,数组】简单

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月11日
    浏览(60)
  • MobaXterm 连接服务器超过指定连接数量(默认14个)Warning: you have reached the maximum number of saved sessions for the

    错误提示: Warning: you have reached the maximum number of saved sessions for the personal edition of MobaXterm. You can start a new session but it wil not be automatically saved. Please support MobaXterm by subscribing to the Professional edition here: https://mobaxterm.mobatek.net 意思就是:警告:您已达到个人版MobaXterm的最大保存会话

    2024年02月14日
    浏览(109)
  • SpringCloud-11-解决[NACOS HTTP-GET] The maximum number of tolerable server reconnection errors has bee

    错误日志显示的是nacos的服务数量已达最大,实际原因是配置中心出问题了。 若仅使用了nacos的发现功能(discovery),则不需要引入配置依赖“spring-cloud-starter-alibaba-nacos-config”,否则将会报错,如下: 解决办法1: 移除config依赖: 解决办法2: bootstrap.yml中将config关闭:

    2024年02月12日
    浏览(45)
  • leetcode 2616. 最小化数对的最大差值

    在数组nums中找到p个数对,使差值绝对值的和最小。 思路: 最小差值应该是数值相近的一对数之间产生,让数值相近的数字尽量靠在一起方便计算,所以需要排序。 这里不去直接考虑一对对的数字,而是直接考虑差值的取值。 用binary search搜索一个差值。 左边界是0,右边界

    2024年02月13日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包