Skip to content

Commit

Permalink
5 more problems solved
Browse files Browse the repository at this point in the history
  • Loading branch information
h-ssiqueira committed Jul 19, 2022
1 parent 7a0fb64 commit b61ff84
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Java/java/ArrayLists.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.io.*;
import java.util.*;

public class ArrayLists {

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 in = new Scanner(System.in);
int n,m,x,y;
ArrayList<ArrayList<Integer>> l = new ArrayList<>();
n = in.nextInt();
for(int i = 0; i < n; i++){
ArrayList<Integer> l1 = new ArrayList<>();
m = in.nextInt();
for(int j = 0; j < m; j++)
l1.add(in.nextInt());
l.add(l1);
}
n = in.nextInt();
for(int i = 0; i < n; i++){
x = in.nextInt();
y = in.nextInt();
try{
System.out.println(l.get(x-1).get(y-1));
}catch(Exception e){
System.out.println("ERROR!");
}
}
}
}
29 changes: 29 additions & 0 deletions Java/java/Array_1D2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.*;

public class Array_1D2 {

public static boolean canWin(int pos, int leap, int[] game) {
// Return true if you can win the game; otherwise, return false.
if(pos < 0 || game[pos] == 1) return false;
if(pos + leap > game.length-1 || pos == game.length-1) return true;
game[pos] = 1;
return canWin(pos-1,leap,game) || canWin(pos+1,leap,game) || canWin(pos+leap,leap,game);
}

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int q = scan.nextInt();
while (q-- > 0) {
int n = scan.nextInt();
int leap = scan.nextInt();

int[] game = new int[n];
for (int i = 0; i < n; i++) {
game[i] = scan.nextInt();
}

System.out.println( (canWin(0, leap, game)) ? "YES" : "NO" );
}
scan.close();
}
}
28 changes: 28 additions & 0 deletions Java/java/Hashsets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Hashsets {

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> hs = new HashSet<>(t);
for(int i = 0; i < t; i++){
hs.add(pair_left[i]+", "+pair_right[i]);
System.out.println(hs.size());
}

}
}
27 changes: 27 additions & 0 deletions Java/java/Lists.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.io.*;
import java.util.*;

public class Lists {

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 in = new Scanner(System.in);
int len = in.nextInt();
LinkedList<Integer> list = new LinkedList<>();
for(int i = 0; i < len; i++)
list.add(in.nextInt());
int j = in.nextInt();
for(int i = 0; i < j; i++){
switch(in.next()){
case "Insert":
list.add(in.nextInt(), in.nextInt());
break;
case "Delete":
list.remove(in.nextInt());
break;
}
}
for(Integer i:list)
System.out.print(i+" ");
}
}
24 changes: 24 additions & 0 deletions Java/java/Subarray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.io.*;
import java.util.*;

public class Subarray {

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);
Integer n = input.nextInt();
Integer[] array = new Integer[n];
for(int i = n-1; i >= 0; i--)
array[i] = input.nextInt();
Integer negatives = 0;
for(int i = 0; i < n; i++){
int total = 0;
for(int j = i; j < n; j++){
total = array[j] + total;
if(total < 0)
negatives++;
}
}
System.out.println(negatives);
}
}
Binary file added Java/pdf/java-1d-array.pdf
Binary file not shown.
Binary file added Java/pdf/java-arraylist.pdf
Binary file not shown.
Binary file added Java/pdf/java-hashset.pdf
Binary file not shown.
Binary file added Java/pdf/java-list.pdf
Binary file not shown.
Binary file added Java/pdf/java-negative-subarray.pdf
Binary file not shown.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,12 @@ Using:

### Data Structures
* [Array_1D](Java/pdf/java-1d-array-introduction.pdf) - [Solution](Java/java/Array_1D.java)
* [Array_1D2](Java/pdf/java-1d-array.pdf) - [Solution](Java/java/Array_1D2.java)
* [ArrayLists](Java/pdf/java-arraylist.pdf) - [Solution](Java/java/ArrayLists.java)
* [Generics](Java/pdf/java-generics.pdf) - [Solution](Java/java/Generics.java)
* [Hashsets](Java/pdf/java-hashset.pdf) - [Solution](Java/java/Hashsets.java)
* [Lists](Java/pdf/java-list.pdf) - [Solution](Java/java/Lists.java)
* [Subarray](Java/pdf/java-negative-subarray.pdf) - [Solution](Java/java/Subarray.java)

### Exception Handling
* [Exception_handling](Java/pdf/java-exception-handling.pdf) - [Solution](Java/java/Exception_handling.java)
Expand Down

0 comments on commit b61ff84

Please sign in to comment.