2024-09-03

 03 Sep 2024

Find a Path with the fewest number of Jumps

Taken from DailyCodingProblem

Starting from 0 on a number line, you would like to make a series of jumps that lead to the integer N.

On the ith jump, you may move exactly i places to the left or right.

Find a path with the fewest number of jumps required to get from 0 to N.

Solution

This is the answer I came up with using JavaScript:

// the last element is the integer N, represented by an array of length N
let ten = [1,2,3,4,5,6,7,8,9,10]
// we can avoid using a literal for longer arrays
let eleven = Array(11)
let two = Array(2)
let one = [1]
let twentyOne = Array(21)
let fortySeven = Array(47)

/**
 * @param {number[]} arr
 * @returns {number}
 */

function jumps(arr) {
    // jump from 0 to 1
    if (arr.length === 1) { return 1 }

    // jump from 0 to N/2, and then to N
    if (arr.length % 2 === 0) {
        return 2
    } else {
        // jump from 0 to 1, and then to N/2, and then to N
        return 3
    }
}

console.log(
    jumps(ten), // 2
    jumps(eleven), // 3    
    jumps(two), // 2  
    jumps(one), // 1    
    jumps(twentyOne), // 3
    jumps(fortySeven) // 3
)

And also implemented using Ruby:

ten = Array.new(10)
eleven = Array.new(11)
two = Array.new(2)
one = Array.new(1)
twenty_one = Array.new(21)
forty_seven = Array.new(47)

def jumps(array)
  if array.length === 1
    return 1
  end
  
  if array.length % 2 === 0
    return 2
  else
    return 3
  end
end

puts jumps(ten) # 2
puts jumps(eleven) # 3
puts jumps(two) # 2
puts jumps(one) # 1
puts jumps(twenty_one) # 3
puts jumps(forty_seven) # 3

Testing

Solution tested in JavaScript REPL:

https://www.typescriptlang.org/play/?filetype=js#code/PTAEBcAsFNQGwIYGdymnaBbaA7VBLJCGUfPaAc2gCdQA5AGlGugAcWldxoATUAIwCeoBDhHVqCYQHsAZvFwUo9AFAZU3MQF5QAbQCMDAEwMAzAwAsDAKwMAbAwDsDABwMAnA30AGALoqQUAB3WABjUREAN2l8PgBXJDIKEXh8bkk4UFlpWjhpHCpaBAkpJDVoVHRoSNxQHQBBEsEACn19AEpyjSDpOtBGyRajTvVQfNgdA39R8BC8QQB5HAn+puajDq6snPBBAGVq2oa1i0dOgIAqC5VQC9AAAVZihExQAG8cOMx+Gl1fAF9xNQbnd7ixwHFqDgiB8vj9qP8QcAVCpZHEcKFwPh8qAAFZfVhIZrFajtd43UCUwL4zCsLLUaSvbwQXr6CmU-DyYkSAB0GAKyi0QtAHXezAqkLE+lAiPZoGpBPpjNAzPAvTowBMIhwfCgtTVqkpHK5JL5imUAFJQEY6sLvGS3nKjeKIVDrXLAehOOTnc6FbSlUyWSKmKJdTAxAaNVqw8R9eqnUbwZLQKYPSpZaF8khpBg+dIKM05TTCc1NO0mIEjMWCUSqjUcBX5WBTM6a7SibNpE2q5T26Xxj2wNKjf3O3NdktoEPU2PmtlqLsDg2yYE050gA

Solution tested in Ruby REPL:

https://try.ruby-lang.org/playground/#code=ten+%3D+Array.new(10)%0Aeleven+%3D+Array.new(11)%0Atwo+%3D+Array.new(2)%0Aone+%3D+Array.new(1)%0Atwenty_one+%3D+Array.new(21)%0Aforty_seven+%3D+Array.new(47)%0A%0Adef+jumps(array)%0A++if+array.length+%3D%3D%3D+1%0A++++return+1%0A++end%0A++%0A++if+array.length+%25+2+%3D%3D%3D+0%0A++++return+2%0A++else%0A++++return+3%0A++end%0Aend%0A%0Aputs+jumps(ten)+%23+2%0Aputs+jumps(eleven)+%23+3%0Aputs+jumps(two)+%23+2%0Aputs+jumps(one)+%23+1%0Aputs+jumps(twenty_one)+%23+3%0Aputs+jumps(forty_seven)+%23+3%0A&engine=cruby-3.2.0

Copyright © Paramdeo Singh · All Rights Reserved · Built with Jekyll

This node last updated October 9, 2024 and is permanently morphing...

Paramdeo Singh Guyana

Generalist. Edgerunner. Riding the wave of consciousness in this treacherous mortal sea.

Technology Design Strategy Literature Personal Blogs
Search Site

Results are from Blog, Link Dumps, and #99Problems