Hiking around HackerRank — 02

Andrew Chen
6 min readJul 15, 2020

--

07/13/2020

Note:

  1. All problems are published by HackerRank, credit to HackerRank
  2. The solutions are coded by myself, and open source just for study.

Java Arraylist

Java Arraylist, retrieved from https://www.hackerrank.com/challenges/java-arraylist/problem

God, I could not even understand the problem in the beginning…

Well, I assume we need to create different Arraylists and get respective elements in them. In other words, it is an ArrayList of ArrayLists. Is it attainable in Java?

I Googled “ArrayLists for ArrayLists” and found this discussion. It is super helpful. And my code is as following:

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) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
int n, d, q, x, y;
Scanner input = new Scanner(System.in);
n = input.nextInt();

List<List<Integer>> sets = new ArrayList<List<Integer>>();

for (int i = 0; i < n; i++){
ArrayList<Integer> set = new ArrayList<Integer>();
d = input.nextInt();

for (int j = 0; j < d; j++){
set.add(input.nextInt());
}
sets.add(set);
}

q = input.nextInt();
for (int i = 0; i < q; i ++){
x = input.nextInt();
y = input.nextInt();
if (y > sets.get(x-1).size()){
System.out.println("ERROR!");
}
else{
System.out.println(sets.get(x-1).get(y-1));
}
}

input.close();

}
}

Frankly speaking, this solution is not as elegant as I expected, but it worked anyway. Do you have any ideas to improve it?

Java List

Java List, retrieved from https://www.hackerrank.com/challenges/java-list/problem

It should not be very difficult, but I made some mistakes. In the beginning, I forgot to use “LinkedList” and generated some errors. Then I modified my code like this:

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) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner input = new Scanner(System.in);

LinkedList<Integer> list = new LinkedList<Integer>();
int n, q;
String command;
n = input.nextInt();
for (int i = 0; i < n; i++){
int val = input.nextInt();
list.add(val);
}

q = input.nextInt();
for (int i = 0; i < q; i++){
command = input.next();
int index = input.nextInt();
int element = input.nextInt();
if (command.equals("INSERT")){
list.add(index, element);
}
else {
list.remove(index);
}

}
input.close();

for (Integer number: list){
System.out.println(number + " ");
}
}
}

But errors still occur…

I think that’s caused by different commands. You know, when the user wants to delete an element, he/she doesn't need to enter a new element. Now, the new version of code is here:

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) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner input = new Scanner(System.in);

LinkedList<Integer> list = new LinkedList<Integer>();
int n, q;
String command;
n = input.nextInt();
for (int i = 0; i < n; i++){
int val = input.nextInt();
list.add(val);
}

q = input.nextInt();
for (int i = 0; i < q; i++){
command = input.next();
//int index = input.nextInt();
//int element = input.nextInt();
if (command == "Insert"){
int index = input.nextInt();
int element = input.nextInt();
list.add(index, element);
}
else {
int index = input.nextInt();
list.remove(index);
}

}
input.close();

for (Integer number: list){
System.out.print(number + " ");
}
}
}

However, another runtime error! There must be something wrong!

Well, guess what? “==” and “equals” are different. The former is used for address reference, while the latter is used for content check. You can refer to this doc for details.

Now, let’s correct the answer!

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) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner input = new Scanner(System.in);

LinkedList<Integer> list = new LinkedList<Integer>();
int n, q;
String command;
n = input.nextInt();
for (int i = 0; i < n; i++){
int val = input.nextInt();
list.add(val);
}

q = input.nextInt();
for (int i = 0; i < q; i++){
command = input.next();
//int index = input.nextInt();
//int element = input.nextInt();
if (command.equals("Insert")){
int index = input.nextInt();
int element = input.nextInt();
list.add(index, element);
}
else {
int index = input.nextInt();
list.remove(index);
}

}
input.close();

for (Integer number: list){
System.out.print(number + " ");
}
}
}

Success!

Anyway, it is an interesting problem!

7/14 2020

Java HashSet

Java HashSet, retrieved from https://www.hackerrank.com/challenges/java-hashset/problem

It is similar to mathematic sets, but there are some trivial differences. I wanted to check the input strings with conditional statements, but the HackerRanki forum had provided a new perspective. Let’s implement it:

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();
}

//Write your code here
HashSet<String> set = new HashSet<String>();

for (int i = 0; i < t; i ++){
set.add(pair_left[i] + pair_right[i]);
System.out.println(set.size());
}

}
}

Well, it worked but failed some tests. It seems the HashSet would not parse the input as a whole automatically, so we need to modify the format a little bit.

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();
}

//Write your code here
HashSet<String> set = new HashSet<String>();

for (int i = 0; i < t; i ++){
set.add(pair_left[i] + "_" + pair_right[i]);
System.out.println(set.size());
}

}
}

Whoa! It worked! What a nice day!

Conclusion:

  • I skipped some questions. Some of them are easy enough, some of them are too difficult for me. I would try to solve the latter ones with my best.
  • There are usually more than one solution to a problem. If you want to share your solution, that’s awesome!

References:

Difference between == and .equals() method in Java,” GeeksforGeeks, retrieved from https://www.geeksforgeeks.org/difference-equals-method-java/#:~:text=We%20can%20use%20%3D%3D%20operators,of%20values%20in%20the%20objects.

Java List,” HackerRank Forum, retrieved from https://www.hackerrank.com/challenges/java-list/forum

Java HashSet,” HackerRank Forum, retrieved from: https://www.hackerrank.com/challenges/java-hashset/forum

--

--

Andrew Chen
Andrew Chen

Written by Andrew Chen

Software Developer / Opinions are mine

No responses yet