Javascript 协调性训练:找到最大数量的时钟,其指针在旋转时看起来相同
以下是问题的链接: 问题是我不能得到100分(只有42分)。运行时间是可以的,但是对于一些测试用例,代码给出了错误的答案,但是我不知道问题出在哪里。 有人能帮我吗 这是我的密码:Javascript 协调性训练:找到最大数量的时钟,其指针在旋转时看起来相同,javascript,Javascript,以下是问题的链接: 问题是我不能得到100分(只有42分)。运行时间是可以的,但是对于一些测试用例,代码给出了错误的答案,但是我不知道问题出在哪里。 有人能帮我吗 这是我的密码: function rotate(arr) { var min = arr.reduce(function(a,b) { return a > b ? b : a }); while (arr[0] != min) { var first = arr.shift();
function rotate(arr) {
var min = arr.reduce(function(a,b) { return a > b ? b : a });
while (arr[0] != min) {
var first = arr.shift();
arr.push(first);
}
}
function solution(A, P) {
var positions = [];
A.forEach(function(clock) {
var position = [];
clock.sort(function(a, b) { return a - b });
clock.push(clock[0] + P);
// calculating the distances between clock hands
clock.forEach(function(hand, idx) {
if (idx == 0) return;
position.push(clock[idx] - clock[idx - 1]);
});
// rotating the distances array to start with the minimum element
rotate(position);
positions.push(position);
});
//lexicographically sort positions array to similar types be consecutive
positions.sort();
var sum = 0;
// create a string to compare types with each other
var type = positions[0].join(",");
var n = 0;
// counting consecutive positions with same type
positions.forEach(function(position, idx) {
if (type == position.join(",")) {
n++;
} else {
type = position.join(",");
sum += (n * (n-1)) / 2;
n = 1;
}
});
sum += (n * (n-1)) / 2;
return sum;
}
我认为问题的关键在于“时钟”这个词,它让我太长时间只想着两只手:) “时钟”之间的相似性似乎可以通过“手”的分离顺序来表示; 在问题的例子中,差异是1,2,1,1,2。然而,这个非常简单的案例很好地避免了主要的问题领域 缠绕:例如,在正常时钟上,指针位于4,6和11,1的距离均为2 多指针:例如,一个有8个指针的四指针钟的指针可能位于1,2,5,6和1,4,5,8 给出1,3,1或3,1,3的间隔,但它们在旋转上是相同的 考虑到有大量指针的时钟,可以想象指针之间的间隔序列不能简单地匹配或排序 因此,我们测量手之间的所有间距-上面的4手示例是1,3,1,3和3,1,3,1(为此,我只需在数组末尾添加第一个元素),然后尝试将其与前面的模式匹配。我们只保留唯一的模式以及每个模式的计数 模式匹配尝试比较数组,然后将数组旋转一个元素并重试(这会占用很多时间!) 最后,我们只对每个计数的组合求和 当前代码的得分为90分,仅在几个测试中由于超时而失败。我相信对Javascript掌握得更好的人可以将其缩短100毫秒 以下是输出: 下面是代码:
// compare 2 arrays - assumes they are the same length
function compareArrays( a1, a2 )
{
for( var i=0; i<a1.length; i++)
if( a1[i] != a2[i] ){
return false;
}
return true;
}
// compare newpos[] with positions[][]
// - rotates newpos[] to attempt match
// returns: index of match or -1 if no match
//
function comparePositions(positions,newpos)
{
for(var ipos=0; ipos<positions.length; ipos++){
for( i=0; i<newpos.length; i++){
if( compareArrays(positions[ipos],newpos))
return ipos;
newpos.push(newpos.shift()); //rotate array
}
}
return -1;
}
function solution(A, P) {
var idx,diff,halfP=P/2;
var highestCount=0;
var highestIndex=0;
var positions = [];
var counts=[];
A.forEach(function(clock) {
var position = [];
// sort 'hands' in ascending order
clock.sort(function(a, b) { return a - b });
// duplicate start point on end
clock.push(clock[0]);
// create array of distances between hands, wrapping around clock
for(idx=1; idx<clock.length;idx++){
diff= Math.abs(clock[idx] - clock[idx-1]);
position.push((diff>halfP)?P-diff:diff);
}
idx= comparePositions(positions,position);
if( idx < 0 ){
positions.push(position); // new pattern
counts.push(1);
}else{
counts[idx]++; // count old pattern
}
});
// sum the combinations of counts for each position type
var sum=0;
for(idx=0; idx<counts.length; idx++){
count=counts[idx];
sum+= (count > 2) ? (count * (count-1))/2 : count-1;
}
return sum;
}
//比较两个数组-假设它们的长度相同
功能比较装置(a1、a2)
{
对于(var i=0;i我已经找到了问题所在。它位于旋转
函数中
我假设通过旋转时钟指针之间的距离列表,直到列表头是最小元素,可以将相同的指针位置转换为相同的列表,但事实并非如此
如果手部距离列表中有多个最小元素,则“旋转”功能可以为相同的手部位置生成不同的列表
例如:[4,1,3,2,1]和[2,1,4,1,3]是相同的,因为它们可以相互旋转,但是rotate
会导致第一个为[1,3,2,1,4],而[1,4,1,3,2]第二个问题。我的答案与TonyWilk的答案相似,但就像OP一样,我旋转所有时钟以找到一个可以与其他时钟进行比较的标准位置
标准位置是所有手部位置之和最小的位置(即所有手部尽可能接近1)
我花了相当多的时间试图找到一个数字函数,该函数允许仅根据手的位置值生成唯一的签名
虽然这在数学上是可能的(使用一个递增函数定义一个密集的整数集),但计算时间和/或浮点精度总是会造成阻碍
我恢复到基本数组排序并加入以生成唯一的时钟签名。
这使我在一次超时()的情况下达到了95%
然后,我又花了一点时间优化最后一次超时,直到我注意到一些奇怪的事情:
在两次暂停的情况下,得分只有85%,但如果你看一下计时,它实际上比我之前95%的得分记录要快
我怀疑这一次的计时有点不稳定,或者是根据算法的预期顺序进行了调整。
严格地说,由于签名计算,我的是o(N*M2),即使你需要数千只指针的时钟才能注意到它。
由于有许多手可以放入内存,数组排序占主导地位,实际顺序是o(N*M*log2(M))
这是最后一个版本,试图优化成对计数,从而降低代码的可读性:
function solution (Clocks, Positions)
{
// get dimensions
var num_clocks = Clocks.length;
var num_hands = Clocks[0].length;
// collect canonical signatures
var signatures = [];
var pairs = 0;
for (var c = 0 ; c != num_clocks ; c++)
{
var s_min = 1e100, o_min;
var clock = Clocks[c];
for (var i = 0 ; i != num_hands ; i++)
{
// signature of positions with current hand rotated to 0
var offset = Positions - clock[i];
var signature = 0;
for (var j = 0 ; j != num_hands ; j++)
{
signature += (clock[j] + offset) % Positions;
}
// retain position with minimal signature
if (signature < s_min)
{
s_min = signature;
o_min = offset;
}
}
// generate clock canonical signature
for (i = 0 ; i != num_hands ; i++)
{
clock[i] = (clock[i] + o_min) % Positions;
}
var sig = clock.sort().join();
// count more pairs if the canonical form already exists
pairs += signatures[sig] = 1 + (signatures[sig]||0);
}
return pairs - num_clocks; // "pairs" includes singleton pairs
}
功能解决方案(时钟、位置)
{
//获取维度
var num_clocks=clocks.length;
var num_hands=时钟[0]。长度;
//收集规范签名
var签名=[];
var对=0;
对于(var c=0;c!=num_clocks;c++)
{
var s_min=1e100,o_min;
var时钟=时钟[c];
对于(var i=0;i!=num_hands;i++)
{
//当前手旋转到0时的位置签名
var偏移=位置-时钟[i];
var签名=0;
对于(var j=0;j!=num_hands;j++)
{
签名+=(时钟[j]+偏移量)%位置;
}
//以最少的签名保留职位
如果(签名
基本上相同的解决方案在普通C中得到了我:
#包括
静态int比较_int(const void*pa,const void*pb){return*(int*)pa-*(int*)pb;}
静态整数比较时钟;
静态整数比较_时钟(常数无效*pa,常数无效*pb)
{
int i;
常数int*a=*(常数int**)pa;
常数int*b=*(常数int**)pb;
对于(i=0;i!=比较时钟\u M;i++)
{
如果(a[i]!=b[i
#include <stdlib.h>
static int compare_ints (const void * pa, const void * pb) { return *(int*)pa - *(int *)pb ; }
static int compare_clocks_M;
static int compare_clocks (const void * pa, const void * pb)
{
int i;
const int * a = *(const int **)pa;
const int * b = *(const int **)pb;
for (i = 0 ; i != compare_clocks_M ; i++)
{
if (a[i] != b[i]) return a[i] - b[i];
}
return 0;
}
int solution(int **clocks, int num_clocks, int num_hands, int positions)
{
int c;
int pairs = 0; // the result
int repeat = 0; // clock signature repetition counter
// put all clocks in canonical position
for (c = 0 ; c != num_clocks ; c++)
{
int i;
unsigned s_min = (unsigned)-1, o_min=-1;
int * clock = clocks[c];
for (i = 0 ; i != num_hands ; i++)
{
// signature of positions with current hand rotated to 0
int j;
unsigned offset = positions - clock[i];
unsigned signature = 0;
for (j = 0 ; j != num_hands ; j++)
{
signature += (clock[j] + offset) % positions;
}
// retain position with minimal signature
if (signature < s_min)
{
s_min = signature;
o_min = offset;
}
}
// put clock in its canonical position
for (i = 0 ; i != num_hands ; i++)
{
clock[i] = (clock[i] + o_min) % positions;
}
qsort (clock, num_hands, sizeof(*clock), compare_ints);
}
// sort clocks
compare_clocks_M = num_hands;
qsort (clocks, num_clocks, sizeof(*clocks), compare_clocks);
// count duplicates
repeat = 0;
for (c = 1 ; c != num_clocks ; c++)
{
if (!compare_clocks (&clocks[c-1], &clocks[c]))
{
pairs += ++repeat;
}
else repeat = 0;
}
return pairs;
}