Refactored all solutions

This commit is contained in:
rshaghoulian
2017-04-05 18:42:35 -05:00
parent 5f4c2f4a78
commit decc8e791e
135 changed files with 848 additions and 849 deletions
+3 -2
View File
@@ -25,13 +25,14 @@ class Prime {
if (n < 2) {
return false;
}
for (int i = 2; i <= (int) Math.sqrt(n); i++) {
int sqrt = (int) Math.sqrt(n);
for (int i = 2; i <= sqrt; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
}
public class Solution {
+1 -1
View File
@@ -6,7 +6,7 @@ import java.math.BigDecimal;
import java.util.*;
class Solution {
public static void main(String []args){
public static void main(String []args) {
//Input
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
@@ -8,23 +8,21 @@ 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++) {
while (T-- > 0) {
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");
System.out.println(isSolvable(array, m, 0) ? "YES" : "NO");
}
scan.close();
}
/* 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) {
private static boolean isSolvable(int [] array, int m, int i) {
/* Base Cases */
if (i < 0 || array[i] == 1) {
return false;
@@ -35,8 +33,8 @@ public class Solution {
array[i] = 1; // marks as visited
/* Recursive Cases */
return solvable(array, m, i - 1) ||
solvable(array, m, i + 1) ||
solvable(array, m, i + m);
return isSolvable(array, m, i - 1) ||
isSolvable(array, m, i + 1) ||
isSolvable(array, m, i + m);
}
}
@@ -8,12 +8,10 @@ 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
@@ -5,7 +5,6 @@
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];
@@ -26,7 +26,7 @@ class Player {
}
class Solution {
public static void main(String[] args) {
public static void main(String [] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
@@ -16,8 +16,8 @@ public class test {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
int max = map.size();
int m = scan.nextInt();
int max = 0;
for (int i = 0; i < n; i++) {
/* Remove old value (if necessary) */
@@ -14,21 +14,19 @@ class Printer {
}
public class Solution {
public static void main( String args[] ) {
Printer myPrinter = new Printer();
Integer[] intArray = { 1, 2, 3 };
public static void main(String args[]) {
Printer myPrinter = new Printer();
Integer[] intArray = { 1, 2, 3 };
String[] stringArray = {"Hello", "World"};
myPrinter.printArray(intArray);
myPrinter.printArray(stringArray);
int count = 0;
for (Method method : Printer.class.getDeclaredMethods()) {
String name = method.getName();
if (name.equals("printArray")) {
count++;
}
}
if (count > 1) {
System.out.println("Method overloading is not allowed!");
}
@@ -11,12 +11,12 @@ import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
String [] pair_left = new String[t];
int t = s.nextInt();
String [] pair_left = new String[t];
String [] pair_right = new String[t];
for (int i = 0; i < t; i++) {
pair_left[i] = s.next();
pair_left[i] = s.next();
pair_right[i] = s.next();
}
s.close();
+1 -1
View File
@@ -16,7 +16,7 @@ public class Solution {
list.add(value);
}
/* Perfrom queries on Linked List */
/* Perform queries on Linked List */
int Q = scan.nextInt();
for (int i = 0; i < Q; i++) {
String action = scan.next();
@@ -6,13 +6,13 @@ import java.util.Scanner;
import java.util.PriorityQueue;
import java.util.Comparator;
class Student{
class Student {
private String fname;
private double cgpa;
private int token;
public Student(String fname, double cgpa, int token) {
this.fname = fname;
this.cgpa = cgpa;
this.cgpa = cgpa;
this.token = token;
}
public int getToken() {
@@ -55,7 +55,7 @@ public class Solution {
if (pq.isEmpty()) {
System.out.println("EMPTY");
} else {
while ( ! pq.isEmpty()) {
while (!pq.isEmpty()) {
Student s = pq.remove();
System.out.println(s.getFname());
}
+7 -9
View File
@@ -8,15 +8,15 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class Student{
private int id;
class Student {
private int id;
private String fname;
private double cgpa;
public Student(int id, String fname, double cgpa) {
super();
this.id = id;
this.id = id;
this.fname = fname;
this.cgpa = cgpa;
this.cgpa = cgpa;
}
public int getId() {
return id;
@@ -30,8 +30,8 @@ class Student{
}
public class Solution {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int testCases = Integer.parseInt(scan.nextLine());
List<Student> studentList = new ArrayList<Student>();
@@ -39,14 +39,12 @@ public class Solution {
int id = scan.nextInt();
String fname = scan.next();
double cgpa = scan.nextDouble();
Student st = new Student(id, fname, cgpa);
Student st = new Student(id, fname, cgpa);
studentList.add(st);
}
scan.close();
Collections.sort(studentList, new StudentComparator());
for (Student st: studentList) {
System.out.println(st.getFname());
}
+10 -11
View File
@@ -3,11 +3,12 @@
// HackerRank: hackerrank.com/rshaghoulian
import java.util.Scanner;
import java.util.Stack;
import java.util.HashMap;
import java.util.ArrayDeque;
// ArrayDeque is "likely to be faster than Stack when used as a stack" - Java documentation
class Solution {
public static void main(String[] args) {
/* Create HashMap to match opening brackets with closing brackets */
HashMap<Character, Character> map = new HashMap<>();
@@ -24,21 +25,19 @@ class Solution {
scan.close();
}
public static boolean isBalanced(String expression, HashMap<Character, Character> map) {
/* Odd length strings are not balanced */
private static boolean isBalanced(String expression, HashMap<Character, Character> map) {
if ((expression.length() % 2) != 0) {
return false;
}
Stack<Character> stack = new Stack<>();
return false; // odd length Strings are not balanced
}
ArrayDeque<Character> deque = new ArrayDeque<>(); // use deque as a stack
for (int i = 0; i < expression.length(); i++) {
Character ch = expression.charAt(i);
if (map.containsKey(ch)) {
stack.push(ch);
} else if (stack.isEmpty() || ch != map.get(stack.pop())) {
deque.push(ch);
} else if (deque.isEmpty() || ch != map.get(deque.pop())) {
return false;
}
}
return stack.isEmpty();
return deque.isEmpty();
}
}
@@ -4,12 +4,15 @@
import java.util.Scanner;
public class Solution {
// Our subarray must be contiguous, so we have O(n^2) contiguous subarrays
// Time Complexity: O(n^2)
// Space Complexity: O(1)
public class Solution {
public static void main(String[] args) {
/* Read and save input */
/* Save input */
Scanner scan = new Scanner(System.in);
int size = scan.nextInt();
int size = scan.nextInt();
int [] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = scan.nextInt();
@@ -19,7 +22,6 @@ public class Solution {
System.out.println(negativeSubarrays(array));
}
/* O(n^2) runtime */
private static int negativeSubarrays(int [] array) {
int count = 0;
int sum = 0;
@@ -7,15 +7,13 @@ import java.text.NumberFormat;
import java.util.Locale;
public class Solution {
public static void main(String[] args) {
/* Read input */
Scanner scanner = new Scanner(System.in);
/* Save input */
Scanner scan = new Scanner(System.in);
double payment = scanner.nextDouble();
scanner.close();
scan.close();
/* Create custom Locale for India.
I used the "IANA Language Subtag Registry" to find India's country code */
/* Create custom Locale for India - I used the "IANA Language Subtag Registry" to find India's country code */
Locale indiaLocale = new Locale("en", "IN");
/* Create NumberFormats using Locales */
@@ -4,12 +4,10 @@
import java.util.Scanner;
class Solution{
public static void main(String []args) {
class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
for (int i = 0; i < t; i++) {
try {
long x = scan.nextLong();
@@ -26,8 +24,8 @@ class Solution{
if (x >= Long.MIN_VALUE && x <= Long.MAX_VALUE) {
System.out.println("* long");
}
} catch(Exception e) {
System.out.println(scan.next() +" can't be fitted anywhere.");
} catch (Exception e) {
System.out.println(scan.next() + " can't be fitted anywhere.");
}
}
scan.close();
@@ -5,7 +5,6 @@
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = 1;
@@ -5,14 +5,13 @@
import java.util.Scanner;
class Solution{
public static void main(String []args) {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
for (int i = 0; i < t; i++) {
int a = scan.nextInt();
int b = scan.nextInt();
int n = scan.nextInt();
for (int j = 0; j < n; j++) {
a += b * (int) Math.pow(2, j);
System.out.print(a + " ");
@@ -5,16 +5,15 @@
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("================================");
for (int i = 0; i < 3; i++) {
String s1 = scan.next();
int x = scan.nextInt();
System.out.format("%-15s%03d%n", s1, x);
}
System.out.println("================================");
scan.close();
Scanner scan = new Scanner(System.in);
System.out.println("================================");
for (int i = 0; i < 3; i++) {
String s1 = scan.next();
int x = scan.nextInt();
System.out.format("%-15s%03d%n", s1, x);
}
scan.close();
System.out.println("================================");
}
}
@@ -16,10 +16,10 @@ import java.security.Permission;
class Calculate {
static Scanner scan = new Scanner(System.in);
static Output output = new Output();
static Calculate output = new Calculate();
public static Calculate do_calc() {
return new Calculate();
return output;
}
public int get_int_val() throws IOException, NumberFormatException {
@@ -54,13 +54,64 @@ class Calculate {
return Math.PI * (r * r) * h;
}
static void display (double value) {
System.out.println(value);
}
}
class Output {
void display(double value) {
System.out.format("%.3f%n", value);
}
}
public class Solution {
public static void main(String[] args) {
DoNotTerminate.forbidExit();
try {
Calculate cal = new Calculate();
int T = cal.get_int_val();
while (T--> 0) {
double volume = 0.0;
int ch = cal.get_int_val();
if (ch == 1) {
int a = cal.get_int_val();
volume = Calculate.do_calc().get_volume(a);
} else if (ch == 2) {
int l = cal.get_int_val();
int b = cal.get_int_val();
int h = cal.get_int_val();
volume = Calculate.do_calc().get_volume(l, b, h);
} else if (ch == 3) {
double r = cal.get_double_val();
volume = Calculate.do_calc().get_volume(r);
} else if (ch == 4) {
double r = cal.get_double_val();
double h = cal.get_double_val();
volume = Calculate.do_calc().get_volume(r, h);
}
cal.output.display(volume);
}
} catch (NumberFormatException e) {
System.out.print(e);
} catch (IOException e) {
e.printStackTrace();
} catch (DoNotTerminate.ExitTrappedException e) {
System.out.println("Unsuccessful Termination!!");
}
} // end of main
} //end of Solution
/**
*This class prevents the user form using System.exit(0)
* from terminating the program abnormally.
*
*/
class DoNotTerminate {
public static class ExitTrappedException extends SecurityException {}
public static void forbidExit() {
final SecurityManager securityManager = new SecurityManager() {
@Override
public void checkPermission(Permission permission) {
if (permission.getName().contains("exitVM")) {
throw new ExitTrappedException();
}
}
};
System.setSecurityManager(securityManager);
}
} //end of Do_Not_Terminate
@@ -21,12 +21,12 @@ class MyBook extends Book {
public class Main {
public static void main(String []args) {
//Book new_novel=new Book(); This line prHMain.java:25: error: Book is abstract; cannot be instantiated
// Book new_novel=new Book(); This line prHMain.java:25: error: Book is abstract; cannot be instantiated
Scanner sc = new Scanner(System.in);
String title = sc.nextLine();
MyBook new_novel = new MyBook();
new_novel.setTitle(title);
System.out.println("The title is: "+ new_novel.getTitle());
System.out.println("The title is: " + new_novel.getTitle());
sc.close();
}
}
@@ -16,7 +16,7 @@ class Arithmetic {
class Adder extends Arithmetic {}
public class Solution{
public class Solution {
public static void main(String []args) {
// Create a new Adder object
Adder a = new Adder();
@@ -26,5 +26,5 @@ public class Solution{
// Print the result of 3 calls to Adder's `add(int,int)` method as 3 space-separated integers:
System.out.print(a.add(10,32) + " " + a.add(10,3) + " " + a.add(10,10) + "\n");
}
}
}
@@ -9,10 +9,10 @@ interface AdvancedArithmetic {
}
class MyCalculator implements AdvancedArithmetic {
// Runtime: O(n^(1/2))
// Time Complexity: O(n^(1/2))
// Space Complexity: O(1)
public int divisor_sum(int n) {
int sum = 0;
int sum = 0;
int sqrt = (int) Math.sqrt(n);
for (int i = 1; i <= sqrt; i++) {
if (n % i == 0) { // if "i" is a divisor
@@ -28,7 +28,7 @@ class MyCalculator implements AdvancedArithmetic {
}
class Solution {
public static void main(String []args) {
public static void main(String[] args) {
MyCalculator my_calculator = new MyCalculator();
System.out.print("I implemented: ");
ImplementedInterfaceNames(my_calculator);
+2 -3
View File
@@ -6,15 +6,14 @@ import java.io.*;
import java.util.*;
public class Solution {
/* Runtime: O(n) using a HashMap */
// Time Complexity: O(n) using a HashMap
// Space Complexity: O(n)
static boolean isAnagram(String a, String b) {
if (a == null || b == null || a.length() != b.length()) {
return false;
}
a = a.toLowerCase();
b = b.toLowerCase();
HashMap<Character, Integer> map = new HashMap<>();
/* Fill HashMap with 1st String */
@@ -9,7 +9,7 @@ import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args){
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int testCases = Integer.parseInt(in.nextLine());
while (testCases > 0) {
@@ -5,9 +5,8 @@
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
/* Read input */
/* Save input */
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
int k = scan.nextInt();
@@ -6,7 +6,6 @@ import java.util.Scanner;
/* If a String is equivalent to itself when reversed, it's a palindrome */
public class Solution {
public static void main(String[] args) {
/* Read input */
Scanner scan = new Scanner(System.in);
@@ -5,9 +5,8 @@
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
/* Read input */
/* Save input */
Scanner scan = new Scanner(System.in);
String A = scan.next();
String B = scan.next();
@@ -6,14 +6,12 @@ import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/* If a PatternSyntaxException is not thrown by Pattern.compile,
then the regular expression is valid.
*/
// If a PatternSyntaxException is not thrown by Pattern.compile, the regular expresion is valid
public class Solution {
public static void main(String[] args){
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int testCases = Integer.parseInt(scan.nextLine());
while (testCases-- > 0){
while (testCases-- > 0) {
String pattern = scan.nextLine();
try {
Pattern.compile(pattern);
@@ -7,25 +7,23 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
/* Solution assumes we can't have the symbol "<" as text between tags */
public class Solution{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int testCases = Integer.parseInt(scan.nextLine());
while (testCases-- > 0) {
String line = scan.nextLine();
boolean matchFound = false;
Pattern r = Pattern.compile("<(.+)>([^<]+)</\\1>");
Matcher m = r.matcher(line);
while (m.find()) {
System.out.println(m.group(2));
matchFound = true;
}
if ( ! matchFound) {
if (!matchFound) {
System.out.println("None");
}
}
scan.close();
}
}