Mal*_*e85 38 javascript algorithm
我最近在工作中进行了促销编码测试.这是我真正努力的任务之一,并且想知道最好的方法是什么.我使用if和if的负载,而不是最干净的解决方案,但完成了工作.
我被问到的问题是:
将4个数字格式化为24小时(00:00),找到可能的最长(最晚)时间,考虑到最大小时数为23,最大分钟数为59.如果不可能,则返回NOT POSSIBLE.
例如:
6,5,2,0将是20:56
3,9,5,0将是09:53
7,6,3,8是不可能的
必须返回时间或字符串的示例函数看起来像这样,A,B,C,D与上面以逗号分隔的列表不同的数字:
function generate(A, B, C, D) {
// Your code here
}
Run Code Online (Sandbox Code Playgroud)
人们将如何解决这个问题?
Cam*_*vik 12
这是我提出的非暴力解决方案.查看代码中的注释,了解它是如何工作的.如果其中任何一个不清楚我可以帮助澄清.
function generate(A, B, C, D) {
vals = [A, B, C, D];
counts = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
for (i = 0; i < vals.length; i++) {
for (j = vals[i]; j < counts.length; j++) counts[j]++;
}
// counts is now populated with the number of values less than or equal to the index it belongs to
// so counts[2] is the total number of 0's, 1's and 2's
if (counts[2] === 0) return 'NOT POSSIBLE';
// if there are no 0's and 1's, then it must start with 2
mustStartWith2 = counts[1] === 0;
if (mustStartWith2 && counts[3] === 1) return 'NOT POSSIBLE';
// We want a count of the number of free digits that are 5 or less (for the minute digit)
numbersAvailableForMinute = counts[5] - (mustStartWith2 ? 2 : 1);
if (numbersAvailableForMinute === 0) return 'NOT POSSIBLE';
// we now know that it is a valid time
time = [0, 0, 0, 0];
// we also know if it starts with 2
startsWith2 = mustStartWith2 || (numbersAvailableForMinute >= 2 && counts[2] > counts[1]);
// knowing the starting digit, we know the maximum value for each digit
maxs = startsWith2 ? [2, 3, 5, 9] : [1, 9, 5, 9];
for (i = 0; i < maxs.length; i++) {
// find the first occurrence in counts that has the same count as the maximum
time[i] = counts.indexOf(counts[maxs[i]]);
// update counts after the value was removed
for (j = time[i]; j < counts.length; j++) counts[j]--;
}
// create the time
return time[0]+""+time[1]+":"+time[2]+""+time[3];
}
Run Code Online (Sandbox Code Playgroud)
一旦我了解到您可以将问题视为“生成一个小于 24 的数字和一个小于 60 的数字”而不是尝试处理单个数字,这一事实的推理就变得容易多了。
这将遍历集合中的数字对,找到可以从该数字对中得出的最大有效小时,然后找到可以从剩余数字中得出的最大有效分钟。
var generate = function(a, b, c, d) {
var biggest = function(a, b, max) {
// returns largest of 'ab' or 'ba' which is below max, or false.
// I'm sure there's a more concise way to do this, but:
var x = '' + a + b;
var y = '' + b + a;
if (max > x && max > y) {
var tmp = Math.max(x,y);
return (tmp < 10) ? "0"+tmp : tmp;
}
if (max > x) return x;
if (max > y) return y;
return false;
}
var output = false;
var input = [].slice.call(arguments);
for (var i = 0; i < arguments.length; i++) {
for (var j = i + 1; j < arguments.length; j++) {
// for every pair of numbers in the input:
var hour = biggest(input[i], input[j], 24); // What's the biggest valid hour we can make of that pair?
if (hour) {
// do the leftovers make a valid minute?
var tmp = input.slice(); // copy the input
tmp.splice(j, 1);
tmp.splice(i, 1);
var minute = biggest(tmp[0], tmp[1], 60);
if (hour && minute) {
// keep this one if it's bigger than what we had before:
var nval = hour + ':' + minute;
if (!output || nval > output) output = nval;
}
}
}
}
return output || 'NOT POSSIBLE';
}
/* --------------- Start correctness test --------------------- */
var tests = ['0000', '1212', '1234', '2359', '2360','2362','2366', '1415', '1112', '1277', '9999', '0101'];
console.log('---');
for (var i = 0; i < tests.length; i++) {
console.log(
tests[i],
generate.apply(this, tests[i].split(''))
)
}
/* --------------- Start Speed Test --------------------- */
let startTime = Math.floor(Date.now());
let times = 10000; //how many generate call you want?
let timesHolder = times;
while (times--) {
let A = randNum();
let B = randNum();
let C = randNum();
let D = randNum();
generate(A, B, C, D);
if (times == 0) {
let totalTime = Math.floor(Date.now()) - startTime;
let msg = timesHolder + ' Call Finished Within -> ' + totalTime + ' ms <-';
console.log(msg);
// alert(msg);
}
}
function randNum() {
return Math.floor(Math.random() * (9 - 0 + 1)) + 0;
}
/* --------------- END Speed Test --------------------- */Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8506 次 |
| 最近记录: |