[英]Make array consecutive
我陷入了 codeFights 的挑战中。我的代码通过了简单测试,但在五个隐藏测试中只有 2 个失败了,她是挑战指令:
Ratorg 从 CodeMaster 那里得到了不同大小的雕像作为生日礼物,每个雕像的大小都是非负的 integer。 因为他喜欢把事情做得完美,所以他想把它们从小到大排列,这样每个雕像都会比前一个大 1。他可能需要一些额外的雕像才能完成。 帮助他算出所需的额外雕像的最少数量。
Example
For statues = [6, 2, 3, 8], the output should be
makeArrayConsecutive2(statues) = 3.
Ratiorg needs statues of sizes 4, 5 and 7.
Input/Output
[time limit] 4000ms (js)
[input] array.integer statues
An array of distinct non-negative integers.
Constraints:
1 ≤ statues.length ≤ 10,
0 ≤ statues[i] ≤ 20.
[output] integer
需要添加到现有雕像的最少数量的雕像,这样它包含间隔 [L, R](对于某些 L, R)中的每个 integer 大小并且没有其他大小。
这是我的代码:
function makeArrayConsecutive2(statues) {
//range the table from min to max
var rang=statues.sort();
var some=0;
//if the table is one element
if(rang.length-1==0){
return 0;
}else{
//if the table contain more then one element
for(i=0;i<=rang.length-2;i++){
//add the deference of two consecutive position -1
//to find the number of missing numbers
some+=(rang[i+1]-rang[i]-1);
}
return some;
}
}
一切都是正确的,除了排序部分。
您已使用 sort 函数按递增顺序对数组进行排序
var rang = statues.sort();
但是如果 sort 函数没有提供比较函数,它会将其元素转换为字符串,然后按 unicode 顺序对其进行排序。
例如: [2,1,11] 将被排序为 [1,11,2] 这将产生不需要的输出。
正确的做法是
var rang = statues.sort(function (a, b){
return (a - b)
});
所以解决这个问题的逻辑是:
查找数组中的最小和最大元素。
得到可以说,数组的最大值和最小值的差的计数,以便计算,必须有多少个元素才能使其成为连续数组。 像从 5 到 9,总元素的计数必须是 5(即 5,6,7,8,9),并且还要在结果上加 1以使 count inclusive 。
求数组的长度
用数组的长度减去计数,即“最大值和最小值的差”
蟒蛇代码(解释):
def makeArrayConsecutive2(statues):
max_height = max(statues)
min_height = min(statues)
array_length = len(statues)
count_of_element = max_height - min_height + 1
# '1 ' is added to make it inclusive
return count_of_element-array_length
和 Python 一个班轮:
def makeArrayConsecutive2(statues):
return max(statues)-min(statues)-len(statues)+1
不需要排序 (nlogn)。 以下是Java中的解决方案。
int makeArrayConsecutive2(int[] statues) {
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i = 0; i < statues.length; i++) {
max = Math.max(max, statues[i]);
min = Math.min(min, statues[i]);
}
return (max - min) + 1 - statues.length;
}
我同意迪帕克的解决方案。 问题不是关于排序,而是帮助确定所需的额外雕像的最小数量。 您只需要获取最大值和最小值。
int makeArrayConsecutive2(int[] statues)
{
int min=Integer.MAX_VALUE,max=-1;
for(int i=0;i<statues.length;i++)
{
if(statues[i] < min){ min = statues[i]; }
if(statues[i] > max){ max = statues[i]; }
}
return (max-min)+1 - statues.length;
}
打字稿中的解决方案。 使用 for 循环从状态数组的最小值和最大值创建一个新数组。 用状态数组长度减去新数组长度。
function makeArrayConsecutive2(statues: number[]): number {
let min = Math.min(...statues);
let max = Math.max(...statues);
let arr = [];
for (let i = min; i <= max; i++) {
arr.push(i);
}
return arr.length - statues.length;
}
此代码有效
var statues = [2, 3, 6, 8];
var newStatues = [];
函数声明
function makeArrayConsecutive2(statues) {
statues.sort(function(a, b) { return a - b });
for(var i = statues[0]; i <= statues[statues.length-1]; i++) {
newStatues.push(i);
}
return console.log(newStatues.length - statues.length);
}
函数调用
makeArrayConsecutive2(statues);
最佳解决方案仅在 O(1) 复杂度中出现:
let n = statues.length;
let max = Math.max.apply(null, statues);
let min = Math.min.apply(null, statues);
return max - min - n + 1;
function makeArrayConsecutive2(statues) {
var rang = statues.sort(function (a, b){
return (a - b)
});
var some=0;
if(rang.length-1==0){
return 0;
}else{
for(i=0;i<=rang.length-2;i++){
some+=(rang[i+1]-rang[i]-1);
}
return some;
}
}
function makeArrayConsecutive2(statues) {
const n = statues.length;
const min = Math.min(...statues);
const max = Math.max(...statues);
return max - min - n + 1;
}
如果我们从最大元素中减去最小值,那么我们就会得到最终数组中应该包含的元素数。 现在从这个数量中减去已经存在的元素数量并加 1,然后我们得到我们需要的结果 - 缺失元素的数量
只是为了好玩在 C#
static int makeArrayConsecutive2(int[] statues)
{
List<int> ConsecutiveNums = new List<int>();
for(int i = statues.Min(); i != statues.Max() + 1; i++)
ConsecutiveNums.Add(i);
return ConsecutiveNums.Count - statues.Length;
}
function makeArrayConsecutive2(statues) {
return Math.max(...statues) - Math.min(...statues) + 1 -(statues.length)
}
我认为我们不需要在那里循环,这是我的解决方案
function makeArrayConsecutive2(statues) { const nums = []; for (let i = Math.min(...statues); i <= Math.max(...statues); i++) { if (!statues.includes(i)) { nums.push(i); } } return nums.length; } console.log(makeArrayConsecutive2([6, 2, 3, 8]))
您可以尝试通过以下代码使用 for 循环和三元运算
def makeArrayConsecutive2(statues):
count=0
for i in range (min(statues),max(statues)):
count=count+1 if i not in statues else count
return count
function makeArrayConsecutive2(statues) {
s = statues.sort(function(a, b){return a - b});
n = statues.length;
val = 0;
for (let i=0;i<n-1;i++) {
val += (Math.abs(s[i]-s[i+1]))-1;
}
return val;
}
sort(statues.begin(), statues.end());
int count = 0;
for(int i = 1;i<statues.size(); i++){
int diff = statues[i]-statues[i-1];
if(diff>1){
count+=diff-1;
}
}
return count;
解决方案在PHP
function solution($statues) {
return max($statues) - min($statues) - count($statues) + 1;
}
PHP 解决问题。
function solution($statues) {
sort($statues);
$missing = 0;
$lowest = min($statues);
$highest = max($statues);
$numbers = range($lowest, $highest);
foreach($numbers as $number){
if(!in_array($number, $statues)){
$missing++;
}
}
return $missing;
}
这是python中的代码
def solution(statues):
statues.sort()
c = 0
for i in range(len(statues)-1):
if statues[i+1]-statues[i] > 1:
c += statues[i+1]-statues[i] -1
return (c)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.