一种Java方法的优化
Arthur和Alena正在玩一个包含一些整数的数组的游戏。Arthur可以取任意整数并将其从数组中移除,他必须将该数字的一半(向上舍入)加回到数组中。 阿莱娜给阿瑟分配了固定数量的移动,并要求他最小化最终阵列的总和。 作为移动工作原理的示例,从数组nums=[10,20,7]开始,执行k=4个移动 选择要在其上执行移动的任何图元,例如7。执行除法:7/2=3.5,并四舍五入到4。用新值4替换7。此时,数组为nums=[10,20,4]。所有4个移动都按如下方式执行:一种Java方法的优化,java,arrays,optimization,Java,Arrays,Optimization,Arthur和Alena正在玩一个包含一些整数的数组的游戏。Arthur可以取任意整数并将其从数组中移除,他必须将该数字的一半(向上舍入)加回到数组中。 阿莱娜给阿瑟分配了固定数量的移动,并要求他最小化最终阵列的总和。 作为移动工作原理的示例,从数组nums=[10,20,7]开始,执行k=4个移动 选择要在其上执行移动的任何图元,例如7。执行除法:7/2=3.5,并四舍五入到4。用新值4替换7。此时,数组为nums=[10,20,4]。所有4个移动都按如下方式执行: Pick Pick/
Pick Pick/2 Rounded Result
---------------------------------------------
Initial array [10, 20, 7]
----------------------------------------------
7 3.5 4 [ 10, 20, 4]
10 5 5 [5, 20, 4]
20 10 10 [5, 10, 4]
10 5 5 [5, 5, 4]
最后一个数组的和是5+5+4=14,这个和是最小的
功能描述
在下面的编辑器中完成函数minSum。函数必须返回一个表示最小和的整数
k步之后的数组的
minSum具有以下参数:
nums[nums[0],...nums[n-1]]: an array of integers
k: an integer
约束条件
1 ≤ n ≤ 105
1 ≤ numi ≤ 104 (where 0 ≤ i < n)
1 ≤ k ≤ 107
样本输出
1
解释
该数组只有一个整数,即2,在该数组上执行的步骤数为1。
在第一步之后,数字2减少到数字1,因此,在一步之后数组的最小和是1
这是昨天对一家公司的配对编码采访:
我的想法是:
TestCase #0: Success
Output:
1
Expected Output:
1
TestCase #1: Success
Output:
4
Expected Output:
4
TestCase #2: Success
TestCase #3: Success
TestCase #4: Success
TestCase #5: Success
TestCase #6: Terminated due to timeout
TestCase #7: Terminated due to timeout
TestCase #8: Terminated due to timeout
TestCase #9: Terminated due to timeout
TestCase #10: Terminated due to timeout
TestCase #11: Terminated due to timeout
/*
*完成下面的“minSum”功能。
*
*该函数应返回一个整数。
*该函数接受以下参数:
* 1. 整数数组数
* 2. 整数k
*/
公共静态int minSum(列表编号,int k)
{
//在这里编写代码
//首先:将为num创建一个arraylist
//做精神检查
如果(num==null | | k您应该尽可能使用标准库函数。它们提供了一个PriorityQueue
类,该类保持良好的(对数添加和删除头操作)复杂性操作。这给了我以下实现:
static int playGame(int turns, List<Integer> numbers) {
PriorityQueue<Integer> pq = new PriorityQueue<>(numbers.size(), (left, right) ->
right - left
);
pq.addAll(numbers);
for (int i = 0; i < turns; i++) {
int next = pq.remove();
pq.add((next + 1) / 2);
}
return pq.stream().mapToInt(Integer::intValue).sum();
}
静态整数游戏(整数回合,列表编号){
PriorityQueue pq=新的PriorityQueue(numbers.size(),(左,右)->
左右
);
pq.addAll(编号);
对于(int i=0;i
如果性能是一个问题,它可能有助于避免实数运算。因此,更喜欢原始数的一半=(convertedArray[length-1]%2==0)?convertedArray[length-1]/2:1+convertedArray[length-1]/2;而且,它感觉像是O(n log n)可以替换为查找最大元素在数组中的位置,即O(n)并处理它。使用heap,复杂性是klog(n)
我知道的最快的“除法取整”是(n+k-1)/k,仅适用于整数数学,如果k是2的幂,则可以优化到位移位。感谢大家的帮助和评论。我从采访中得到的反馈是:“他只是使用蛮力方法,在面临的挑战/优化等问题上缺乏设计经验和基本/通用的回答。”这听起来很糟糕,但我仍然很感激我能得到反馈。然而,在一次配对编码面试中,仅仅在20到30分钟内,如何在不考虑暴力方法的情况下进行表演?这就是我正在思考的如何改进……感谢你们所有人的想法和评论。反馈非常好。IMHO。你做了一次非常暴力的面试找到最小值的方法——实际上只能是你减半的数字,或者是跟随它的数字。重新排序数组每一个循环都是一个巨大的浪费。你也使用了非常基本的工具。数组是一个基础,但通常不是一个解决方案。看看什么更高级的结构,比如<代码> PrimeRealQue>代码>,并学习它们。优势和劣势。这会让你走得更远。
/*
* Complete the 'minSum' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER_ARRAY num
* 2. INTEGER k
*/
public static int minSum(List<Integer> num, int k)
{
// Write your code here
// first of all: will have an arraylist for num
// do the sanity check
if(num==null || k<1)
{
return -1;
}
ArrayList<Integer> arrayList = new ArrayList<>(num);
int[] convertedArray = new int[arrayList.size()];
// convertedArray = arrayList.toArray();
for(int i = 0; i<convertedArray.length; i++)
{
convertedArray[i] = arrayList.get(i);
}
// after the array implementation: sort the array
Arrays.sort(convertedArray); // this will get the sorted array
int length = convertedArray.length;
// for K times of movements
for(int j=1 ; j<=k; j++)
{
int halfOfOrigianalNumber = (int)Math.round((double)convertedArray[length-1]/2);
convertedArray[length-1] = halfOfOrigianalNumber;
Arrays.sort(convertedArray);
}
//to sum up :
int sumUp = 0; //to sum up
for (int i=0; i<convertedArray.length;i++)
{
sumUp+=convertedArray[i];
}
return sumUp;
}
static int playGame(int turns, List<Integer> numbers) {
PriorityQueue<Integer> pq = new PriorityQueue<>(numbers.size(), (left, right) ->
right - left
);
pq.addAll(numbers);
for (int i = 0; i < turns; i++) {
int next = pq.remove();
pq.add((next + 1) / 2);
}
return pq.stream().mapToInt(Integer::intValue).sum();
}