'Jumping on the Clouds HackerRank
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)
Solution 1:[1]
function jumpingOnClouds(c) {
var n = 0;
for (var i = 0; i < c.length - 1;) {
i += (c[i+2] ? 1 : 2);
n++;
}
return n;
}
Solution 2:[2]
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);
Solution 3:[3]
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));
};
Solution 4:[4]
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;
}
Solution 5:[5]
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 6:[6]
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;
}
Solution 7:[7]
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;
}
Solution 8:[8]
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++;
}
}
}
Solution 9:[9]
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;
}
Solution 10:[10]
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;
}
Solution 11:[11]
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;
}
Solution 12:[12]
'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();
}
Solution 13:[13]
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`
}
}
Solution 14:[14]
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;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
