首页 > 其他分享 >Make Lexicographically Smallest Array by Swapping Elements

Make Lexicographically Smallest Array by Swapping Elements

时间:2023-11-28 09:22:18浏览次数:40  
标签:Elements Swapping nums int text Make limit array lexicographically

Make Lexicographically Smallest Array by Swapping Elements

You are given a 0-indexed array of positive integers nums and a positive integer limit.

In one operation, you can choose any two indices i and j and swap nums[i] and nums[j] if |nums[i] - nums[j]| <= limit.

Return the lexicographically smallest array that can be obtained by performing the operation any number of times.

An array a is lexicographically smaller than an array b if in the first position where a and b differ, array a has an element that is less than the corresponding element in b. For example, the array [2,10,3] is lexicographically smaller than the array [10,2,3] because they differ at index 0 and 2 < 10.

 

Example 1:

Input: nums = [1,5,3,9,8], limit = 2
Output: [1,3,5,8,9]
Explanation: Apply the operation 2 times:
- Swap nums[1] with nums[2]. The array becomes [1,3,5,9,8]
- Swap nums[3] with nums[4]. The array becomes [1,3,5,8,9]
We cannot obtain a lexicographically smaller array by applying any more operations.
Note that it may be possible to get the same result by doing different operations.

Example 2:

Input: nums = [1,7,6,18,2,1], limit = 3
Output: [1,6,7,18,1,2]
Explanation: Apply the operation 3 times:
- Swap nums[1] with nums[2]. The array becomes [1,6,7,18,2,1]
- Swap nums[0] with nums[4]. The array becomes [2,6,7,18,1,1]
- Swap nums[0] with nums[5]. The array becomes [1,6,7,18,1,2]
We cannot obtain a lexicographically smaller array by applying any more operations.

Example 3:

Input: nums = [1,7,28,19,10], limit = 3
Output: [1,7,28,19,10]
Explanation: [1,7,28,19,10] is the lexicographically smallest array we can obtain because we cannot apply the operation on any two indices.

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109
  • 1 <= limit <= 109

 

解题思路

  lc 难得爆零。

  看题解都说经典结论题,把序列的每个元素看作是一个点,如果两个点之间可以互换则连一条边。即对于元素 $x$,将 $x$ 与值域在 $[x - \text{limit}, x + \text{limit}]$ 范围内的元素连一条边即可。那么就会形成若干个连通块,每个连通块内的元素可以任意互换,为了让字典序最小只需让每个连通块内的元素递增即可。

  剩下的问题就是如何连边,先对数组进行排序,容易想到枚举每个元素,与右边不超过 $x + \text{limit}$ 的元素都连一条边,但最糟糕的情况下时间复杂度会达到 $O(n^2)$。实际上我们只需考虑右边与它相邻的元素即可,即如果 $a_{i+1} - a_i \leq \text{limit}$,那么 $a_i$ 与 $a_{i+1}$ 连边。如果 $a_j - a_i \leq \text{limit}$,那么必然有 $a_j - a_{i+1} \leq \text{limit}$,并且因为连通性 $a_i$ 必然会与 $a_j$ 在同一个连通块。

  实际上上面的做法等价于将序列分成连续的若干段,每一段内的元素都有 $a_{i+1} - a_i \leq \text{limit}$。我们只需找出这样的每一段即可。

  AC 代码如下,时间复杂度为 $O(n \log{n})$:

class Solution {
public:
    vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {
        int n = nums.size();
        vector<int> p(n);
        iota(p.begin(), p.end(), 0);
        sort(p.begin(), p.end(), [&](int i, int j) {
            return nums[i] < nums[j];
        });
        vector<int> ans(n);
        for (int i = 0; i < n; i++) {
            int j = i;
            set<int> st({p[i]});
            while (i + 1 < n && nums[p[i + 1]] - nums[p[i]] <= limit) {
                st.insert(p[++i]);
            }
            auto it = st.begin();
            for (int k = j; k <= i; k++, it++) {
                ans[*it] = nums[p[k]];
            }
        }
        return ans;
    }
};

 

