Compare commits
9 Commits
525fa089b9
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 8292c346aa | |||
| fb5f689af3 | |||
| b79fe73ffb | |||
| 47bab72ff3 | |||
| c93a26c059 | |||
| f8748fcbdf | |||
| ba2db6a59e | |||
| 29251ff399 | |||
| 9c622c1aa4 |
@@ -0,0 +1,112 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* CountingValleys
|
||||
* An avid hiker keeps meticulous records of their hikes. During the last hike
|
||||
* that took exactly steps, for every step it was noted if it was an uphill, ,
|
||||
* or a downhill, step. Hikes always start and end at sea level, and each step
|
||||
* up or down represents a
|
||||
*
|
||||
* unit change in altitude. We define the following terms:
|
||||
*
|
||||
* A mountain is a sequence of consecutive steps above sea level, starting with
|
||||
* a step up from sea level and ending with a step down to sea level.
|
||||
* A valley is a sequence of consecutive steps below sea level, starting with a
|
||||
* step down from sea level and ending with a step up to sea level.
|
||||
*
|
||||
* Given the sequence of up and down steps during a hike, find and print the
|
||||
* number of valleys walked through.
|
||||
*
|
||||
* Example
|
||||
*
|
||||
* The hiker first enters a valley units deep. Then they climb out and up onto a
|
||||
* mountain
|
||||
*
|
||||
* units high. Finally, the hiker returns to sea level and ends the hike.
|
||||
*
|
||||
* Function Description
|
||||
*
|
||||
* Complete the countingValleys function in the editor below.
|
||||
*
|
||||
* countingValleys has the following parameter(s):
|
||||
*
|
||||
* int steps: the number of steps on the hike
|
||||
* string path: a string describing the path
|
||||
*
|
||||
* Returns
|
||||
*
|
||||
* int: the number of valleys traversed
|
||||
*
|
||||
* Input Format
|
||||
*
|
||||
* The first line contains an integer
|
||||
* , the number of steps in the hike.
|
||||
* The second line contains a single string , of
|
||||
*
|
||||
* characters that describe the path.
|
||||
*
|
||||
* Constraints
|
||||
*
|
||||
* Sample Input
|
||||
*
|
||||
* 8
|
||||
* UDDDUDUU
|
||||
*
|
||||
* Sample Output
|
||||
*
|
||||
* 1
|
||||
* Explanation
|
||||
*
|
||||
* If we represent _ as sea level, a step up as /, and a step down as \, the
|
||||
* hike can be drawn as:
|
||||
*
|
||||
* The hiker enters and leaves one valley.
|
||||
* BlogScoringEnvironmentFAQAbout UsSupportCareersTerms Of ServicePrivacy Policy
|
||||
*/
|
||||
public class CountingValleys {
|
||||
|
||||
// /\
|
||||
// _ / \_
|
||||
// \ /
|
||||
// \/
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Please enter the number of steps: ");
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int steps = sc.nextInt();
|
||||
sc.nextLine(); // consume the newline
|
||||
|
||||
System.out.println("The hiker is taking " + steps + " steps starting from sea level.");
|
||||
System.out.println("Please enter the path (U for up, D for down):");
|
||||
String path = sc.nextLine();
|
||||
|
||||
sc.close();
|
||||
System.out.println("Number of valleys: " + countingHikeValleys(steps, path));
|
||||
}
|
||||
|
||||
private static int countingHikeValleys(int steps, String path) {
|
||||
int valleysCount = 0;
|
||||
int seaLevel = 0;
|
||||
boolean inValley = false;
|
||||
|
||||
for (char step : path.toCharArray()) {
|
||||
if (Character.toUpperCase(step) == 'U') {
|
||||
seaLevel++;
|
||||
} else if (Character.toUpperCase(step) == 'D') {
|
||||
seaLevel--;
|
||||
}
|
||||
|
||||
// If we're below sea level and weren't before, we're entering a valley
|
||||
if (seaLevel < 0 && !inValley) {
|
||||
valleysCount++;
|
||||
inValley = true;
|
||||
}
|
||||
// If we're at or above sea level, we're no longer in a valley
|
||||
if (seaLevel >= 0) {
|
||||
inValley = false;
|
||||
}
|
||||
}
|
||||
|
||||
return valleysCount;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* FindTheOne
|
||||
* given an array of ints, find the one that start with 1
|
||||
*/
|
||||
public class FindTheOne {
|
||||
public static void main(String[] args) {
|
||||
|
||||
int[] arr = { 1, 23, 123, 45, 67, 176 };
|
||||
System.out.println(OneOne(arr));
|
||||
}
|
||||
|
||||
// private static List<String> OneOne(int[] arr) {
|
||||
// List<String> Ones = new ArrayList<>();
|
||||
// String[] Convereted = new String[arr.length];
|
||||
// for (int i = 0; i < arr.length; i++) {
|
||||
// Convereted[i] = String.valueOf(arr[i]);
|
||||
// }
|
||||
// for (String str : Convereted) {
|
||||
// if (str.startsWith("1")) {
|
||||
// Ones.add(str);
|
||||
// }
|
||||
// }
|
||||
// return Ones;
|
||||
// }
|
||||
private static List<String> OneOne(int[] arr) {
|
||||
List<String> Ones = new ArrayList<>();
|
||||
for (int x : arr) {
|
||||
String str = Integer.toString(x);
|
||||
if (str.startsWith("1")) {
|
||||
Ones.add(str);
|
||||
}
|
||||
}
|
||||
return Ones;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Vowels {
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Please enter your String here: ");
|
||||
String str = sc.nextLine();
|
||||
System.out.println(vowelsFound(str));
|
||||
sc.close();
|
||||
}
|
||||
|
||||
private static boolean vowelsFound(String str) {
|
||||
String vowels = "aeoui";
|
||||
for (char c : str.toLowerCase().toCharArray()) {
|
||||
if (vowels.indexOf(c) != -1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Generated
+3
@@ -0,0 +1,3 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
Generated
+8
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/GamePlatform.iml" filepath="$PROJECT_DIR$/GamePlatform.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -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 }));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
Binary file not shown.
@@ -0,0 +1,117 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* InfinityString
|
||||
* There is a string, , of lowercase English letters that is repeated infinitely
|
||||
* many times. Given an integer, , find and print the number of letter a's in
|
||||
* the first
|
||||
*
|
||||
* letters of the infinite string.
|
||||
*
|
||||
* Example
|
||||
*
|
||||
* The substring we consider is , the first characters of the infinite string.
|
||||
* There are
|
||||
*
|
||||
* occurrences of a in the substring.
|
||||
*
|
||||
* Function Description
|
||||
*
|
||||
* Complete the repeatedString function in the editor below.
|
||||
*
|
||||
* repeatedString has the following parameter(s):
|
||||
*
|
||||
* s: a string to repeat
|
||||
* n: the number of characters to consider
|
||||
*
|
||||
* Returns
|
||||
*
|
||||
* int: the frequency of a in the substring
|
||||
*
|
||||
* Input Format
|
||||
*
|
||||
* The first line contains a single string,
|
||||
* .
|
||||
* The second line contains an integer,
|
||||
*
|
||||
* .
|
||||
*
|
||||
* Constraints
|
||||
*
|
||||
* For of the test cases,
|
||||
*
|
||||
* .
|
||||
*
|
||||
* Sample Input
|
||||
*
|
||||
* Sample Input 0
|
||||
*
|
||||
* aba
|
||||
* 10
|
||||
*
|
||||
* Sample Output 0
|
||||
*
|
||||
* 7
|
||||
*
|
||||
*/
|
||||
public class InfinityString {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
// MY SOLUTION IS SLOW BUT IT WORKS
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Please enter your 'String' here and it must contains the letter A: ");
|
||||
String str = sc.nextLine();
|
||||
System.out.println("Please enter how many times you want to repeat that: ");
|
||||
int x = sc.nextInt();
|
||||
System.out.println(numsOfAs(str, x));
|
||||
sc.close();
|
||||
}
|
||||
|
||||
//
|
||||
// private static int numsOfAs(String str, int n) {
|
||||
// int numberOfAs = 0;
|
||||
// int x = n;
|
||||
// String newSTR = "";
|
||||
// int remeinder = n / str.length();
|
||||
// while (n > 0 && newSTR.toCharArray().length < n + remeinder) {
|
||||
// newSTR += str;
|
||||
// n--;
|
||||
// }
|
||||
//
|
||||
// char[] charNewstr = newSTR.toCharArray();
|
||||
// for (int i = 0; i < x; i++) {
|
||||
// if (charNewstr[i] == 'a') {
|
||||
// numberOfAs++;
|
||||
// }
|
||||
// }
|
||||
// return numberOfAs;
|
||||
//
|
||||
//
|
||||
private static int numsOfAs(String str, int n) {
|
||||
int numberOfAs = 0;
|
||||
int numRepeats = n / str.length();
|
||||
int remainder = n % str.length();
|
||||
|
||||
// Count the number of 'a' characters in the repeated string
|
||||
numberOfAs = countAs(str) * numRepeats;
|
||||
|
||||
// Count the number of 'a' characters in the remaining portion
|
||||
if (remainder > 0) {
|
||||
numberOfAs += countAs(str.substring(0, remainder));
|
||||
}
|
||||
|
||||
return numberOfAs;
|
||||
}
|
||||
|
||||
private static int countAs(String str) {
|
||||
int count = 0;
|
||||
for (char c : str.toCharArray()) {
|
||||
if (c == 'a') {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* JumpingClouds
|
||||
* There is a new mobile game that starts with consecutively numbered clouds.
|
||||
* Some of the clouds are thunderheads and others are cumulus. The player can
|
||||
* jump on any cumulus cloud having a number that is equal to the number of the
|
||||
* current cloud plus or
|
||||
*
|
||||
* . The player must avoid the thunderheads. Determine the minimum number of
|
||||
* jumps it will take to jump from the starting postion to the last cloud. It is
|
||||
* always possible to win the game.
|
||||
*
|
||||
* For each game, you will get an array of clouds numbered
|
||||
* if they are safe or
|
||||
*
|
||||
* if they must be avoided.
|
||||
*
|
||||
* Example
|
||||
* Index the array from . The number on each cloud is its index in the list so
|
||||
* the player must avoid the clouds at indices and . They could follow these two
|
||||
* paths: or . The first path takes jumps while the second takes . Return
|
||||
*
|
||||
* .
|
||||
*
|
||||
* Function Description
|
||||
*
|
||||
* Complete the jumpingOnClouds function in the editor below.
|
||||
*
|
||||
* jumpingOnClouds has the following parameter(s):
|
||||
*
|
||||
* int c[n]: an array of binary integers
|
||||
*
|
||||
* Returns
|
||||
*
|
||||
* int: the minimum number of jumps required
|
||||
*
|
||||
* Input Format
|
||||
*
|
||||
* The first line contains an integer
|
||||
* , the total number of clouds. The second line contains space-separated binary
|
||||
* integers describing clouds where
|
||||
*
|
||||
* .
|
||||
*
|
||||
* Constraints
|
||||
*
|
||||
* Output Format
|
||||
*
|
||||
* Print the minimum number of jumps needed to win the game.
|
||||
*
|
||||
* Sample Input 0
|
||||
*
|
||||
* 7
|
||||
* 0 0 1 0 0 1 0
|
||||
*
|
||||
* Sample Output 0
|
||||
*
|
||||
* 4
|
||||
*
|
||||
*/
|
||||
public class JumpingClouds {
|
||||
public static void main(String[] args) {
|
||||
|
||||
int[] steps = { 0, 0, 0, 0, 0, 1, 0 };
|
||||
System.out.println(JumpsCount(steps));
|
||||
}
|
||||
|
||||
private static int JumpsCount(int[] clounds) {
|
||||
|
||||
int jumps = 0;
|
||||
int i = 0;
|
||||
while (i < clounds.length - 1) {
|
||||
if (i + 2 == clounds.length || clounds[i + 2] == 1) {
|
||||
i++;
|
||||
jumps++;
|
||||
} else {
|
||||
i += 2;
|
||||
jumps++;
|
||||
}
|
||||
}
|
||||
return jumps;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* PairedSocks
|
||||
*/
|
||||
public class PairedSocks {
|
||||
public static void main(String[] args) {
|
||||
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int n = sc.nextInt();
|
||||
int[] randSocks = new int[n];
|
||||
for (int i = 0; i < randSocks.length; i++) {
|
||||
randSocks[i] = sc.nextInt();
|
||||
}
|
||||
sc.close();
|
||||
int pairCounted = pairingSocks(n, randSocks);
|
||||
System.out.println(pairCounted);
|
||||
}
|
||||
|
||||
public static int pairingSocks(int n, int[] randSocks) {
|
||||
int socks = 0;
|
||||
Map<Integer, Integer> pairCount = new HashMap<>();
|
||||
// we are gonna go through the array of integer and check if there is 2 paires
|
||||
for (int x : randSocks) {
|
||||
System.out.print(x + "\t");
|
||||
pairCount.put(x, pairCount.getOrDefault(x, 0) + 1);
|
||||
}
|
||||
// count how many paires there is and return the sum.
|
||||
System.out.println("\n");
|
||||
for (int c : pairCount.values()) {
|
||||
socks = socks + c / 2;
|
||||
}
|
||||
return socks;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Palindrom {
|
||||
public static String reversedStr;
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Scanner sc = new Scanner(System.in);
|
||||
String string = sc.nextLine();
|
||||
System.out.println(revString(string));
|
||||
System.out.println(PalindromFlag(string));
|
||||
sc.close();
|
||||
}
|
||||
|
||||
private static String revString(String str) {
|
||||
reversedStr = "";
|
||||
|
||||
for (int i = str.length() - 1; i >= 0; i--) {
|
||||
char temp = str.charAt(i);
|
||||
reversedStr += temp;
|
||||
}
|
||||
return reversedStr;
|
||||
}
|
||||
|
||||
private static boolean PalindromFlag(String str) {
|
||||
// let's reverse the String first
|
||||
if (str.equals(reversedStr)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* ReversedString
|
||||
*/
|
||||
public class ReversedString {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Please enter your String:");
|
||||
String str = sc.nextLine();
|
||||
System.out.println(revSTR(str));
|
||||
}
|
||||
|
||||
private static String revSTR(String str) {
|
||||
String revString = "";
|
||||
for (int i = str.length() - 1; i >= 0; i--) {
|
||||
char temp = str.charAt(i);
|
||||
revString += temp;
|
||||
}
|
||||
return revString;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user