From 8292c346aa3ec197b5200987b915e6e0fb217a58 Mon Sep 17 00:00:00 2001 From: dadgam3er Date: Sun, 24 Nov 2024 19:24:23 -0500 Subject: [PATCH] fix: 2 more challenges --- EggDropping/EggDropping.java | 55 ++++++++++++++++++++++++++++++++++ GamePlatform/.idea/.gitignore | 3 ++ GamePlatform/.idea/misc.xml | 6 ++++ GamePlatform/.idea/modules.xml | 8 +++++ GamePlatform/.idea/vcs.xml | 6 ++++ GamePlatform/FinalSpeed.java | 25 ++++++++++++++++ GamePlatform/GamePlatform.iml | 11 +++++++ 7 files changed, 114 insertions(+) create mode 100644 EggDropping/EggDropping.java create mode 100644 GamePlatform/.idea/.gitignore create mode 100644 GamePlatform/.idea/misc.xml create mode 100644 GamePlatform/.idea/modules.xml create mode 100644 GamePlatform/.idea/vcs.xml create mode 100644 GamePlatform/FinalSpeed.java create mode 100644 GamePlatform/GamePlatform.iml diff --git a/EggDropping/EggDropping.java b/EggDropping/EggDropping.java new file mode 100644 index 0000000..73f3280 --- /dev/null +++ b/EggDropping/EggDropping.java @@ -0,0 +1,55 @@ +import java.util.Scanner; + +/** + * EggDropping + * You are given k identical eggs and you have access to a building with n + * floors labeled from 1 to n. + * + * You know that there exists a floor f where 0 <= f <= n such that any egg + * dropped at a floor higher than f will break, + * and any egg dropped at or below floor f will not break. + * + * Each move, you may take an unbroken egg and drop it from any floor x (where 1 + * <= x <= n). + * If the egg breaks, you can no longer use it. However, if the egg does not + * break, you may reuse it in future moves. + * + * Return the minimum number of moves that you need to determine with certainty + * what the value of f is. + * + * Example 1: + * + * Input: k = 1, n = 2 + * Output: 2 + * Explanation: + * Drop the egg from floor 1. If it breaks, we know that f = 0. + * Otherwise, drop the egg from floor 2. If it breaks, we know that f = 1. + * If it does not break, then we know f = 2. + * Hence, we need at minimum 2 moves to determine with certainty what the value + * of f is. + * + */ +public class EggDropping { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + int a = scanner.nextInt(); + int b = scanner.nextInt(); + scanner.close(); + System.out.println(minMoves(a, b)); + } + + private static int minMoves(int k, int n) { + // if n > f ==> eggs break + // if n < f ==> eggs are good + int min = 0; + int[][] dp = new int[n + 1][k + 1]; + while (dp[min][k] < n) { + min++; + for (int i = 1; i <= k; i++) { + dp[min][i] = dp[min - 1][i - 1] + dp[min - 1][i] + 1; + } + } + return min; + } +} diff --git a/GamePlatform/.idea/.gitignore b/GamePlatform/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/GamePlatform/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/GamePlatform/.idea/misc.xml b/GamePlatform/.idea/misc.xml new file mode 100644 index 0000000..31e1ebc --- /dev/null +++ b/GamePlatform/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/GamePlatform/.idea/modules.xml b/GamePlatform/.idea/modules.xml new file mode 100644 index 0000000..1832455 --- /dev/null +++ b/GamePlatform/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/GamePlatform/.idea/vcs.xml b/GamePlatform/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/GamePlatform/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/GamePlatform/FinalSpeed.java b/GamePlatform/FinalSpeed.java new file mode 100644 index 0000000..30ecb7e --- /dev/null +++ b/GamePlatform/FinalSpeed.java @@ -0,0 +1,25 @@ +/** + * FinalSpeed + */ +public class FinalSpeed { + + public static double calculateFinalSpeed(double initialSpeed, int[] inclinations) { + /* + * double finalSpeed = 0; + * for (int speed : inclinations) { + * double temp = initialSpeed; + * initialSpeed = (-1 * speed) + temp; + * finalSpeed = initialSpeed; + * } + */ + double finalSpeed = initialSpeed; + for (int speed : inclinations) { + finalSpeed -= speed; + } + return finalSpeed; + } + + public static void main(String[] args) { + System.out.println(calculateFinalSpeed(60.0, new int[] { 0, 30, 0, -45, 0 })); + } +} diff --git a/GamePlatform/GamePlatform.iml b/GamePlatform/GamePlatform.iml new file mode 100644 index 0000000..b107a2d --- /dev/null +++ b/GamePlatform/GamePlatform.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file