-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
216 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
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 scanner = new Scanner(System.in); | ||
int t = Integer.parseInt(scanner.nextLine()); | ||
for (int i = 0; i < t; i++) { | ||
int n = scanner.nextInt(); | ||
int m = scanner.nextInt(); | ||
int[] arr = new int[n]; | ||
for (int j = 0; j < n; j++) { | ||
arr[j] = scanner.nextInt(); | ||
} | ||
solve(n, m, arr); | ||
} | ||
} | ||
|
||
public static void solve(int n, int m, int[] arr) { | ||
if (solve(n, m, arr, new boolean[n], 0)) { | ||
System.out.println("YES"); | ||
} else { | ||
System.out.println("NO"); | ||
} | ||
} | ||
|
||
public static boolean solve(int n, int m, int[] arr, boolean[] visited, int curr) { | ||
if (curr + m >= n || curr + 1 == n) { | ||
return true; | ||
} | ||
|
||
boolean[] newVisited = new boolean[n]; | ||
for (int i = 0; i < n; i++) { | ||
newVisited[i] = visited[i]; | ||
} | ||
|
||
boolean s = false; | ||
if (!visited[curr + 1] && arr[curr + 1] == 0) { | ||
newVisited[curr + 1] = true; | ||
s = solve(n, m, arr, newVisited, curr + 1); | ||
} | ||
if (s) { | ||
return true; | ||
} | ||
if (m > 1 && arr[curr + m] == 0 && !visited[curr + m]) { | ||
newVisited[curr + m] = true; | ||
s = solve(n, m, arr, newVisited, curr + m); | ||
} | ||
if (s) { | ||
return true; | ||
} | ||
if (curr > 0 && arr[curr - 1] == 0 && !visited[curr - 1]) { | ||
newVisited[curr - 1] = true; | ||
s = solve(n, m, arr, newVisited, curr - 1); | ||
} | ||
return s; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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 scan = new Scanner(System.in); | ||
|
||
int total = scan.nextInt(); | ||
|
||
ArrayList<ArrayList<Integer>> mainlist = new ArrayList<>(); | ||
|
||
for (int i = 0; i < total; i++) { | ||
int size = scan.nextInt(); | ||
ArrayList<Integer> list = new ArrayList(); | ||
|
||
for (int j = 0; j < size; j++) { | ||
int item = scan.nextInt(); | ||
list.add(item); | ||
} | ||
mainlist.add(list); | ||
} | ||
|
||
int totalprint = scan.nextInt(); | ||
for (int k = 0; k < totalprint; k++) { | ||
int row = scan.nextInt(); | ||
int col = scan.nextInt(); | ||
try { | ||
System.out.println(mainlist.get(row - 1).get(col - 1)); | ||
} catch (Exception e) { | ||
System.out.println("ERROR!"); | ||
} | ||
} | ||
scan.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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) { | ||
try { | ||
int no_of_entries = 0; | ||
int i = 0; | ||
String name = null; | ||
int number = 0; | ||
String query = null; | ||
HashMap<String, Integer> phoneBook = new HashMap<String, Integer>(); | ||
BufferedReader b = new BufferedReader(new InputStreamReader( | ||
System.in)); | ||
no_of_entries = Integer.parseInt(b.readLine()); | ||
while (i < no_of_entries) { | ||
name = b.readLine(); | ||
number = Integer.parseInt(b.readLine()); | ||
phoneBook.put(name, number); | ||
i++; | ||
} | ||
while (!(query = b.readLine().trim()).isEmpty()) { | ||
if (phoneBook.containsKey(query)) | ||
System.out.println(query + "=" + phoneBook.get(query)); | ||
else | ||
System.out.println("Not found"); | ||
} | ||
} catch (Exception e) { | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
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) { | ||
final Scanner in = new Scanner(System.in); | ||
final int size = in.nextInt(); | ||
final List<Integer> list = new LinkedList<>(); | ||
for (int i = 0; i < size; i++) { | ||
list.add(in.nextInt()); | ||
} | ||
final int commandCount = in.nextInt(); | ||
for (int i = 0; i < commandCount; i++) { | ||
in.nextLine(); | ||
String command = in.nextLine(); | ||
if (command.equals("Insert")) { | ||
int index = in.nextInt(); | ||
int value = in.nextInt(); | ||
list.add(index, value); | ||
} else { | ||
int index = in.nextInt(); | ||
list.remove(index); | ||
} | ||
} | ||
int count = 0; | ||
for (Integer number : list) { | ||
System.out.print(number); | ||
if (count != list.size() - 1) { | ||
System.out.print(" "); | ||
} | ||
count++; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
import java.text.*; | ||
import java.math.*; | ||
import java.util.regex.*; | ||
|
||
public class Solution { | ||
|
||
public static boolean isBalanced(String s) { | ||
Stack<Character> stack = new Stack<Character>(); | ||
for (int i = 0; i < s.length(); ++i) { | ||
if (s.charAt(i) == '(') stack.push('('); | ||
else if (s.charAt(i) == '{') stack.push('{'); | ||
else if (s.charAt(i) == '[') stack.push('['); | ||
else if (s.charAt(i) == ')') { | ||
if (stack.isEmpty()) return false; | ||
if (stack.pop() != '(') return false; | ||
} else if (s.charAt(i) == '}') { | ||
if (stack.isEmpty()) return false; | ||
if (stack.pop() != '{') return false; | ||
} else if (s.charAt(i) == ']') { | ||
if (stack.isEmpty()) return false; | ||
if (stack.pop() != '[') return false; | ||
} | ||
} | ||
return stack.isEmpty(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
Stack<Character> stack = new Stack<Character>(); | ||
Scanner sc = new Scanner(System.in); | ||
String line; | ||
while (sc.hasNextLine()) { | ||
line = sc.nextLine(); | ||
if (isBalanced(line)) System.out.println("true"); | ||
else System.out.println("false"); | ||
} | ||
} | ||
} |