Added 10 solutions
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Solution {
|
||||
public static void main(String [] args) {
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int size = scan.nextInt();
|
||||
long sum = 0; // use long to prevent integer overflow
|
||||
|
||||
@@ -14,7 +14,7 @@ import java.util.Scanner;
|
||||
// and a delete.
|
||||
|
||||
public class Solution {
|
||||
public static void main(String [] args) {
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
String s = scan.next();
|
||||
String t = scan.next();
|
||||
|
||||
@@ -7,7 +7,7 @@ import java.util.Scanner;
|
||||
// Time Complexity: O(m + n)
|
||||
// Space Complexity: O(1)
|
||||
public class Solution {
|
||||
public static void main(String [] args) {
|
||||
public static void main(String[] args) {
|
||||
/* Save input */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int s = scan.nextInt();
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Solution {
|
||||
public static void main(String [] args) {
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int i = scan.nextInt();
|
||||
int j = scan.nextInt();
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.util.Scanner;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Solution {
|
||||
public static void main(String [] args) {
|
||||
public static void main(String[] args) {
|
||||
/* Save input */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int n = scan.nextInt();
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
// Author: Rodney Shaghoulian
|
||||
// Github: github.com/rshaghoulian
|
||||
// HackerRank: hackerrank.com/rshaghoulian
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.Arrays;
|
||||
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(n) to save the input.
|
||||
// You can reduce the space complexity to O(1) by omitting the array and
|
||||
// working with the input directly, but it will make the code less modular
|
||||
|
||||
public class Solution {
|
||||
static int cakeCandles(int[] candles) {
|
||||
int tallest = 0;
|
||||
int count = 0;
|
||||
for (int candle : candles) {
|
||||
if (candle > tallest) {
|
||||
tallest = candle;
|
||||
count = 1;
|
||||
} else if (tallest == candle) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Save input */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int n = scan.nextInt();
|
||||
int[] candles = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
candles[i] = scan.nextInt();
|
||||
}
|
||||
scan.close();
|
||||
|
||||
/* Get num candles blown out */
|
||||
int result = cakeCandles(candles);
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// Author: Rodney Shaghoulian
|
||||
// Github: github.com/rshaghoulian
|
||||
// HackerRank: hackerrank.com/rshaghoulian
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
// Idea: Use a "sliding window" of "m" squares. Each time we add a square to the right, remove one from the left.
|
||||
|
||||
// Time Complexity: O(n)
|
||||
// Space Complexity: O(1) (other than saving input)
|
||||
public class Solution {
|
||||
static int solve(int n, int[] square, int d, int m) {
|
||||
int sum = 0;
|
||||
int numWays = 0;
|
||||
|
||||
/* Test first set of "m" chocolates */
|
||||
for (int i = 0; i < m; i++) {
|
||||
sum += square[i];
|
||||
}
|
||||
if (sum == d) {
|
||||
numWays++;
|
||||
}
|
||||
|
||||
/* Test remaining sets of "m" chocolates */
|
||||
for (int i = m; i < square.length; i++) {
|
||||
sum += square[i] - square[i-m]; // add chocolate, remove chocolate
|
||||
if (sum == d) {
|
||||
numWays++;
|
||||
}
|
||||
}
|
||||
|
||||
return numWays;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Save input */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int n = scan.nextInt();
|
||||
int[] square = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
square[i] = scan.nextInt();
|
||||
}
|
||||
int d = scan.nextInt();
|
||||
int m = scan.nextInt();
|
||||
scan.close();
|
||||
|
||||
/* Calculate result */
|
||||
int result = solve(n, square, d, m);
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
// Author: Rodney Shaghoulian
|
||||
// Github: github.com/rshaghoulian
|
||||
// HackerRank: hackerrank.com/rshaghoulian
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
// Time complexity: O(n)
|
||||
public class Solution {
|
||||
public static void main(String[] args) {
|
||||
/* Save input */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int n = scan.nextInt();
|
||||
int[] scores = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
scores[i] = scan.nextInt();
|
||||
}
|
||||
scan.close();
|
||||
|
||||
/* Calculate answer */
|
||||
printRecords(scores);
|
||||
}
|
||||
|
||||
static void printRecords(int[] scores){
|
||||
int min = scores[0];
|
||||
int max = scores[0];
|
||||
int higherScore = 0;
|
||||
int lowerScore = 0;
|
||||
for (int score: scores) {
|
||||
if (score > max) {
|
||||
max = score;
|
||||
higherScore++;
|
||||
}
|
||||
if (score < min) {
|
||||
min = score;
|
||||
lowerScore++;
|
||||
}
|
||||
}
|
||||
System.out.print(higherScore + " " + lowerScore);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Author: Rodney Shaghoulian
|
||||
// Github: github.com/rshaghoulian
|
||||
// HackerRank: hackerrank.com/rshaghoulian
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Solution {
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int q = scan.nextInt();
|
||||
while (q-- > 0) {
|
||||
int x = scan.nextInt();
|
||||
int y = scan.nextInt();
|
||||
int z = scan.nextInt();
|
||||
System.out.println(solve(x, y, z));
|
||||
}
|
||||
scan.close();
|
||||
}
|
||||
|
||||
private static String solve(int x, int y, int z) {
|
||||
int A_to_C = Math.abs(z - x);
|
||||
int B_to_C = Math.abs(z - y);
|
||||
if (A_to_C < B_to_C) {
|
||||
return "Cat A";
|
||||
} else if (A_to_C > B_to_C) {
|
||||
return "Cat B";
|
||||
} else {
|
||||
return "Mouse C";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// Author: Rodney Shaghoulian
|
||||
// Github: github.com/rshaghoulian
|
||||
// HackerRank: hackerrank.com/rshaghoulian
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
// Time Complexity: O(n + m)
|
||||
// Space Complexity: O(1) (other than storing input)
|
||||
public class Solution {
|
||||
public static void main(String[] args) {
|
||||
/* Save input - discard duplicates in leaderboard */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int n = scan.nextInt();
|
||||
int lastScore = -1;
|
||||
int[] scores = new int[n];
|
||||
int index = 0;
|
||||
while (n-- > 0) {
|
||||
int currScore = scan.nextInt();
|
||||
if (currScore != lastScore) {
|
||||
scores[index] = currScore;
|
||||
index++;
|
||||
}
|
||||
lastScore = currScore;
|
||||
}
|
||||
int m = scan.nextInt();
|
||||
int[] alice = new int[m];
|
||||
for (int i = 0; i < m; i++) {
|
||||
alice[i] = scan.nextInt();
|
||||
}
|
||||
scan.close();
|
||||
|
||||
/* Print ranks */
|
||||
int i = index - 1;
|
||||
for (int aliceScore : alice) {
|
||||
while (i >= 0) {
|
||||
if (aliceScore < scores[i]) {
|
||||
System.out.println(i + 2); // add 2 to get correct rank
|
||||
break;
|
||||
}
|
||||
i--;
|
||||
}
|
||||
if (i < 0) { // if true, each remaining aliceScore is highest score
|
||||
System.out.println(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Solution {
|
||||
public static void main(String [] args) {
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int a0 = scan.nextInt();
|
||||
int a1 = scan.nextInt();
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
// Author: Rodney Shaghoulian
|
||||
// Github: github.com/rshaghoulian
|
||||
// HackerRank: hackerrank.com/rshaghoulian
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
// Idea: Keep track of your "level", which is how far above or below sea level you are.
|
||||
// If you ever take a step up to sea level, you just completed walking through a valley.
|
||||
|
||||
// Time Complexity: O(n)
|
||||
public class Solution {
|
||||
public static void main(String[] args) {
|
||||
/* Save input */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
scan.nextLine(); // gets rid of 1st line
|
||||
String str = scan.nextLine(); // String is of size "n"
|
||||
scan.close();
|
||||
|
||||
System.out.println(countValleys(str));
|
||||
}
|
||||
|
||||
private static int countValleys(String str) {
|
||||
int level = 0;
|
||||
int numValleys = 0;
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
char ch = str.charAt(i);
|
||||
if (ch == 'U') {
|
||||
level++;
|
||||
if (level == 0) {
|
||||
numValleys++; // since we just stepped up to sea level
|
||||
}
|
||||
} else if (ch == 'D') {
|
||||
level--;
|
||||
}
|
||||
}
|
||||
return numValleys;
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ public class Solution {
|
||||
|
||||
private static final int letters = 26;
|
||||
|
||||
public static void main(String [] args) {
|
||||
public static void main(String[] args) {
|
||||
/* Save input */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int [] height = new int[letters];
|
||||
|
||||
@@ -32,7 +32,7 @@ this complement bucket can be paired with our original number to create a sum di
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Solution {
|
||||
public static void main(String [] args) {
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int n = scan.nextInt();
|
||||
int k = scan.nextInt();
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
// Author: Rodney Shaghoulian
|
||||
// Github: github.com/rshaghoulian
|
||||
// HackerRank: hackerrank.com/rshaghoulian
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
// Explanation:
|
||||
// - The last page is n/2 page turns away.
|
||||
// - It takes p/2 pages to get to the p-th page (from the front)
|
||||
// - It takes n/2 - p/2 pages to get to the p-th page (from the back)
|
||||
// - The minimum of p/2 and (n/2 - p/2) is the minimum number of pages Brie must turn
|
||||
|
||||
// Time complexity: O(1)
|
||||
// Space complexity: O(1)
|
||||
public class Solution {
|
||||
static int solve (int n, int p) {
|
||||
int pagesFromFront = p/2;
|
||||
int pagesFromBack = n/2 - p/2;
|
||||
return Math.min(pagesFromFront, pagesFromBack);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int n = scan.nextInt();
|
||||
int p = scan.nextInt();
|
||||
scan.close();
|
||||
|
||||
int result = solve(n, p);
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// Author: Rodney Shaghoulian
|
||||
// Github: github.com/rshaghoulian
|
||||
// HackerRank: hackerrank.com/rshaghoulian
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.Arrays;
|
||||
|
||||
// Algorithm:
|
||||
// 1. Sort both arrays.
|
||||
// 2. Linearly walk through both arrays (at the same time).
|
||||
// If the combined price is unaffordable, find a cheaper harddrive (j--).
|
||||
// If the combined price is too low, update "max", and find a more expensive keyboard (i++)
|
||||
|
||||
// Time complexity: O(n log n + m log m)
|
||||
public class Solution {
|
||||
static int getMoneySpent(int[] keyboards, int[] drives, int s) {
|
||||
Arrays.sort(keyboards); // O(n log n) time complexity
|
||||
Arrays.sort(drives); // O(m log m) time complexity
|
||||
int max = -1;
|
||||
int i = 0;
|
||||
int j = drives.length - 1;
|
||||
while (i < keyboards.length && j >= 0) { // O(n + m) time complexity
|
||||
int cost = keyboards[i] + drives[j];
|
||||
if (cost > s) {
|
||||
j--; // look for a cheaper hard drive
|
||||
} else {
|
||||
if (cost > max) {
|
||||
max = cost;
|
||||
}
|
||||
i++; // look for a more expensive keyboard
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Save input */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int s = scan.nextInt();
|
||||
int n = scan.nextInt();
|
||||
int m = scan.nextInt();
|
||||
int[] keyboards = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
keyboards[i] = scan.nextInt();
|
||||
}
|
||||
int[] drives = new int[m];
|
||||
for (int i = 0; i < m; i++) {
|
||||
drives[i] = scan.nextInt();
|
||||
}
|
||||
scan.close();
|
||||
|
||||
/* Calculate result */
|
||||
int moneySpent = getMoneySpent(keyboards, drives, s);
|
||||
System.out.println(moneySpent);
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ public class Solution {
|
||||
|
||||
private static final int size = 100;
|
||||
|
||||
public static void main(String [] args) {
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int n = scan.nextInt();
|
||||
int [] counts = new int[size + 1]; // could alternatively use a HashMap
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Solution {
|
||||
public static void main(String [] args) {
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int n = scan.nextInt();
|
||||
while (n-- > 0) {
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Solution {
|
||||
public static void main(String [] args) {
|
||||
public static void main(String[] args) {
|
||||
int E = 100;
|
||||
/* Save input */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
@@ -13,7 +13,7 @@ import java.util.Scanner;
|
||||
// If the distance between the kangaroos (x2 - x1) is divisible by how much closer they get in each step (v2 - v1), then they will land at the same location at the same time. Otherwise, 1 kangaroo will jump over the other and they will never be at the same location at the same time.
|
||||
|
||||
public class Solution {
|
||||
public static void main(String [] args) {
|
||||
public static void main(String[] args) {
|
||||
/* Save input */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int x1 = scan.nextInt();
|
||||
|
||||
@@ -25,7 +25,7 @@ Space complexity: O(1)
|
||||
*/
|
||||
|
||||
public class Solution {
|
||||
public static void main(String [] args) {
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int T = scan.nextInt();
|
||||
while (T-- > 0) {
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
// Author: Rodney Shaghoulian
|
||||
// Github: github.com/rshaghoulian
|
||||
// HackerRank: hackerrank.com/rshaghoulian
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
// Time Complexity: O(n)
|
||||
public class Solution {
|
||||
|
||||
static final int NUM_TYPES = 5;
|
||||
|
||||
static int migratoryBirds(int[] birds) {
|
||||
/* Get counts of each type */
|
||||
int[] count = new int[NUM_TYPES + 1];
|
||||
for (int num : birds) {
|
||||
count[num]++;
|
||||
}
|
||||
|
||||
/* Find max */
|
||||
int maxIndex = 1;
|
||||
for (int i = 0; i < count.length; i++) {
|
||||
if (count[i] > count[maxIndex]) {
|
||||
maxIndex = i;
|
||||
}
|
||||
}
|
||||
return maxIndex;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Save input */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int n = scan.nextInt();
|
||||
int[] birds = new int[n];
|
||||
for (int i = 0; i < n; i++){
|
||||
birds[i] = scan.nextInt();
|
||||
}
|
||||
scan.close();
|
||||
|
||||
/* Calculate result */
|
||||
int result = migratoryBirds(birds);
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// Author: Rodney Shaghoulian
|
||||
// Github: github.com/rshaghoulian
|
||||
// HackerRank: hackerrank.com/rshaghoulian
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.HashMap;
|
||||
|
||||
// Main trick: Use a HashMap<Integer, Integer> that maps from "value" to "index"
|
||||
// to keep track of of the largest index for each value that we've
|
||||
// seen so far as we loop through array
|
||||
|
||||
// Time Complexity: O(n)
|
||||
// Space Compleixty: O(n)
|
||||
public class Solution {
|
||||
public static void main(String[] args) {
|
||||
/* Save input */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int n = scan.nextInt();
|
||||
int[] array = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
array[i] = scan.nextInt();
|
||||
}
|
||||
scan.close();
|
||||
|
||||
/* Calculate + print result */
|
||||
System.out.println(solve(array));
|
||||
}
|
||||
|
||||
private static int solve(int[] array) {
|
||||
HashMap<Integer, Integer> map = new HashMap<>();
|
||||
int minDistance = Integer.MAX_VALUE;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (map.containsKey(array[i])) {
|
||||
int prevIndex = map.get(array[i]);
|
||||
int currDistance = i - prevIndex;
|
||||
minDistance = Math.min(minDistance, currDistance);
|
||||
}
|
||||
map.put(array[i], i);
|
||||
}
|
||||
return minDistance == Integer.MAX_VALUE ? -1 : minDistance;
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ since prisoners are counted from 1 to N while modular arithmetic is counted from
|
||||
*/
|
||||
|
||||
public class Solution {
|
||||
public static void main(String [] args) {
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int T = scan.nextInt();
|
||||
while (T-- > 0) {
|
||||
|
||||
@@ -10,7 +10,7 @@ import java.util.Scanner;
|
||||
// - We need find a y where p(p(y)) = x. This equation can be rewritten as y = p_inverse(p_inverse(x)), which is the version of the equation we use to calculate and print "y".
|
||||
|
||||
public class Solution {
|
||||
public static void main(String [] args) {
|
||||
public static void main(String[] args) {
|
||||
/* Create function: p_inverse */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int n = scan.nextInt();
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Solution {
|
||||
public static void main(String [] args) {
|
||||
public static void main(String[] args) {
|
||||
/* Save input */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int a = scan.nextInt();
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Solution {
|
||||
public static void main(String [] args) {
|
||||
public static void main(String[] args) {
|
||||
/* Save input */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int n = scan.nextInt();
|
||||
|
||||
@@ -6,7 +6,7 @@ import java.util.Scanner;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Solution {
|
||||
public static void main(String [] args) {
|
||||
public static void main(String[] args) {
|
||||
/* Save input */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int n = scan.nextInt();
|
||||
|
||||
@@ -8,7 +8,7 @@ import java.util.Arrays;
|
||||
// Time Complexity: O(N + T)
|
||||
// Space Complexity: O(N + T)
|
||||
public class Solution {
|
||||
public static void main(String [] args) {
|
||||
public static void main(String[] args) {
|
||||
/* Save input */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int T = scan.nextInt();
|
||||
|
||||
Reference in New Issue
Block a user