需要帮助获得最大和最小值之差为 1 的最长和谐数组
Need help in obtaining the longest harmonious array where the difference between max and min values are 1
我修改了以下问题,而不是显示长度。我想实际输出数组。
我们将和谐数组定义为最大值和最小值之差正好为1的数组。
现在,给定一个整数数组,您需要在所有可能的子序列中找出其最长和谐子序列的长度。 (我试图找到实际的数组)
示例 1:
输入:[1,3,2,2,5,2,3,7]
输出:5
解释:最长的和谐子序列是[3,2,2,2,3].
我编写了以下代码,但我推入 vals 的值不正确。我需要帮助找出如何更正它。
const findLHS = (nums) => {
const hash = new Map()
let vals = []
let min = nums[0], max = nums[0]
for (let num of nums) {
//logic for finding min and max in range is flawed
min = min + max === 1 ? Math.min(min, num) : min
max = max - min === 1 ? Math.max(max, num) : max
hash.set(num, (hash.get(num) || 0) + 1)
console.log(max);
}
//logic is flawed below
for (let key of nums) {
// if (hash.has(key + 1) && hash.has(key + 1) <= max) {
// vals.push(hash.get(key + 1))
// }
if (hash.has(key) === min || hash.has(key) === max) {
vals.push(min) || vals.push(max)
}
}
return vals
}
测试用例,以及我认为应该返回的内容:
console.log(findLHS([1, 3, 2, 2, 5, 2, 3, 7]))//5 [3,2,2,2,3]
console.log(findLHS([1, 2, 3, 4])) //2 [1,2]
console.log(findLHS([1, 2, 2, 1])) //4 [1,2,2,1]
这是我的看法。以 [1, 3, 2, 2, 5, 2, 3, 7] 为例...
- 第 1 阶段:创建一个计算值数量的地图。例如,1=>1, 3=>2, 2=>3, 5=>1, 7=>1
- 第 2 阶段:找到两个连续值之间的最大总数。例如,1 出现 1 次,2 出现 3 次,因此在 1 和 2 之间,共有 4 个值。接下来,3 出现了 2 次,但数组中没有 4,因此移至下一个 Map 元素。因此,2 被看到 3 次,3 被看到 2 次,总共有 5 个值。等等
- 阶段 3:最后,遍历原始数组,提取值。现在我们知道对于一个数组值 2,总共有 5 个 2 和 3 的值,通过数组查找 2 和 3。
function findLHS( arr ) {
// Count the number of occurrences of each value in the array...
let count = new Map();
arr.forEach( v => {
count.set( v, ( count.get( v ) || 0 ) + 1 );
} );
// Now, find the largest count of two consecutive numbers.
let maxKey;
let maxCount = -1;
count.forEach( ( val, key ) => {
if ( count.get( key + 1 ) ) {
let total = val + count.get( key + 1 );
if ( maxCount < total ) {
maxKey = key;
maxCount = total;
}
}
} );
// Finally, return the result.
if ( maxCount == -1 ) {
return [];
}
return arr.reduce( ( acc, val) => {
if ( val == maxKey || val == maxKey + 1 ) {
acc.push( val );
}
return acc
}, [] );
}
console.log( findLHS( [ 1, 3, 2, 2, 5, 2, 3, 7] ) );
console.log( findLHS( [ 1, 2, 3, 4] ) );
console.log( findLHS( [ 1, 2, 2, 1 ] ) );
希望这对您有所帮助...
我修改了以下问题,而不是显示长度。我想实际输出数组。
我们将和谐数组定义为最大值和最小值之差正好为1的数组。 现在,给定一个整数数组,您需要在所有可能的子序列中找出其最长和谐子序列的长度。 (我试图找到实际的数组)
示例 1: 输入:[1,3,2,2,5,2,3,7] 输出:5 解释:最长的和谐子序列是[3,2,2,2,3].
我编写了以下代码,但我推入 vals 的值不正确。我需要帮助找出如何更正它。
const findLHS = (nums) => {
const hash = new Map()
let vals = []
let min = nums[0], max = nums[0]
for (let num of nums) {
//logic for finding min and max in range is flawed
min = min + max === 1 ? Math.min(min, num) : min
max = max - min === 1 ? Math.max(max, num) : max
hash.set(num, (hash.get(num) || 0) + 1)
console.log(max);
}
//logic is flawed below
for (let key of nums) {
// if (hash.has(key + 1) && hash.has(key + 1) <= max) {
// vals.push(hash.get(key + 1))
// }
if (hash.has(key) === min || hash.has(key) === max) {
vals.push(min) || vals.push(max)
}
}
return vals
}
测试用例,以及我认为应该返回的内容:
console.log(findLHS([1, 3, 2, 2, 5, 2, 3, 7]))//5 [3,2,2,2,3]
console.log(findLHS([1, 2, 3, 4])) //2 [1,2]
console.log(findLHS([1, 2, 2, 1])) //4 [1,2,2,1]
这是我的看法。以 [1, 3, 2, 2, 5, 2, 3, 7] 为例...
- 第 1 阶段:创建一个计算值数量的地图。例如,1=>1, 3=>2, 2=>3, 5=>1, 7=>1
- 第 2 阶段:找到两个连续值之间的最大总数。例如,1 出现 1 次,2 出现 3 次,因此在 1 和 2 之间,共有 4 个值。接下来,3 出现了 2 次,但数组中没有 4,因此移至下一个 Map 元素。因此,2 被看到 3 次,3 被看到 2 次,总共有 5 个值。等等
- 阶段 3:最后,遍历原始数组,提取值。现在我们知道对于一个数组值 2,总共有 5 个 2 和 3 的值,通过数组查找 2 和 3。
function findLHS( arr ) {
// Count the number of occurrences of each value in the array...
let count = new Map();
arr.forEach( v => {
count.set( v, ( count.get( v ) || 0 ) + 1 );
} );
// Now, find the largest count of two consecutive numbers.
let maxKey;
let maxCount = -1;
count.forEach( ( val, key ) => {
if ( count.get( key + 1 ) ) {
let total = val + count.get( key + 1 );
if ( maxCount < total ) {
maxKey = key;
maxCount = total;
}
}
} );
// Finally, return the result.
if ( maxCount == -1 ) {
return [];
}
return arr.reduce( ( acc, val) => {
if ( val == maxKey || val == maxKey + 1 ) {
acc.push( val );
}
return acc
}, [] );
}
console.log( findLHS( [ 1, 3, 2, 2, 5, 2, 3, 7] ) );
console.log( findLHS( [ 1, 2, 3, 4] ) );
console.log( findLHS( [ 1, 2, 2, 1 ] ) );
希望这对您有所帮助...