(Coderbyte) Results for Off Line Minimum - Solução
Have the function OffLineMinimum(strArr) take the strArr parameter being passed
which will be an array of integers ranging from 1...n and the
letter "E" and return the correct subset based on the following rules.
The input will be in the following format:
["I","I","E","I",...,"E",...,"I"] where the I's stand for integers and
the E means take out the smallest integer currently in the whole set.
When finished, your program should return that new set with integers
separated by commas. For example: if strArr is ["5","4","6","E","1","7","E","E","3","2"] then your program should return 4,1,5.
Solução:
import java.util.*;
import java.io.*;
import java.lang.*;
class Function {
String OffLineMinimum(String[] strArr) {
ArrayList<Integer> numeros = new ArrayList<Integer>();
String saida = "";
boolean maisQueUmElemento = false;
for (int i = 0; i < strArr.length; i++) {
if (strArr[i].equals("E")) {
Collections.sort(numeros);
if (maisQueUmElemento) {
saida += ",";
}
saida += String.valueOf(numeros.get(0));
numeros.remove(0);
maisQueUmElemento = true;
}
else {
numeros.add(Integer.parseInt(strArr[i]));
}
}
return saida;
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
Function c = new Function();
System.out.print(c.OffLineMinimum(s.nextLine()));
}
}
Solução:
import java.util.*;
import java.io.*;
import java.lang.*;
class Function {
String OffLineMinimum(String[] strArr) {
ArrayList<Integer> numeros = new ArrayList<Integer>();
String saida = "";
boolean maisQueUmElemento = false;
for (int i = 0; i < strArr.length; i++) {
if (strArr[i].equals("E")) {
Collections.sort(numeros);
if (maisQueUmElemento) {
saida += ",";
}
saida += String.valueOf(numeros.get(0));
numeros.remove(0);
maisQueUmElemento = true;
}
else {
numeros.add(Integer.parseInt(strArr[i]));
}
}
return saida;
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
Function c = new Function();
System.out.print(c.OffLineMinimum(s.nextLine()));
}
}
Comments
Post a Comment