Added 20 solutions

This commit is contained in:
rshaghoulian
2017-02-16 23:51:56 -06:00
parent 5ac5cfba70
commit 34e66564f9
26 changed files with 859 additions and 57 deletions
@@ -0,0 +1,44 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian
import java.util.Scanner;
import java.util.HashMap;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int T = scan.nextInt();
for (int testCase = 0; testCase < T; testCase++) {
int n = scan.nextInt();
int m = scan.nextInt();
/* Create array of values */
int [] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = scan.nextInt();
}
System.out.println(solvable(array, m, 0) ? "YES" : "NO");
}
}
/* Basically a depth-first search (DFS).
Marks each array value as 1 when visiting (Side-effect: alters array) */
private static boolean solvable(int [] array, int m, int i) {
/* Base Cases */
if (i < 0 || array[i] == 1) {
return false;
} else if (i + 1 >= array.length || i + m >= array.length) {
return true;
}
array[i] = 1; // marks as visited
/* Recursive Cases */
return solvable(array, m, i - 1) ||
solvable(array, m, i + 1) ||
solvable(array, m, i + m);
}
}
@@ -0,0 +1,26 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int [] a = new int[n];
for (int i = 0 ; i < n; i++) {
a[i] = scan.nextInt();
}
scan.close();
// Prints each sequential element in array a
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
@@ -0,0 +1,39 @@
// 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 arr[][] = new int[6][6];
for (int row = 0; row < 6; row++) {
for (int col = 0; col < 6; col++) {
arr[row][col] = scan.nextInt();
}
}
scan.close();
System.out.println(maxHourglass(arr));
}
public static int maxHourglass(int [][] arr) {
int max = Integer.MIN_VALUE;
for (int row = 0; row < 4; row++) {
for (int col = 0; col < 4; col++) {
int sum = findSum(arr, row, col);
max = Math.max(max, sum);
}
}
return max;
}
private static int findSum(int [][] arr, int r, int c) {
int sum = arr[r+0][c+0] + arr[r+0][c+1] + arr[r+0][c+2]
+ arr[r+1][c+1] +
arr[r+2][c+0] + arr[r+2][c+1] + arr[r+2][c+2];
return sum;
}
}
@@ -0,0 +1,42 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian
import java.util.Scanner;
import java.util.ArrayList;
/* We need a 2-D array to solve this problem. We can't use an int[][] since we
don't know the initial dimensions. So, we use ArrayList<ArrayList<Integer>> */
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
/* Save numbers in 2-D ArrayList */
ArrayList<ArrayList<Integer>> list = new ArrayList<>();
for (int row = 0; row < n; row++) {
int d = scan.nextInt();
ArrayList<Integer> numbers = new ArrayList<>();
for (int col = 0; col < d; col++) {
numbers.add(scan.nextInt());
}
list.add(numbers);
}
/* Answer the queries */
int q = scan.nextInt();
for (int i = 0; i < q; i++) {
int x = scan.nextInt();
int y = scan.nextInt();
try {
int num = list.get(x-1).get(y-1);
System.out.println(num);
} catch (IndexOutOfBoundsException e) {
System.out.println("ERROR!");
}
}
scan.close();
}
}
@@ -0,0 +1,37 @@
// Author: Rodney Shaghoulian
// Github: github.com/rshaghoulian
// HackerRank: hackerrank.com/rshaghoulian
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
/* Read and save input */
Scanner scan = new Scanner(System.in);
int size = scan.nextInt();
int [] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = scan.nextInt();
}
scan.close();
System.out.println(negativeSubarrays(array));
}
/* O(n^2) runtime */
private static int negativeSubarrays(int [] array) {
int count = 0;
int sum = 0;
for (int i = 0; i < array.length; i++) {
for (int j = i; j < array.length; j++) {
sum += array[j];
if (sum < 0) {
count++;
}
}
sum = 0;
}
return count;
}
}