【每日一题Day82】LC1806还原排列的最少操作步数 | 模拟

简介: 思路:按照题意进行变换,直至数组变为原始数组,记录变换次数

还原排列的最少操作步数【LC1806】


You are given an even integer n. You initially have a permutation perm of size n where perm[i] == i (0-indexed).


In one operation, you will create a new array arr, and for each i:


  • If i % 2 == 0, then arr[i] = perm[i / 2].
  • If i % 2 == 1, then arr[i] = perm[n / 2 + (i - 1) / 2].


You will then assign arr to perm.


Return the minimum non-zero number of operations you need to perform on perm to return the permutation to its initial value.


数学就算了 不要为难自己


  • 思路:按照题意进行变换,直至数组变为原始数组,记录变换次数


  • 实现


class Solution {
    public int reinitializePermutation(int n) {
        int[] perm = new int[n];
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) perm[i] = i;
        for (int i = 0; i < n; i++){
            if (i % 2 == 0){
                arr[i] = perm[i / 2];
            }else{
                arr[i] = perm[n / 2 + (i - 1) / 2];
            }
        }
        int count = 1;
        while (!Arrays.equals(perm, arr)){
            int[] tmp = new int[n];
            for (int i = 0; i < n; i++){
                if (i % 2 == 0){
                    tmp[i] = arr[i / 2];
                }else{
                    tmp[i] = arr[n / 2 + (i - 1) / 2];
                }
            }
            for (int i = 0; i < n; i++) arr[i] = tmp[i];
            count++;
        }
        return count;
    }
}


。复杂度


  • 时间复杂度:O ( n 2 )
  • 空间复杂度:O ( n )


  • 好看的代码


class Solution {
    public int reinitializePermutation(int n) {
        int[] prem = new int[n], arr = new int[n];
        for (int i = 0; i < n; i++) prem[i] = i;
        int i, step = 1;
        while (true) {
            for (i = 0; i < n; i++) arr[i] = i % 2 == 0 ? prem[i / 2] : prem[(n - 1 + i) / 2];
            for (i = 0; i < n && arr[i] == i; i++); 
            if (i == n) return step;
            for (i = 0; i < n; i++) prem[i] = arr[i];
            step++;
        }
    }
}
作者:Tizzi
链接:https://leetcode.cn/problems/minimum-number-of-operations-to-reinitialize-a-permutation/solutions/2052353/liang-chong-jie-fa-mo-ni-mo-ni-you-hua-b-2ijm/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


目录
相关文章
|
16天前
|
存储 弹性计算 人工智能
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
2025年9月24日,阿里云弹性计算团队多位产品、技术专家及服务器团队技术专家共同在【2025云栖大会】现场带来了《通用计算产品发布与行业实践》的专场论坛,本论坛聚焦弹性计算多款通用算力产品发布。同时,ECS云服务器安全能力、资源售卖模式、计算AI助手等用户体验关键环节也宣布升级,让用云更简单、更智能。海尔三翼鸟云服务负责人刘建锋先生作为特邀嘉宾,莅临现场分享了关于阿里云ECS g9i推动AIoT平台的场景落地实践。
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
|
8天前
|
云安全 人工智能 安全
Dify平台集成阿里云AI安全护栏,构建AI Runtime安全防线
阿里云 AI 安全护栏加入Dify平台,打造可信赖的 AI
|
11天前
|
人工智能 运维 Java
Spring AI Alibaba Admin 开源!以数据为中心的 Agent 开发平台
Spring AI Alibaba Admin 正式发布!一站式实现 Prompt 管理、动态热更新、评测集构建、自动化评估与全链路可观测,助力企业高效构建可信赖的 AI Agent 应用。开源共建,现已上线!
1022 34
|
10天前
|
机器学习/深度学习 人工智能 搜索推荐
万字长文深度解析最新Deep Research技术:前沿架构、核心技术与未来展望
近期发生了什么自 2025 年 2 月 OpenAI 正式发布Deep Research以来,深度研究/深度搜索(Deep Research / Deep Search)正在成为信息检索与知识工作的全新范式:系统以多步推理驱动大规模联网检索、跨源证据。
770 54
|
8天前
|
文字识别 测试技术 开发者
Qwen3-VL新成员 2B、32B来啦!更适合开发者体质
Qwen3-VL家族重磅推出2B与32B双版本,轻量高效与超强推理兼备,一模型通吃多模态与纯文本任务!
661 11