Fundamentals
Arrays
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Arrays; import java.util.HashSet; import java.util.Random; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test void basicTest() { assertEquals(10, Solution.lastPoint(52, 5, new int[] { 2, 5, 10, 15 })); assertEquals(45, Solution.lastPoint(250, 5, new int[] { 10, 15, 30, 45 })); assertEquals(-1, Solution.lastPoint(50, 25, new int[] { 10, 20, 30, 40, 50 })); } @Test void moreBasicTest() { assertEquals(16, Solution.lastPoint(100, 2, new int[] { 4, 8, 12, 16 })); assertEquals(20, Solution.lastPoint(130, 4, new int[] { 5, 15, 20, 50 })); assertEquals(-1, Solution.lastPoint(200, 10, new int[] { 80, 85, 100 })); } @Test void rareTest() { assertEquals(-1, Solution.lastPoint(0, 2, new int[] { 40, 80, 120, 160 })); assertEquals(160, Solution.lastPoint(100, 0, new int[] { 40, 80, 120, 160 })); assertEquals(0, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 0 })); assertEquals(5, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 5 })); } @Test void randomTest() { int fuel =0; int consumption =0; int[] stations; int[] values; values = randomNumbers(200); for(int i = 0; i <= 20;i++){ for(int valores : randomNumbers(200)) fuel =values[0]; consumption =values[1]; } } public static int[] randomNumbers(int valorMaximo) { Random random = new Random(); int[] numeros = new int[2]; do { numeros[0] = random.nextInt(valorMaximo + 1); numeros[1] = random.nextInt(valorMaximo + 1); } while (numeros[0] == numeros[1]); Arrays.sort(numeros); return numeros; } private static int[] randomArray(int max) { int n = Math.min(max, new Random().nextInt(100)); HashSet<Integer> distinctsNumbers = new HashSet<>(); while (distinctsNumbers.size() < n) { distinctsNumbers.add(new Random().nextInt(max)); } int[] result = new int[n]; int i = 0; for (Integer value : distinctsNumbers) result[i++] = value; Arrays.sort(result); return result; } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import java.util.Arrays;
- import java.util.HashSet;
- import java.util.Random;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- @Test
- void basicTest() {
- assertEquals(10, Solution.lastPoint(52, 5, new int[] { 2, 5, 10, 15 }));
- assertEquals(45, Solution.lastPoint(250, 5, new int[] { 10, 15, 30, 45 }));
- assertEquals(-1, Solution.lastPoint(50, 25, new int[] { 10, 20, 30, 40, 50 }));
- }
- @Test
- void moreBasicTest() {
- assertEquals(16, Solution.lastPoint(100, 2, new int[] { 4, 8, 12, 16 }));
- assertEquals(20, Solution.lastPoint(130, 4, new int[] { 5, 15, 20, 50 }));
- assertEquals(-1, Solution.lastPoint(200, 10, new int[] { 80, 85, 100 }));
- }
- @Test
- void rareTest() {
- assertEquals(-1, Solution.lastPoint(0, 2, new int[] { 40, 80, 120, 160 }));
- assertEquals(160, Solution.lastPoint(100, 0, new int[] { 40, 80, 120, 160 }));
- assertEquals(0, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 0 }));
- assertEquals(5, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 5 }));
- }
- @Test
- void randomTest() {
- int fuel =0;
- int consumption =0;
- int[] stations;
- int[] values;
- values = randomNumbers(200);
- for(int i = 0; i <= 20;i++){
- for(int valores : randomNumbers(200))
- fuel =values[0];
- consumption =values[1];
- }
- }
- public static int[] randomNumbers(int valorMaximo) {
- Random random = new Random();
- int[] numeros = new int[2];
- do {
- numeros[0] = random.nextInt(valorMaximo + 1);
- numeros[1] = random.nextInt(valorMaximo + 1);
- } while (numeros[0] == numeros[1]);
- Arrays.sort(numeros);
- return numeros;
- }
- private static int[] randomArray(int max) {
- int n = Math.min(max, new Random().nextInt(100));
- HashSet<Integer> distinctsNumbers = new HashSet<>();
- while (distinctsNumbers.size() < n) {
- distinctsNumbers.add(new Random().nextInt(max));
- }
- int[] result = new int[n];
- int i = 0;
- for (Integer value : distinctsNumbers)
- result[i++] = value;
- Arrays.sort(result);
- return result;
- }
- }
Fundamentals
Arrays
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Arrays; import java.util.Random; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test void basicTest() { assertEquals(10, Solution.lastPoint(52, 5, new int[] { 2, 5, 10, 15 } )); assertEquals(45, Solution.lastPoint(250, 5, new int[] { 10, 15, 30, 45 } )); assertEquals(-1, Solution.lastPoint(50, 25, new int[] { 10, 20, 30, 40, 50 } )); } @Test void moreBasicTest() { assertEquals(16, Solution.lastPoint(100, 2, new int[] { 4, 8, 12, 16 } )); assertEquals(20, Solution.lastPoint(130, 4, new int[] { 5, 15, 20, 50 } )); assertEquals(-1, Solution.lastPoint(200, 10, new int[] { 80, 85, 100} )); } @Test void rareTest() { assertEquals(-1, Solution.lastPoint(0, 2, new int[] { 40, 80, 120, 160 } )); assertEquals(160, Solution.lastPoint(100, 0, new int[] { 40, 80, 120, 160 } )); assertEquals(0, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 0 } )); assertEquals(5, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 5 } )); } public static int[] randomNumbers(int valorMaximo) { Random random = new Random(); int[] numeros = new int[2]; do { numeros[0] = random.nextInt(valorMaximo + 1); numeros[1] = random.nextInt(valorMaximo + 1); } while (numeros[0] == numeros[1]); Arrays.sort(numeros); return numeros; } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import java.util.Arrays;
- import java.util.Random;
- // TODO: Replace examples and use TDD by writing your own tests
- class SolutionTest {
- @Test
- void basicTest() {
- assertEquals(10, Solution.lastPoint(52, 5, new int[] { 2, 5, 10, 15 } ));
- assertEquals(45, Solution.lastPoint(250, 5, new int[] { 10, 15, 30, 45 } ));
- assertEquals(-1, Solution.lastPoint(50, 25, new int[] { 10, 20, 30, 40, 50 } ));
- }
- @Test
- void moreBasicTest() {
- assertEquals(16, Solution.lastPoint(100, 2, new int[] { 4, 8, 12, 16 } ));
- assertEquals(20, Solution.lastPoint(130, 4, new int[] { 5, 15, 20, 50 } ));
- assertEquals(-1, Solution.lastPoint(200, 10, new int[] { 80, 85, 100} ));
- }
- @Test
- void rareTest() {
- assertEquals(-1, Solution.lastPoint(0, 2, new int[] { 40, 80, 120, 160 } ));
- assertEquals(160, Solution.lastPoint(100, 0, new int[] { 40, 80, 120, 160 } ));
- assertEquals(0, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 0 } ));
- assertEquals(5, Solution.lastPoint(200, 5, new int[] { 0, 0, 0, 5 } ));
- }
- public static int[] randomNumbers(int valorMaximo) {
- Random random = new Random();
- int[] numeros = new int[2];
- do {
- numeros[0] = random.nextInt(valorMaximo + 1);
- numeros[1] = random.nextInt(valorMaximo + 1);
- } while (numeros[0] == numeros[1]);
- Arrays.sort(numeros);
- return numeros;
- }
- }
Fundamentals
Arrays
class Solution{ public static int lastPoint(int fuel, int consumption, int[] stations) { if (fuel / consumption < stations[0]) { return -1; } int fuelLeft = fuel - stations[0] * consumption; for (int i = 1; i < stations.length; i++) { int distance = stations[i] - stations[i - 1]; if (fuelLeft > distance * consumption) { fuelLeft = fuelLeft - (distance * consumption); } else { return stations[i - 1]; } } return stations[stations.length - 1]; } }
- class Solution{
- public static int lastPoint(int fuel, int consumption, int[] stations) {
- if (fuel / consumption < stations[0]) {
- return -1;
- }
- int fuelLeft = fuel - stations[0] * consumption;
- for (int i = 1; i < stations.length; i++) {
- int distance = stations[i] - stations[i - 1];
fuelLeft = fuelLeft - distance * consumption;if (fuelLeft < 0) {- if (fuelLeft > distance * consumption) {
- fuelLeft = fuelLeft - (distance * consumption);
- }
- else {
- return stations[i - 1];
- }
- }
return -1;- return stations[stations.length - 1];
- }
- }
Fundamentals
Arrays
class Solution{ public static int lastPoint(int fuel, int consumption, int[] stations) { if (fuel / consumption < stations[0]) { return -1; } int fuelLeft = fuel - stations[0] * consumption; for (int i = 1; i < stations.length; i++) { int distance = stations[i] - stations[i - 1]; fuelLeft = fuelLeft - distance * consumption; if (fuelLeft < 0) { return stations[i - 1]; } } return -1; } }
- class Solution{
public static int lastPoint (int fuel, int consumption, int[]stations){//DO YOUR MAGIC!! YESreturn -1;}- public static int lastPoint(int fuel, int consumption, int[] stations) {
- if (fuel / consumption < stations[0]) {
- return -1;
- }
- int fuelLeft = fuel - stations[0] * consumption;
- for (int i = 1; i < stations.length; i++) {
- int distance = stations[i] - stations[i - 1];
- fuelLeft = fuelLeft - distance * consumption;
- if (fuelLeft < 0) {
- return stations[i - 1];
- }
- }
- return -1;
- }
- }