参考资料

  第 373 场力扣周赛:https://leetcode.cn/circle/discuss/ip8jWQ/view/5mBpGb/

  赛事活动丨[第373 场周赛]式酱的解题报告:https://leetcode.cn/circle/discuss/k0kT9w/

标签:Elements,Swapping,nums,int,text,Make,limit,array,lexicographically
From: https://www.cnblogs.com/onlyblues/p/17860803.html

相关文章

  • Makefile - Error: Makefile:2: *** missing separator. Stop.
    Gotbelowerror:Makefile:2:***missingseparator. Stop. ChecktheMakefileusingcat-e-t-v:zzh@ZZHPC:/zdata/Github/zimplebank$cat-e-t-vMakefilecreatedb:$dockerexec-itpostgres16createdb--username=root--owner=rootzimple_bank$$......
  • Makefile中空格与tab
    空格与tabmakefile实际上是在一个文件中用两种完全不同的“语言”编写的。recipe(运行编译器,echo等的命令)是用shell脚本语法编写的。不在recipe中的其余makefile是用makefile语法编写的。为了使make能够区分recipe和不是recipe的东西,它使用了TAB字符。因此,以TAB开头的行......
  • SAP Fiori Elements List Report 应用里 Header 字段的绑定路径
    在ODataMetaModel.bindProperty方法里设置断点:观察到绑定路径:/dataServices/schema/0/entityType/6/com.sap.vocabularies.UI.v1.HeaderInfo在SAPUI5开发中,OData服务是一种常见的数据源。它采用统一的接口和数据模型,使得前端应用可以与后端系统进行交互。在OData服务的......
  • emscripten cmake 简单尝试
    emscripten提供了比较完整的工具链,包含了对于make以及cmake等工具的支持,以下是一个简单的c代码转换为wasm的demo同时基于cmake进行项目管理参考项目项目结构├──CMakeLists.txt├──README.md├──app.js└──src├──add.c......
  • PlayMaker 扩展额外创建更多的脚本
    自定义设置Image扩展usingSystem;usingUnityEngine.UI;usingUnityEngine;namespaceHutongGames.PlayMaker.Actions{[ActionCategory(ActionCategory.UI)][Tooltip("设置图片")]publicclassSetSImage:FsmStateAction{[RequiredFie......
  • make 笔记
    (图一)上图为单独编译单个模块的Makefile模版,38行的CLASS_DIR中包含编译各模块所需的共同依赖文件,路径下会包含一个编译这些依赖文件的Makefile;56行的$(AT) 就是符号@,Makefile中@用于控制其后字符串的显示与否;如果没有$(AT)时,rm前面是否有注释符#都会使rm这条命令的......
  • makeblock
    importrandomimportpygamePANEL_width=1080PANEL_highly=720FONT_PX=15makeblock=pygame.image.load('logo.png')pygame.init()#创建一个可视窗口#CreateavisualwindowwinSur=pygame.display.set_mode((PANEL_width,PANEL_highly))font=......
  • 快速入门CMake
    一、CMake简介​ 使用简单方便,可以跨平台,构建项目编译环境。尤其比直接写Makefile简单(在构建大型工程编译时,需要写大量的文件依赖关系),可以通过简单的CMake生成负责的Makefile文件。二、CMake安装​ ubuntu上直接执行sudoaptinstallcmake安装完成,可以通过cmake-version查......
  • 解决python运行报错Hint: make sure your test modules/packages have valid Python n
    解决方案:在pycharm中的Terminal中运行:pip3install-ihttps://pypi.tuna.tsinghua.edu.cn/simple-rrequirements.txt问题解决优秀不够,你是否无可替代欢迎关注我的微信公众号:软件测试君......
  • Linux-Makefile与make命令
    Makefile命令 makefile文件和make工具的作用make它能够通过查找文件中记录的被修改过的文件根据依赖关系对这些文件来单独编译,达到快速编译多个文件的过程。Make的执行过程当控制台终端执行make命令以后,它就会去寻找Makefile文件并执行文件中的第一个目标的命令。例子中......