Skip to content

Instantly share code, notes, and snippets.

@so77id
Created October 19, 2022 16:13
Show Gist options
  • Save so77id/a5d06df97519c88ace1bcd5414f3d90e to your computer and use it in GitHub Desktop.
Save so77id/a5d06df97519c88ace1bcd5414f3d90e to your computer and use it in GitHub Desktop.
EDA-2022-S2-EV1
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class SinglyLinkedListNode {
public int data;
public SinglyLinkedListNode next;
public SinglyLinkedListNode(int nodeData) {
this.data = nodeData;
this.next = null;
}
}
class SinglyLinkedList {
public SinglyLinkedListNode head;
public SinglyLinkedListNode tail;
public SinglyLinkedList() {
this.head = null;
this.tail = null;
}
public void insertNode(int nodeData) {
SinglyLinkedListNode node = new SinglyLinkedListNode(nodeData);
if (this.head == null) {
this.head = node;
} else {
this.tail.next = node;
}
this.tail = node;
}
}
class SinglyLinkedListPrintHelper {
public static void printList(SinglyLinkedListNode node, String sep, BufferedWriter bufferedWriter) throws IOException {
while (node != null) {
bufferedWriter.write(String.valueOf(node.data));
node = node.next;
if (node != null) {
bufferedWriter.write(sep);
}
}
}
}
class Result {
/*
* Complete the 'sum' function below.
*
* The function is expected to return an INTEGER_SINGLY_LINKED_LIST.
* The function accepts following parameters:
* 1. INTEGER_SINGLY_LINKED_LIST l1
* 2. INTEGER_SINGLY_LINKED_LIST l2
*/
/*
* For your reference:
*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode next;
* }
*
*/
public static SinglyLinkedListNode sum(SinglyLinkedListNode l1, SinglyLinkedListNode l2) {
int residual = 0;
SinglyLinkedList res = new SinglyLinkedList();
for(; l1 != null && l2 != null; l1 = l1.next, l2 = l2.next) {
int sum = l1.data + l2.data + residual;
if (sum > 9) {
residual = 1;
sum %= 10;
} else {
residual = 0;
}
res.insertNode(sum);
}
if(residual > 0) {
res.insertNode(residual);
}
return res.head;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
SinglyLinkedList l1 = new SinglyLinkedList();
int l1Count = Integer.parseInt(bufferedReader.readLine().trim());
IntStream.range(0, l1Count).forEach(i -> {
try {
int l1Item = Integer.parseInt(bufferedReader.readLine().trim());
l1.insertNode(l1Item);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
SinglyLinkedList l2 = new SinglyLinkedList();
int l2Count = Integer.parseInt(bufferedReader.readLine().trim());
IntStream.range(0, l2Count).forEach(i -> {
try {
int l2Item = Integer.parseInt(bufferedReader.readLine().trim());
l2.insertNode(l2Item);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
SinglyLinkedListNode result = Result.sum(l1.head, l2.head);
SinglyLinkedListPrintHelper.printList(result, " ", bufferedWriter);
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'universeHasBalance' function below.
*
* The function is expected to return a BOOLEAN.
* The function accepts following parameters:
* 1. 2D_CHARACTER_ARRAY particlesPairs
* 2. STRING universeOrder
*/
public static boolean universeHasBalance(List<List<Character>> particlesPairs, String universeOrder) {
Map<Character, Character> eliminationRule = new HashMap<>();
Set<Character> actionChars = new HashSet<>();
for(List<Character> pair:particlesPairs) {
eliminationRule.put(pair.get(1), pair.get(0));
actionChars.add(pair.get(0));
actionChars.add(pair.get(1));
}
Stack<Character> stack = new Stack<>();
for(Character c:universeOrder.toCharArray()) {
if(actionChars.contains(c)) {
if(eliminationRule.containsKey(c)) {
if(stack.empty()) return false;
if(stack.peek() == eliminationRule.get(c)) {
stack.pop();
} else {
return false;
}
} else {
stack.push(c);
}
}
}
if(stack.size() > 0)
return false;
return true;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<List<Character>> particlesPairs = new ArrayList<>();
IntStream.range(0, n).forEach(i -> {
try {
particlesPairs.add(
Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(e -> e.charAt(0))
.collect(toList())
);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
String universeOrder = bufferedReader.readLine();
boolean result = Result.universeHasBalance(particlesPairs, universeOrder);
bufferedWriter.write(String.valueOf(result ? 1 : 0));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment