I want to make a function to return an array with the number of steps it will take to finish another array but with a little condition that i want to take my steps on 0 only and that mean if i have array c = [0,0,0,1,0,0,1,0] it will take the first three 0 as one step but if i have just one 0 as you see in the end of the array it will be a step so for this array it will take 4 steps to finish it (0,0,0)(0)(0)(0) as you see it will ignore (1)
This is the link of the test if you want better description HakerRank tets link
var array = [0, 0, 0, 1, 0, 0, 1, 0]
var stepsArray = [];
function jumpingOnClouds(c) {
for (var i = 0; i < c.length; i++) {
if (c[i] === 0) {
if (c[i] === c[i + 1])
stepsArray.push(c[i + 1])
} else {
stepsArray.push(c[i])
}
}
return stepsArray.length
}
var result = jumpingOnClouds(array);
console.log(result);
I want to make a function to return an array with the number of steps it will take to finish another array but with a little condition that i want to take my steps on 0 only and that mean if i have array c = [0,0,0,1,0,0,1,0] it will take the first three 0 as one step but if i have just one 0 as you see in the end of the array it will be a step so for this array it will take 4 steps to finish it (0,0,0)(0)(0)(0) as you see it will ignore (1)
This is the link of the test if you want better description HakerRank tets link
var array = [0, 0, 0, 1, 0, 0, 1, 0]
var stepsArray = [];
function jumpingOnClouds(c) {
for (var i = 0; i < c.length; i++) {
if (c[i] === 0) {
if (c[i] === c[i + 1])
stepsArray.push(c[i + 1])
} else {
stepsArray.push(c[i])
}
}
return stepsArray.length
}
var result = jumpingOnClouds(array);
console.log(result);
I also have tried this but it keep giving me the same error
var array = [0, 0, 0, 1, 0, 0]
var stepsArray = [];
function jumpingOnClouds(c) {
for(var i = 0; i < c.length - 1; i++){
if (c[i] === 0) {
console.log(c[i])
if (c[i] === c[i + 1] && c[i + 1] === c[i + 2]) {
stepsArray.push(c[i + 2])
} else if (c[i] === c[i + 1]) {
stepsArray.push(c[i + 1])
} else {
stepsArray.push(c[i])
}
}
}
return stepsArray.length
}
var result = jumpingOnClouds(array);
console.log(result)
Share
Improve this question
edited Oct 30, 2019 at 17:14
mohamed nageh
asked Oct 30, 2019 at 16:22
mohamed nagehmohamed nageh
7335 gold badges11 silver badges27 bronze badges
10
|
Show 5 more comments
16 Answers
Reset to default 6Just Added 2 constraints on your solution for the index of I
and removed unwanted conditions:
I tested my solution on Hackerrank:
Check this out:
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
inputString += inputStdin;
});
process.stdin.on('end', _ => {
inputString = inputString.replace(/\s*$/, '')
.split('\n')
.map(str => str.replace(/\s*$/, ''));
main();
});
function readLine() {
return inputString[currentLine++];
}
// Complete the jumpingOnClouds function below.
function jumpingOnClouds(c) {
var stepsArray = [];
let i=0;
while(i < c.length - 1){
if ((i+2<c.length) && (c[i+2] === 0)) {
stepsArray.push(c[i + 2]);
i+=2;
} else{
stepsArray.push(c[i + 1]);
i+=1;
}
}
return stepsArray.length
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const n = parseInt(readLine(), 10);
const c = readLine().split(' ').map(cTemp => parseInt(cTemp, 10));
let result = jumpingOnClouds(c);
ws.write(result + "\n");
ws.end();
}
function jumpingOnClouds(c) {
var n = 0;
for (var i = 0; i < c.length - 1;) {
i += (c[i+2] ? 1 : 2);
n++;
}
return n;
}
The first problem is that you're using a global variable stepsArray
to keep track of the steps and you're not clearing the steps after or before each call to jumpingOnClouds
which means that stepsArray
will accumulate the steps of each tests.
Second, you need to increment i
when you get a double 0
step.
Here is a modified working version of your approach:
var array = [0, 0, 0, 1, 0, 0, 1, 0];
function jumpingOnClouds(c) {
var count = 0;
for (var i = 0; i < c.length; i++) {
if (c[i] === 0) {
if (c[i] === c[i + 1]) {
count++;
i++;
}
} else {
count++;
}
}
return count;
}
var result = jumpingOnClouds(array);
console.log(result);
That's my solution:
function jumpingOnClouds(arr) {
let jumps = 0;
let index = 0;
const arrLength = arr.length;
while (index < arrLength) {
const isLastItem = index === arrLength - 1;
const nextItem = arr[index+1];
const nextNextItem = arrLength > index+2 ? arr[index+2] : null;
if (isLastItem) {
index++;
continue;
}
if (nextItem === 1) {
jumps++;
index = index + 2;
continue;
}
if (nextItem === 0) {
if (nextNextItem === 0) {
jumps++;
index = index + 2;
} else if (nextNextItem === 1 || !nextNextItem) {
jumps++;
index++;
}
continue;
}
}
return jumps;
}
The easiest way
const recCloud = clouds => {
if(clouds.length < 4)
return 1;
else
return 1 + recCloud(clouds.slice(clouds[2] == 0 ? 2 : 1));
};
or even
const recCloud = clouds => {
return clouds.length < 4 ? 1 : 1 + recCloud(clouds.slice(clouds[2] == 0 ? 2 : 1));
};
const jumpingOnClouds = (clouds) => {
let jumpCount = 0;
let currentCloud = 0;
while (currentCloud < clouds.length-1) {
// There are only two ways to jump. Either jump two clouds away or one
// cloud away from the current position
// Check if next 2 clouds are "cumulus" which values are 0s
// If they are cumulus then jump to 2 clouds away from the current cloud
// else jump 1 position away from the current cloud
// Since you jump on both cases, you just have to increment the number
// of jumps by 1.
if (clouds[currentCloud + 2] == 0) {
currentCloud += 2;
} else {
currentCloud ++;
}
jumpCount++;
}
return jumpCount;
}
int jumpingOnClouds(vector<int> c)
{
int n;
n=c.size();
int count = 0;
for (int i = 0; i < n-1; i++)
{
count++;
if (i<n-2 && c[i+2]==0) i++;
}
return count;
}
Solution using Java. Passed all test cases given.
static int jumpingOnClouds(int[] c) {
int jump = 0;
int pos = 0;
while (pos < c.length) {
if (pos + 2 < c.length && c[pos + 2] == 0) {
pos = pos + 2;
jump++;
} else if (pos + 1 < c.length && c[pos + 1] == 0) {
pos = pos + 1;
jump++;
} else
break;
}
return jump;
}
If you are looking same in Java
int jumpcount=0;
for(int i=0; i<c.length-1;){
if(c[i]==0){
if(i+2<c.length && c[i+2]==0){
jumpcount++;
i+=2;
}else if(c[i+1]==0){
jumpcount++;
i++;
}
}
}
int jumpingOnClouds(int c_count, int* c) {
int i=0,jump=0;
while(i<c_count-1){
if(*(c+i+1)==0 && *(c+i+2)==0){
jump++; i+=2;
}
else if((*(c+i+1)==0 && *(c+i+2)==1) || *(c+i+1)==0){
jump++; i++;
}
else if(*(c+i+1)==1 && *(c+i+2)==0){
jump++; i+=2;
}
else
break;
}
return jump;
}
static int jumpingOnClouds(int[] c) {
int num_jumps = 0;
int i = 0;
while (i < c.length - 1) {
if (i + 2 == c.length || c[i + 2] == 1) {
i++;
num_jumps++;
} else {
i += 2;
num_jumps++;
}
}
return num_jumps;
}
Php solution to jumpingclouds
function jumpingOnClouds($c) {
$count = 0;
for ($i = 1; $i < count($c); $i++) {
$a = $c[$i];
if ($a == 0) {
if ($a === $c[$i + 1] && $a === $c[$i - 1]) {
$count++;
$i++;
} else {
$count++;
}
}
}
return $count;
}
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'jumpingOnClouds' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY c as parameter.
*/
function jumpingOnClouds(c) {
// Write your code here
let jumpCount = 0;
let step = 0;
for(let i=0; i<c.length; i){
if(c[i] === 0 && i !== (c.length - 2)){
i = i + 2;
jumpCount++
} else if (c[i] === 0 && i === (c.length - 2)){
i = i + 1;
jumpCount++
} else {
i = i - 1;
}
}
return jumpCount - 1;
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const n = parseInt(readLine().trim(), 10);
const c = readLine().replace(/\s+$/g, '').split(' ').map(cTemp => parseInt(cTemp, 10));
const result = jumpingOnClouds(c);
ws.write(result + '\n');
ws.end();
}
class Program
{
static void Main(string[] args)
{
int[] arr = new []{0,0,1,0,0,1,1,0};
int k = 2;
MyHackerRankCode(arr,k);
BestHackerRankCode(arr,k);
}
private static void BestHackerRankCode(int[] c, int k)
{
int i = 0;
int E = 100;
int n = c.Length;
do {
E--;
i = (i + k) % n;
//Console.WriteLine(9%10);
if (c[i] == 1)
E -= 2;
} while (i != 0);
Console.Out.WriteLine(E);
}
private static void MyHackerRankCode(int[] c, int k)
{
int counter =0;
int resultEnergy =100;
int prev = 0;
do
{
prev = counter;
counter += k;
if(counter > c.Length-1)
{
counter = k - ((c.Length-1) -prev) -1 ;
}
if(counter == 0)
{
if(c[0] == 1)
resultEnergy = resultEnergy - 1 - 2;
else if(c[0] == 0)
resultEnergy = resultEnergy - 1;
}
else
{
if(c[counter] == 1)
resultEnergy = resultEnergy - 1 - 2;
else //if(c[counter] == 0)
resultEnergy = resultEnergy - 1;
}
}
while(counter !=0);
//return resultEnergy;
Console.WriteLine(resultEnergy);`enter code here`
}
}
This would be my solution:
function jumpingOnClouds(c) {
let jump = 0;
for (let i = 0; i < c.length - 1; i++) {
if (c[i + 2] == 0) {
jump = jump + 1;
i = i + 1;
} else
if (c[i + 1] == 0) {
jump = jump + 1;
i = i + 0;
}
}
return jump;
}
function jumpingOnClouds(c) {
let jumps = 0;
for (let i = 0; i < c.length - 1; i++, jumps++) {
if (i + 2 < c.length && c[i + 2] == 0) {
i++;
}
}
return jumps;
}
(0,0)(0)(0,0)(0)
. – connexo Commented Oct 30, 2019 at 16:30