##Just a few if statements will do.
Just a few if statements will do.
##Example
Example
##Just a few if statements will do.
##Example
Just a few if statements will do.
Example
There is no need to create any arrays. Your solutions is exponentially(削除) exponentially (削除ここまで) lots more work than is needed. For speed aim to reduce the number of times your code loops. Note that Array.map
is the same as doing a for loop over each item, but much slower in this case.
NOTE I struck out exponentially and added "lots" as some have complained, I used it in the common vernacular (increasing rate of change) and not as in CS time complexity.
Your code loops over the length of the house, then the number of apples and oranges, and then you loop the length of the house times the number of apples plus oranges.
function countApplesAndOranges(houseLeft, houseRight, appleTreePos, orangeTreePos, apples, oranges) {
var appleCount = 0;
var orangeCount = 0;
var i = 0;
// find smallest group
const len = Math.min(apples.length, oranges.length);
// do apples and oranges
while (i < len) {
let pos = apples[i] + appleTreePos;
appleCount += pos >= houseLeft && pos <= houseRight ? 1 : 0;
pos = oranges[i++] + orangeTreePos;
orangeCount += pos >= houseLeft && pos <= houseRight ? 1 : 0;
}
// if same number of fallen then return result
if (apples.length === oranges.length) {
console.log(`${appleCount}${'\n'}${orangeCount}`);
return;
}
// if more apples have fallen the do remaining apples
if (apples.length > len) {
while (i < apples.length) {
const pos = apples[i++] + appleTreePos;
appleCount += pos >= houseLeft && pos <= houseRight ? 1 : 0;
}
// all done return result
console.log(`${appleCount}${'\n'}${orangeCount}`);
return;
}
// must be more oranges so do the rest of them and return result
while (i < oranges.length) {
const pos = oranges[i++] + orangeTreePos;
orangeCount += pos >= houseLeft && pos <= houseRight ? 1 : 0;
}
console.log(`${appleCount}${'\n'}${orangeCount}`);
}
There is no need to create any arrays. Your solutions is exponentially more work than is needed. For speed aim to reduce the number of times your code loops. Note that Array.map
is the same as doing a for loop over each item, but much slower in this case.
Your code loops over the length of the house, then the number of apples and oranges, and then you loop the length of the house times the number of apples plus oranges.
function countApplesAndOranges(houseLeft, houseRight, appleTreePos, orangeTreePos, apples, oranges)
var appleCount = 0;
var orangeCount = 0;
var i = 0;
// find smallest group
const len = Math.min(apples.length, oranges.length);
// do apples and oranges
while (i < len) {
let pos = apples[i] + appleTreePos;
appleCount += pos >= houseLeft && pos <= houseRight ? 1 : 0;
pos = oranges[i++] + orangeTreePos;
orangeCount += pos >= houseLeft && pos <= houseRight ? 1 : 0;
}
// if same number of fallen then return result
if (apples.length === oranges.length) {
console.log(`${appleCount}${'\n'}${orangeCount}`);
return;
}
// if more apples have fallen the do remaining apples
if (apples.length > len) {
while (i < apples.length) {
const pos = apples[i++] + appleTreePos;
appleCount += pos >= houseLeft && pos <= houseRight ? 1 : 0;
}
// all done return result
console.log(`${appleCount}${'\n'}${orangeCount}`);
return;
}
// must be more oranges so do the rest of them and return result
while (i < oranges.length) {
const pos = oranges[i++] + orangeTreePos;
orangeCount += pos >= houseLeft && pos <= houseRight ? 1 : 0;
}
console.log(`${appleCount}${'\n'}${orangeCount}`);
}
There is no need to create any arrays. Your solutions is (削除) exponentially (削除ここまで) lots more work than is needed. For speed aim to reduce the number of times your code loops. Note that Array.map
is the same as doing a for loop over each item, but much slower in this case.
NOTE I struck out exponentially and added "lots" as some have complained, I used it in the common vernacular (increasing rate of change) and not as in CS time complexity.
Your code loops over the length of the house, then the number of apples and oranges, and then you loop the length of the house times the number of apples plus oranges.
function countApplesAndOranges(houseLeft, houseRight, appleTreePos, orangeTreePos, apples, oranges) {
var appleCount = 0;
var orangeCount = 0;
var i = 0;
// find smallest group
const len = Math.min(apples.length, oranges.length);
// do apples and oranges
while (i < len) {
let pos = apples[i] + appleTreePos;
appleCount += pos >= houseLeft && pos <= houseRight ? 1 : 0;
pos = oranges[i++] + orangeTreePos;
orangeCount += pos >= houseLeft && pos <= houseRight ? 1 : 0;
}
// if same number of fallen then return result
if (apples.length === oranges.length) {
console.log(`${appleCount}${'\n'}${orangeCount}`);
return;
}
// if more apples have fallen the do remaining apples
if (apples.length > len) {
while (i < apples.length) {
const pos = apples[i++] + appleTreePos;
appleCount += pos >= houseLeft && pos <= houseRight ? 1 : 0;
}
// all done return result
console.log(`${appleCount}${'\n'}${orangeCount}`);
return;
}
// must be more oranges so do the rest of them and return result
while (i < oranges.length) {
const pos = oranges[i++] + orangeTreePos;
orangeCount += pos >= houseLeft && pos <= houseRight ? 1 : 0;
}
console.log(`${appleCount}${'\n'}${orangeCount}`);
}
function countApplesAndOranges(houseLeft, houseRight, appleTreePos, orangeTreePos, apples, oranges)
var appleCount = 0;
var orangeCount = 0;
var i = 0;
// find smallest group
const len = Math.min(apples.length, oranges.length);
// do apples and oranges
while (i < len) {
let pos = apples[i] + appleTreePos;
appleCount =+= pos >= houseLeft && pos <= houseRight ? 1 : 0;
pos = oranges[i++] + orangeTreePos;
orangeCount =+= pos >= houseLeft && pos <= houseRight ? 1 : 0;
}
// if same number of fallen then return result
if (apples.length === oranges.length) {
console.log(`${applesCountappleCount}${'\n'}${orangesCountorangeCount}`);
return;
}
// if more apples have fallen the do remaining apples
if (apples.length > len) {
while (i < apples.length) {
const pos = apples[i++] + appleTreePos;
appleCount =+= pos >= houseLeft && pos <= houseRight ? 1 : 0;
}
// all done return result
console.log(`${applesCountappleCount}${'\n'}${orangesCountorangeCount}`);
return;
}
// must be more oranges so do the rest of them and return result
while (i < oranges.length) {
const pos = oranges[i++] + orangeTreePos;
orangeCount =+= pos >= houseLeft && pos <= houseRight ? 1 : 0;
}
console.log(`${applesCountappleCount}${'\n'}${orangesCountorangeCount}`);
}
function countApplesAndOranges(houseLeft, houseRight, appleTreePos, orangeTreePos, apples, oranges)
var appleCount = 0;
var orangeCount = 0;
var i = 0;
// find smallest group
const len = Math.min(apples.length, oranges.length);
// do apples and oranges
while (i < len) {
let pos = apples[i] + appleTreePos;
appleCount = pos >= houseLeft && pos <= houseRight ? 1 : 0;
pos = oranges[i++] + orangeTreePos;
orangeCount = pos >= houseLeft && pos <= houseRight ? 1 : 0;
}
// if same number of fallen then return result
if (apples.length === oranges.length) {
console.log(`${applesCount}${'\n'}${orangesCount}`);
return;
}
// if more apples have fallen the do remaining apples
if (apples.length > len) {
while (i < apples.length) {
const pos = apples[i++] + appleTreePos;
appleCount = pos >= houseLeft && pos <= houseRight ? 1 : 0;
}
// all done return result
console.log(`${applesCount}${'\n'}${orangesCount}`);
return;
}
// must be more oranges so do the rest of them and return result
while (i < oranges.length) {
const pos = oranges[i++] + orangeTreePos;
orangeCount = pos >= houseLeft && pos <= houseRight ? 1 : 0;
}
console.log(`${applesCount}${'\n'}${orangesCount}`);
}
function countApplesAndOranges(houseLeft, houseRight, appleTreePos, orangeTreePos, apples, oranges)
var appleCount = 0;
var orangeCount = 0;
var i = 0;
// find smallest group
const len = Math.min(apples.length, oranges.length);
// do apples and oranges
while (i < len) {
let pos = apples[i] + appleTreePos;
appleCount += pos >= houseLeft && pos <= houseRight ? 1 : 0;
pos = oranges[i++] + orangeTreePos;
orangeCount += pos >= houseLeft && pos <= houseRight ? 1 : 0;
}
// if same number of fallen then return result
if (apples.length === oranges.length) {
console.log(`${appleCount}${'\n'}${orangeCount}`);
return;
}
// if more apples have fallen the do remaining apples
if (apples.length > len) {
while (i < apples.length) {
const pos = apples[i++] + appleTreePos;
appleCount += pos >= houseLeft && pos <= houseRight ? 1 : 0;
}
// all done return result
console.log(`${appleCount}${'\n'}${orangeCount}`);
return;
}
// must be more oranges so do the rest of them and return result
while (i < oranges.length) {
const pos = oranges[i++] + orangeTreePos;
orangeCount += pos >= houseLeft && pos <= houseRight ? 1 : 0;
}
console.log(`${appleCount}${'\n'}${orangeCount}`);
}