当前位置: 首页 > news >正文

LeetCode 2161. Partition Array According to Given Pivot(2025/3/3每日一题)

就很奇怪,最近的每日一题怎么都是序列题。

标题:2161. Partition Array According to Given Pivot

题目:You are given a 0-indexed integer array nums and an integer pivot. Rearrange nums such that the following conditions are satisfied:

  • Every element less than pivot appears before every element greater than pivot.
  • Every element equal to pivot appears in between the elements less than and greater than pivot.
  • The relative order of the elements less than pivot and the elements greater than pivot is maintained.
    • More formally, consider every pipj where pi is the new position of the ith element and pj is the new position of the jth element. If i < j and both elements are smaller (or larger) than pivot, then pi < pj.

Return nums after the rearrangement.

例:

Example 1:

Input: nums = [9,12,5,10,14,3,10], pivot = 10
Output: [9,5,3,10,10,12,14]
Explanation: 
The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array.
The elements 12 and 14 are greater than the pivot so they are on the right side of the array.
The relative ordering of the elements less than and greater than pivot is also maintained. [9, 5, 3] and [12, 14] are the respective orderings.

Example 2:

Input: nums = [-3,4,3,2], pivot = 2
Output: [-3,2,4,3]
Explanation: 
The element -3 is less than the pivot so it is on the left side of the array.
The elements 4 and 3 are greater than the pivot so they are on the right side of the array.
The relative ordering of the elements less than and greater than pivot is also maintained. [-3] and [4, 3] are the respective orderings.

Constraints:

  • 1 <= nums.length <= 105
  • -106 <= nums[i] <= 106
  • pivot equals to an element of nums.

解题思路:这题用O(N)存储空间的解题方法不难。声明一个与给定数组同等大小的数组,用两个指针一个指向数组最前端,用于填充小于pivot的元素,一个指向数组最后端,用于填充大于pivot的元素。最后两个指针中间的部分就是等于pivot的元素。这里有个小trick:在筛选小于pivot元素时,由于需要保持相对位置不变,需要正向遍历整个数组;筛选大于pivot元素时,也需要保持相对位置不变,需要反向遍历整个数组。加起来是需要遍历两次。可以用正向索引i,计算出反向索引(size-1-i),正向反向同时遍历,可以只遍历一次,时间复杂度减小一半。代码如下:

class Solution {
public:
    vector<int> pivotArray(vector<int>& nums, int pivot) {
        vector<int> ans(nums.size(), 0);
        int start = 0, end = nums.size()-1;
        for(int i = 0; i < nums.size(); i++){
            if (nums[i] < pivot) ans[start++] = nums[i];
            if (nums[nums.size()-1-i] > pivot) ans[end--] = nums[nums.size()-1-i];
        }
        while(start <= end) ans[start++] = pivot;
        return ans;
    }
};

时间复杂度O(N)。空间复杂度O(N).

还有in-place解法,等有空更新

相关文章:

  • flink分布式事务 - 两阶段提交
  • 大模型微调
  • 从零到一:快速上手 Poetry——Python 项目管理的利器
  • 高频 SQL 50 题(基础版)_196. 删除重复的电子邮箱
  • 0304作业
  • 深入探索像ChatGPT这样的大语言模型-03-POST-Training:Reinforcement Learning
  • 《几何原本》命题I.9
  • Swagger详细使用介绍
  • Python第十三课:数据库交互 | 信息帝国的基石
  • 慕慕手记项目日记2025.3.3
  • 计划和展示(5条)DeepSeek提示词
  • 小米手机如何录制屏幕?手机、电脑屏幕录制方法分享
  • PMP项目管理—资源管理篇—6.控制资源
  • 课程2. 机器学习方法论
  • 【折线图 Line】——9
  • 阿里通义万相2.1模型在亚马逊云科技ECS容器中的私有化部署
  • Lumerical INTERCONNECT 中的自相位调制 (SPM)
  • 从零开始用react + tailwindcss + express + mongodb实现一个聊天程序(十) 收发消息
  • STM32F103C8T6 标准库(SPL)迁移到 HAL 库指南
  • DeepSeek本地接口调用(Ollama)
  • 大庆免费网站建设/友情链接交易购买
  • 100元建网站/百度软件应用中心下载
  • 深圳设计网站建设公司/seo教程seo官网优化详细方法
  • 网站中的表单怎么做/收录入口在线提交
  • 国内大的做网站的公司/网络seo培训
  • 建网站的目的是什么/品牌推广是做什么的