Problem Statement
A permutation A[0], A[1], ..., A[N-1] is a sequence containing each integer between 0 and N-1, inclusive, exactly once. Each permutation A of length N has a corresponding child array B of the same length, where B is defined as follows:
B[0] = 0
B[i] = A[B[i-1]], for every i between 1 and N-1, inclusive.
A permutation is considered perfect if its child array is also a permutation. Below are given all permutations for N=3 with their child arrays. Note that for two of these permutations ({1, 2, 0} and {2, 0, 1}) the child array is also a permutation, so these two permutations are perfect.
Permutation Child array
{0, 1, 2} {0, 0, 0}
{0, 2, 1} {0, 0, 0}
{1, 0, 2} {0, 1, 0}
{1, 2, 0} {0, 1, 2}
{2, 0, 1} {0, 2, 1}
{2, 1, 0} {0, 2, 0}
You are given a int[] P containing a permutation of length N. Find a perfect permutation Q of the same length such that the difference between P and Q is as small as possible. The difference between P and Q is the number of indices i for which P[i] and Q[i] are different. If there are several such permutations Q, return the one among them that has the lexicographically smallest child array.
Definition
????
Class:
PerfectPermutationHard
Method:
reorder
Parameters:
int[]
Returns:
int[]
Method signature:
int[] reorder(int[] P)
(be sure your method is public)
Notes
-
int[] A comes before int[] B (with the same length) lexicographically if A has a smaller integer at the first position where the arrays differ.
Constraints
-
P will contain between 1 and 50 elements, inclusive.
-
P will contain each integer between 0 and N-1, inclusive, exactly once, where N is the number of elements in P.
Examples
0)
{2, 0, 1}
Returns: {2, 0, 1 }
This permutation is already perfect.
1)
{4, 0, 5, 2, 1, 3}
Returns: {2, 0, 5, 4, 1, 3 }
Here the smallest possible difference between P and Q is 2. There are 9 possible choices for Q: {2,0,5,4,1,3}, {3,0,5,2,1,4}, {4,0,1,2,5,3}, {4,0,5,1,2,3}, {4,0,5,2,3,1}, {4,2,5,0,1,3}, {4,3,5,2,1,0}, {4,5,0,2,1,3} and {5,0,4,2,1,3}. Among them, {2,0,5,4,1,3} has the lexicographically smallest child array (this array is {0,2,5,3,4,1}).
2)
{2, 7, 3, 0, 6, 4, 5, 1}
Returns: {1, 7, 3, 0, 6, 2, 5, 4 }
3)
{11, 8, 10, 1, 5, 4, 0, 7, 3, 9, 12, 6, 2}
Returns: {1, 8, 10, 2, 5, 7, 0, 9, 3, 11, 12, 6, 4 }
4)
{0, 1, 4, 2, 3, 5}
Returns: {1, 2, 4, 5, 3, 0 }
5)
{0, 2, 6, 5, 7, 3, 1, 4}
Returns: {1, 2, 6, 5, 7, 4, 3, 0 }
英语太菜了,看不懂!
分享到:
相关推荐
为了提供一篇超过1000字的IT知识文章,我们需要更详尽的信息,例如应用的功能实现、开发过程、使用的技术栈、遇到的问题及解决方案、大赛的评审标准和结果,或者该应用对Android开发社区的影响等。如果能提供这些...
在学习外语的过程中,我们经常会遇到各种挑战和误区。这篇源自英国每日电讯报的文章,发布于2012年12月20日,探讨了五个最常见的错误,并提供了改进策略。以下是这五个错误的详细解释: 1. **忽视听力训练**: ...
4-网易有道-有道智能硬件的高效测试探索-刘哲.pdf
《Linux C/C++ 一码农有道教程》是一门专为初学者设计的课程,通过系统性地讲解Linux操作系统和C/C++编程语言的基础知识和应用技巧,帮助学员快速掌握开发Linux应用程序的能力。课程包括理论与实践相结合的教学方式...
标题 "2010年有道难题资格赛试题3套" 暗示了这是一个包含编程竞赛或逻辑思维挑战的资源包,特别是来自2010年网易有道的资格赛题目。有道是中国知名的在线学习平台,尤其以其编程挑战和逻辑测试闻名。这些试题可能...
有道英文写作批改工具 您可以在任意网页上,通过有道写作-浏览器扩展解析各类输入框、文本框中英文内容的拼写、语法、样式、词级润色等不足,并根据智能建议进行修改,实现完美写作。 1、智能识别100多种错误类型,...
有道翻译是一款非常实用的在线翻译工具,其CRX插件是专为浏览器设计的扩展程序,便于用户在浏览网页时快速进行文字翻译。这款插件支持中文(简体),为中国用户提供了极大的便利。它的主要功能包括全屏翻译和内容...
从提供的文档内容中,我们能够提取以下与有道公司招股说明书相关的知识点: 1. 招股说明书的基本信息: - 有道公司的名称及其英文翻译(如果适用)。 - 注册公司所在的国家或地区为“中华人民共和国”。 - 公司...
【互联网保险概述】 互联网保险是将信息技术应用于保险行业,通过线上平台进行保险产品销售、风险管理和服务的一种新型模式。它利用大数据、云计算、人工智能等技术,提高保险服务的效率,优化客户体验,降低运营...
在IT行业中,Linux系统是许多企业和开发者日常工作中不可或缺的一部分,特别是在服务器管理和运维方面。本篇文章将深入探讨在面试中可能遇到的Linux相关知识点,并结合描述中的内容进行详细讲解。...
接口测试是软件测试中至关重要的一环,主要用于检查系统中各个组件间的交互,确保数据传输的正确性和稳定性。... 1. GET与POST的区别: - GET是一种查询请求,用于获取资源,其参数可见于URL,安全性较低,且受到URL...
《融资有道-中小企业融资操作技巧》.ppt
在IT行业中,测试工程师扮演着至关重要的角色,他们确保产品的质量与稳定性。下面将详细解析题目中的关键知识点: 1. **项目工作流程**: 项目通常从需求分析开始,产品经理提供PRD(需求文档),然后团队进行需求...
考试篇目-7(有道文档翻译-英译中结果).pages
manual(有道文档翻译-英译中结果).pdf
在Fedora21上安装有道词典youdao-dict on Fedora21.docx