Skip to content

Commit

Permalink
Add frog jump (Go/Java)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuamegnauth54 committed May 11, 2024
1 parent c942da9 commit af00c44
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Go/jobspls/algorithms/frog_jump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"fmt"
"math"
)

// How many jumps does it take to reach dest by dist?
func FrogJump(start, dest, dist uint) (uint, error) {
if start > dest {
return 0, fmt.Errorf("start > dest")
}

diff := dest - start
jumps := uint(diff/dist) + uint(math.Min(1.0, float64(diff%dist)))

return jumps, nil
}
22 changes: 22 additions & 0 deletions Java/jobspls/algorithms/FrogJump.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package jobspls.algorithms;

import java.lang.Math;
import java.util.OptionalInt;

public class FrogJump {
public static OptionalInt frog_jump(int start, int dest, int dist) {
if (
start > dest ||
start < 0 ||
dest < 0 ||
dist <= 0
) {
return OptionalInt.empty();
}

var diff = dest - start;
var jumps = (int)(diff/dist) + (int)(Math.min(1.0, diff%dist));

return OptionalInt.of(jumps);
}
}

0 comments on commit af00c44

Please sign in to comment.