Array a consists of distinct, positive integers and serves as a work array. Array rotate contains a list of integers that define operations on the work array. For each element of the rotate array, that number of left circular rotations is applied to the work array. In one left circular rotation, the element at index 0 is moved to the highest index of the array, and all the other elements shift left by 1 index.
Given an array of distinct positive integers, perform each rotation in the second array and determine the index of the highest value element within the array.
Example
a = [1, 2, 3]
rotate = [1, 2, 3, 4]
The resulting arrays are:
a = [1, 2, 3],rotate[0] = 1->a = [2, 3, 1]and the maximum element is at index1. Setindices[0] = 1.a = [1, 2, 3],rotate[1] = 2->a = [3, 1, 2]andindices[1] = 0.a = [1, 2, 3],rotate[2] = 3->a = [1, 2, 3]andindices[2] = 2.a = [1, 2, 3],rotate[3] = 4->a = [2, 3, 1]andindices[3] = 1.
Return the new array made of the indices of the maximal elements, indices = [1, 0, 2, 1].
Note: Each rotation is performed on the original array formation.
Function Description
Complete the function getMaxElementIndexes in the editor.
getMaxElementIndexes has the following parameter(s):
int a[n]: an array ofnintegers to rotateint rotate[m]: the rotations to perform
Returns
int[m]: each elementiis the index of the maximal element inaafterrotate[i]rotations
这道题要求对同一个数组 a 按 rotate 中给出的每个次数分别做左循环旋转,并返回每次旋转后数组最大值所在的下标。因为 a 中元素互不相同,最大值的位置可以先在原数组中确定,再结合左旋 k 次后下标如何平移来计算结果:若原始最大值在位置 pos,长度为 n,则旋转 k 次后它的新下标是 (pos – k % n + n) % n。样例 a = [1,2,3]、rotate = [1,2,3,4] 的输出为 [1,0,2,1]。本题核心是利用取模和数组下标映射,避免每次真的旋转数组,从而把时间复杂度降到 O(n + m)。