Added 26 solutions
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
// Author: Rodney Shaghoulian
|
||||
// Github: github.com/rshaghoulian
|
||||
// HackerRank: hackerrank.com/rshaghoulian
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.BitSet;
|
||||
|
||||
public class Solution {
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int N = scan.nextInt();
|
||||
int M = scan.nextInt();
|
||||
BitSet B1 = new BitSet(N);
|
||||
BitSet B2 = new BitSet(N);
|
||||
while (M-- > 0) {
|
||||
String str = scan.next();
|
||||
int a = scan.nextInt();
|
||||
int b = scan.nextInt();
|
||||
switch (str) {
|
||||
case "AND":
|
||||
if (a == 1) {
|
||||
B1.and(B2);
|
||||
} else {
|
||||
B2.and(B1);
|
||||
}
|
||||
break;
|
||||
case "OR":
|
||||
if (a == 1) {
|
||||
B1.or(B2);
|
||||
} else {
|
||||
B2.or(B1);
|
||||
}
|
||||
break;
|
||||
case "XOR":
|
||||
if (a == 1) {
|
||||
B1.xor(B2);
|
||||
} else {
|
||||
B2.xor(B1);
|
||||
}
|
||||
break;
|
||||
case "FLIP":
|
||||
if (a == 1) {
|
||||
B1.flip(b);
|
||||
} else {
|
||||
B2.flip(b);
|
||||
}
|
||||
break;
|
||||
case "SET":
|
||||
if (a == 1) {
|
||||
B1.set(b);
|
||||
} else {
|
||||
B2.set(b);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
System.out.println(B1.cardinality() + " " + B2.cardinality());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
// Author: Rodney Shaghoulian
|
||||
// Github: github.com/rshaghoulian
|
||||
// HackerRank: hackerrank.com/rshaghoulian
|
||||
|
||||
import java.util.*;
|
||||
|
||||
class Checker implements Comparator<Player> {
|
||||
@Override
|
||||
public int compare(Player p1, Player p2) {
|
||||
if (p1.score == p2.score) {
|
||||
return p1.name.compareTo(p2.name);
|
||||
} else {
|
||||
return p2.score - p1.score;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Player{
|
||||
String name;
|
||||
int score;
|
||||
|
||||
Player(String name, int score){
|
||||
this.name = name;
|
||||
this.score = score;
|
||||
}
|
||||
}
|
||||
|
||||
class Solution {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int n = scan.nextInt();
|
||||
|
||||
Player[] player = new Player[n];
|
||||
Checker checker = new Checker();
|
||||
|
||||
for(int i = 0; i < n; i++){
|
||||
player[i] = new Player(scan.next(), scan.nextInt());
|
||||
}
|
||||
scan.close();
|
||||
|
||||
Arrays.sort(player, checker);
|
||||
for(int i = 0; i < player.length; i++){
|
||||
System.out.printf("%s %s\n", player[i].name, player[i].score);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// Author: Rodney Shaghoulian
|
||||
// Github: github.com/rshaghoulian
|
||||
// HackerRank: hackerrank.com/rshaghoulian
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.Deque;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.HashMap;
|
||||
|
||||
// O(n) runtime
|
||||
// O(n) space complexity
|
||||
public class test {
|
||||
public static void main(String[] args) {
|
||||
/* Create our data structures */
|
||||
Deque<Integer> deque = new ArrayDeque<>();
|
||||
HashMap<Integer, Integer> map = new HashMap<>();
|
||||
|
||||
/* Save first m entries */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int n = scan.nextInt();
|
||||
int m = scan.nextInt();
|
||||
for (int i = 0; i < m; i++) {
|
||||
int num = scan.nextInt();
|
||||
deque.add(num);
|
||||
if (map.containsKey(num)) {
|
||||
map.put(num, map.get(num) + 1);
|
||||
} else {
|
||||
map.put(num, 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Loop through remaining n-m entries */
|
||||
int max = map.size();
|
||||
for (int i = m; i < n; i++) {
|
||||
/* Remove old value */
|
||||
int old = deque.remove();
|
||||
if (map.get(old) == 1) {
|
||||
map.remove(old);
|
||||
} else {
|
||||
map.put(old, map.get(old) - 1);
|
||||
}
|
||||
/* Add new value */
|
||||
int num = scan.nextInt();
|
||||
deque.add(num);
|
||||
if (map.containsKey(num)) {
|
||||
map.put(num, map.get(num) + 1);
|
||||
} else {
|
||||
map.put(num, 1);
|
||||
}
|
||||
|
||||
max = Math.max(max, map.size());
|
||||
}
|
||||
System.out.println(max);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// Author: Rodney Shaghoulian
|
||||
// Github: github.com/rshaghoulian
|
||||
// HackerRank: hackerrank.com/rshaghoulian
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
class Printer<T> {
|
||||
void printArray(T[] array) {
|
||||
for (T item : array) {
|
||||
System.out.println(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class Solution {
|
||||
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!");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Author: Rodney Shaghoulian
|
||||
// Github: github.com/rshaghoulian
|
||||
// HackerRank: hackerrank.com/rshaghoulian
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.text.*;
|
||||
import java.math.*;
|
||||
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];
|
||||
String [] pair_right = new String[t];
|
||||
|
||||
for (int i = 0; i < t; i++) {
|
||||
pair_left[i] = s.next();
|
||||
pair_right[i] = s.next();
|
||||
}
|
||||
s.close();
|
||||
|
||||
HashSet<String> set = new HashSet<>(t);
|
||||
for (int i = 0; i < t; i++) {
|
||||
set.add(pair_left[i] + " " + pair_right[i]);
|
||||
System.out.println(set.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// Author: Rodney Shaghoulian
|
||||
// Github: github.com/rshaghoulian
|
||||
// HackerRank: hackerrank.com/rshaghoulian
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class Solution {
|
||||
public static void main(String[] args) {
|
||||
/* Create and fill Linked List of Integers */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int N = scan.nextInt();
|
||||
LinkedList<Integer> list = new LinkedList<>();
|
||||
for (int i = 0; i < N; i++) {
|
||||
int value = scan.nextInt();
|
||||
list.add(value);
|
||||
}
|
||||
|
||||
/* Perfrom queries on Linked List */
|
||||
int Q = scan.nextInt();
|
||||
for (int i = 0; i < Q; i++) {
|
||||
String action = scan.next();
|
||||
if (action.equals("Insert")) {
|
||||
int index = scan.nextInt();
|
||||
int value = scan.nextInt();
|
||||
list.add(index, value);
|
||||
} else { // "Delete"
|
||||
int index = scan.nextInt();
|
||||
list.remove(index);
|
||||
}
|
||||
}
|
||||
scan.close();
|
||||
|
||||
/* Print our updated Linked List */
|
||||
for (Integer num : list) {
|
||||
System.out.print(num + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// Author: Rodney Shaghoulian
|
||||
// Github: github.com/rshaghoulian
|
||||
// HackerRank: hackerrank.com/rshaghoulian
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.HashMap;
|
||||
|
||||
/* BufferedReader is used instead of Scanner since it's faster and
|
||||
won't time out on HackerRank test cases when using Java8 */
|
||||
class Solution {
|
||||
public static void main(String [] args) throws Exception {
|
||||
/* Read input and save as entries in a HashMap */
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
int n = Integer.valueOf(br.readLine());
|
||||
HashMap<String, Integer> map = new HashMap<>();
|
||||
while (n-- > 0) {
|
||||
String name = br.readLine();
|
||||
int phone = Integer.valueOf(br.readLine());
|
||||
map.put(name, phone);
|
||||
}
|
||||
|
||||
/* Read each query and check if its in our HashMap */
|
||||
String s;
|
||||
while((s = br.readLine()) != null) {
|
||||
if (map.containsKey(s)) {
|
||||
System.out.println(s + "=" + map.get(s));
|
||||
} else {
|
||||
System.out.println("Not found");
|
||||
}
|
||||
}
|
||||
|
||||
br.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// Author: Rodney Shaghoulian
|
||||
// Github: github.com/rshaghoulian
|
||||
// HackerRank: hackerrank.com/rshaghoulian
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
|
||||
class Student{
|
||||
private int id;
|
||||
private String fname;
|
||||
private double cgpa;
|
||||
public Student(int id, String fname, double cgpa) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.fname = fname;
|
||||
this.cgpa = cgpa;
|
||||
}
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
public String getFname() {
|
||||
return fname;
|
||||
}
|
||||
public double getCgpa() {
|
||||
return cgpa;
|
||||
}
|
||||
}
|
||||
|
||||
public class Solution {
|
||||
public static void main(String[] args){
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int testCases = Integer.parseInt(scan.nextLine());
|
||||
|
||||
List<Student> studentList = new ArrayList<Student>();
|
||||
while (testCases-- > 0) {
|
||||
int id = scan.nextInt();
|
||||
String fname = scan.next();
|
||||
double cgpa = scan.nextDouble();
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class StudentComparator implements Comparator<Student> {
|
||||
double epsilon = 0.001; // since we shouldn't use "==" with doubles
|
||||
@Override
|
||||
public int compare(Student s1, Student s2) {
|
||||
if (Math.abs(s1.getCgpa() - s2.getCgpa()) > epsilon) {
|
||||
return s1.getCgpa() < s2.getCgpa() ? 1 : -1; // descending order
|
||||
} else if (!s1.getFname().equals(s2.getFname())) {
|
||||
return s1.getFname().compareTo(s2.getFname());
|
||||
} else {
|
||||
return s1.getId() - s2.getId();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// Author: Rodney Shaghoulian
|
||||
// Github: github.com/rshaghoulian
|
||||
// HackerRank: hackerrank.com/rshaghoulian
|
||||
|
||||
import java.util.Scanner;
|
||||
import java.util.Stack;
|
||||
import java.util.HashMap;
|
||||
|
||||
class Solution {
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Create HashMap to match opening brackets with closing brackets */
|
||||
HashMap<Character, Character> map = new HashMap<>();
|
||||
map.put('(', ')');
|
||||
map.put('[', ']');
|
||||
map.put('{', '}');
|
||||
|
||||
/* Test each expression for validity */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
while (scan.hasNext()) {
|
||||
String expression = scan.next();
|
||||
System.out.println(isBalanced(expression, map) ? "true" : "false" );
|
||||
}
|
||||
scan.close();
|
||||
}
|
||||
|
||||
public static boolean isBalanced(String expression, HashMap<Character, Character> map) {
|
||||
/* Odd length strings are not balanced */
|
||||
if ((expression.length() % 2) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Stack<Character> stack = new 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())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return stack.isEmpty();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user