Refactored all solutions
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user