首页 > 其他分享 >[LeetCode] 2966. Divide Array Into Arrays With Max Difference

[LeetCode] 2966. Divide Array Into Arrays With Max Difference

时间:2024-02-02 13:12:11浏览次数:20  
标签:2966 Divide nums Arrays 元素 int length 数组 array

You are given an integer array nums of size n and a positive integer k.

Divide the array into one or more arrays of size 3 satisfying the following conditions:

Each element of nums should be in exactly one array.
The difference between any two elements in one array is less than or equal to k.
Return a 2D array containing all the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return any of them.

Example 1:
Input: nums = [1,3,4,8,7,9,3,5,1], k = 2
Output: [[1,1,3],[3,4,5],[7,8,9]]
Explanation: We can divide the array into the following arrays: [1,1,3], [3,4,5] and [7,8,9].
The difference between any two elements in each array is less than or equal to 2.
Note that the order of elements is not important.

Example 2:
Input: nums = [1,3,3,2,7,3], k = 3
Output: []
Explanation: It is not possible to divide the array satisfying all the conditions.

Constraints:
n == nums.length
1 <= n <= 105
n is a multiple of 3.
1 <= nums[i] <= 105
1 <= k <= 105

划分数组并满足最大差限制。

给你一个长度为 n 的整数数组 nums,以及一个正整数 k 。
将这个数组划分为一个或多个长度为 3 的子数组,并满足以下条件:
nums 中的 每个 元素都必须 恰好 存在于某个子数组中。
子数组中 任意 两个元素的差必须小于或等于 k 。
返回一个 二维数组 ,包含所有的子数组。如果不可能满足条件,就返回一个空数组。如果有多个答案,返回 任意一个 即可。

思路

题目只问是否可以划分,并不在意数组元素的是否需要保持不变,所以这里我选择对 input 数组排序。排序过后,可以每次看三个元素 nums[i], nums[i + 1], nums[i + 2]。如果 nums[i + 2] - nums[i] > k,说明当前这一行的最大元素和最小元素的差不满足题意,直接返回一个空的二维数组。如果满足题意,则还是每次看三个元素直到填满最后的二维数组。

复杂度

时间O(nlogn)
空间O(3n) - O(n)

代码

Java实现

class Solution {
    public int[][] divideArray(int[] nums, int k) {
        int[][] res = new int[nums.length / 3][3];
        // corner case
        if (nums == null || nums.length == 0) {
            return new int[0][0];
        }

        // normal case
        Arrays.sort(nums);
        int m = 0;
        for (int i = 2; i < nums.length; i += 3) {
            if (nums[i] - nums[i - 2] <= k) {
                res[m] = new int[] { nums[i - 2], nums[i - 1], nums[i] };
                m++;
            } else {
                return new int[0][0];
            }
        }
        return res;
    }
}

标签:2966,Divide,nums,Arrays,元素,int,length,数组,array
From: https://www.cnblogs.com/cnoodle/p/18003004

相关文章

  • 无涯教程-Scala - 数组(Arrays)
    Scala提供了一种数据结构数组,它存储了相同类型元素的固定大小的顺序集合。声明数组要在程序中使用数组,必须声明一个变量以引用该数组,并且必须指定该变量可以引用的数组的类型。varz:Array[String]=newArray[String](3)orvarz=newArray[String](3)在此,z被声明为可容......
  • HDU2966 In case of failure 题解
    QuestionHDU2966Incaseoffailure给出平面上\(n\)个点坐标,求每个点最近的点的欧几里得距离的平方Solution算是一道K-D树的板子题维度\(K=2\)建立\(K-D\)树,在每一层更新当前最小答案\(now\_ans\),如果在然后继续遍历当前维度下距离\(\le\)的区块随机数据时间复......
  • 15 Friendly Arrays
    FriendlyArrays打表#include<bits/stdc++.h>#defineintlonglongusingnamespacestd;voidsolve(){ intn,m; cin>>n>>m; vector<int>a(n+1); vector<int>b(m+1); for(inti=1;i<=n;i++)cin>>a[i]; for(inti=1;i<......
  • android navigationBarDividerColor 无效
    AndroidnavigationBarDividerColor无效问题解析与解决1.问题背景在开发Android应用程序时,我们经常会使用导航栏(NavigationBar)来提供用户导航和操作的功能。导航栏中的分割线(divider)是一种常见的设计元素,用于分隔不同的导航按钮或操作按钮。在Android中,我们可以使用navigationB......
  • Android navigationBarDividerColor
    实现AndroidnavigationBarDividerColor的步骤流程图flowchartTDA(开始)B(查找navigationBar对象)C(创建dividerDrawable对象)D(设置dividerDrawable为navigationBar的dividerDrawable属性)E(结束)A-->B-->C-->D-->E介绍在Android开发......
  • 方法&Arrays_API总结
    总结方法方法的组成:修饰符+返回值类型+方法名+形参列表+方法体方法签名:方法名+形参列表调用方法:方法有static修饰,调用是:类名.方法名();调用方法使用参数是实际参数(必须是具体的数据)在java里面用static修饰的方法叫做:类方法或者静态方法形参和实参声明......
  • Arrays.asList方法返回对象
    上例子int[]arr={1,2,3};Listlist=Arrays.asList(arr);for(Objectobject:list){System.out.println(object);}可以看到输出的其实是一个对象,并不是1,2,3解决方法Integer[]arr={1,2,3};......
  • Arrays.asList的坑
    2023年12月21日下午16.46分,咪宝马上下班去上海过圣诞节去了,一个人孤单 CTO:谁在项目中使用Arrays.asList、ArrayList.subList,就立马滚蛋! 1.asList用来把数组转成ArrayList,方便。但问题是这个方法生成的ArrayList是Arrays的内部类,这个内部类没有实现抽象类AbstractList的......
  • CF1870B-Friendly-Arrays-题解
    title:CF1870BFriendlyArrays题解date:2023-09-2010:32:12categories:-题解翻译给出长度为\(n\)的序列\(a\)和长度为\(m\)的序列\(b\),选出\(b\)中的任意个数(可以不选),让\(a\)的每个数都或上它们,求\(a_1\oplusa_2\oplus\dots\oplusa_n\)的最大值......
  • Arrays工具类二分查找方法binarySearch方法详解​
    Arrays工具类二分查找方法binarySearch方法详解基础知识该方法的一般形式是publicstatic<T>intbinarySearch(T[]a,Tkey),对于基本类型,都有相应的重载方法,如针对int类型有binarySearch(int[]a,intkey)等。该方法的原理是使用二分查找算法在指定的数组中搜索指定的值。在调......