Skip to content

Instantly share code, notes, and snippets.

@so77id
Last active April 8, 2021 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save so77id/451b492e9d8204a395af2588cab8fb89 to your computer and use it in GitHub Desktop.
Save so77id/451b492e9d8204a395af2588cab8fb89 to your computer and use it in GitHub Desktop.
soluciones
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 'BatchDelete' function below.
*
* The function is expected to return an INTEGER_SINGLY_LINKED_LIST.
* The function accepts following parameters:
* 1. INTEGER_SINGLY_LINKED_LIST head
* 2. INTEGER m
* 3. INTEGER n
*/
/*
* For your reference:
*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode next;
* }
*
*/
public static SinglyLinkedListNode BatchDelete(SinglyLinkedListNode head, int m, int n) {
// Write your code here
SinglyLinkedListNode tmp = head;
while(tmp != null) {
for(int i=0;i+1<m;i++){
if(tmp.next == null) return head;
tmp = tmp.next;
}
for(int i=0;i<n;i++){
if(tmp.next == null) return head;
tmp.next = tmp.next.next;
}
tmp = tmp.next;
}
return 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 OutputStreamWriter(System.out));
int m = Integer.parseInt(bufferedReader.readLine().trim());
int n = Integer.parseInt(bufferedReader.readLine().trim());
SinglyLinkedList l = new SinglyLinkedList();
int lCount = Integer.parseInt(bufferedReader.readLine().trim());
IntStream.range(0, lCount).forEach(i -> {
try {
int lItem = Integer.parseInt(bufferedReader.readLine().trim());
l.insertNode(lItem);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
SinglyLinkedListNode result = Result.BatchDelete(l.head, m, n);
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 'ForecastTermometer' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts INTEGER_ARRAY T as parameter.
*/
public static List<Integer> ForecastTermometer(List<Integer> T) {
List<Integer> ans = new ArrayList<Integer>();
Stack<Integer> stack = new Stack();
for (int i = T.size() - 1; i >= 0; --i) {
while (!stack.isEmpty() && T.get(i) >= T.get(stack.peek())) stack.pop();
ans.add(stack.isEmpty() ? 0 : stack.peek() - i);
stack.push(i);
}
Collections.reverse(ans);
return ans;
}
}
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<Integer> T = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
List<Integer> result = Result.ForecastTermometer(T);
bufferedWriter.write(
result.stream()
.map(Object::toString)
.collect(joining(" "))
+ "\n"
);
bufferedReader.close();
bufferedWriter.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment