Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- import java.util.concurrent.atomic.AtomicInteger;
- public class Solution {
- public static void main(String[] args) throws IOException {
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
- int n = Integer.parseInt(bufferedReader.readLine().trim());
- List<String> genes = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
- .collect(toList());
- List<Integer> health = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
- .map(Integer::parseInt)
- .collect(toList());
- int s = Integer.parseInt(bufferedReader.readLine().trim());
- //Map<String, Integer> mappings = mapping(genes, health);
- List<GenePair<String,Integer>> map = zipToPairs(genes, health);
- AtomicInteger min = new AtomicInteger(Integer.MAX_VALUE);
- AtomicInteger max = new AtomicInteger(0);
- IntStream.range(0, s).forEach(sItr -> {
- try {
- String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
- int first = Integer.parseInt(firstMultipleInput[0]);
- int last = Integer.parseInt(firstMultipleInput[1]);
- String d = firstMultipleInput[2];
- int weight = weight(genes, health, first, last, d);
- if (weight < min.intValue()){
- min.set(weight);
- }
- if (weight > max.intValue()){
- max.set(weight);
- }
- } catch (IOException ex) {
- throw new RuntimeException(ex);
- }
- });
- bufferedReader.close();
- System.out.println(min + " " + max);
- }
- public static int geneOccurence(String strand, String gene){
- int result = 0;
- /*
- *did not improve on: 0 1 10 11 12 29
- */
- if (gene.length() == 1){
- char[] gChar = gene.toCharArray();
- return (int)strand.chars().filter(c -> c == gChar[0]).count();
- }
- int index = 0;
- while (true) {
- index = strand.indexOf(gene, index);
- if (index == -1){
- break;
- }
- result++;
- index++;
- }
- return result;
- }
- /*
- *map the genes to corresponding health value
- *in weight() method, retrieve the value of each gene
- *does not work due to duplicate values
- */
- /*public static Map<String, Integer> mapping(List<String> genes, List<Integer> health){
- return IntStream.range(0, genes.size()).boxed()
- .collect(Collectors.toMap(genes::get, health::get));
- }*/
- public static int weight(List<String> genes, List<Integer> health, int first, int last, String strand){
- /*
- *create custom Pair class
- *merge the Genes and Health into List<Pair>
- *by using zipToPairs() method
- *create a stream in the range first - last
- */
- AtomicInteger result = new AtomicInteger(0);
- map.stream().skip(first).limit(last-first+1).forEach(p ->{
- int occurence = geneOccurence(strand, p.GENE);
- result.addAndGet(occurence*p.HEALTH);
- });
- return result.intValue();
- /*
- *stream genes and set range between first and last
- *iterate over stream with forEach
- *use index to retrieve corresponding health value
- *passes: 0 1 10 11 12 29
- */
- AtomicInteger index = new AtomicInteger(first);
- AtomicInteger result = new AtomicInteger(0);
- genes.stream().skip(first).limit(last-first+1).forEach(gene ->{
- int occurence = geneOccurence(strand, gene);
- int weight = health.get(index.intValue());
- //System.out.println("Occurence: " + occurence);
- //System.out.println("Weight: " + weight);
- result.addAndGet(weight*occurence);
- index.incrementAndGet();
- });
- return result.intValue();
- /*
- *create subList of genes and health
- *iterate of genes with forEach
- *retrieve corresponding values from health
- *using index
- *passes: 0 1 10 11 12 29
- */
- AtomicInteger result = new AtomicInteger(0);
- AtomicInteger index = new AtomicInteger(0);
- List<String> gSubList = genes.subList(first, last+1);
- List<Integer> hSubList = health.subList(first, last+1);
- gSubList.forEach(gene -> {
- int occurence = geneOccurence(strand, gene);
- int weight = hSubList.get(index.intValue());
- result.addAndGet(occurence*weight);
- index.incrementAndGet();
- });
- return result.intValue();
- /*
- *see mappings method above for explanation
- */
- List<String> subList = genes.subList(first, last+1);
- AtomicInteger result = new AtomicInteger(0);
- subList.forEach(gene ->{
- int occurence = geneOccurence(strand, gene);
- int weight = mappings.get(gene);
- result.addAndGet(occurence*weight);
- });
- return result.intValue();
- /*
- *use forEach() to iterate over entire list
- *use index to ignore indexes outside of range
- *and to access relevant value in health List
- *passes: 0 1 10 11 12 29
- */
- AtomicInteger index = new AtomicInteger(0);
- AtomicInteger result = new AtomicInteger(0);
- genes.forEach(gene -> {
- if (index.intValue() >= first && index.intValue() <= last){
- int occurence = geneOccurence(strand, gene);
- int weight = health.get(index.intValue());
- //System.out.println("Occurence: " + occurence);
- //System.out.println("Weight: " + weight);
- result.addAndGet(weight*occurence);
- }
- index.incrementAndGet();
- });
- return result.intValue();
- /*
- *use regular for loop
- *to iterate over relevant genes
- *and to retrieve corresponding health
- *too slow
- */
- int result = 0;
- int occurences;
- for (int i = first; i <= last; i++){
- occurences = geneOccurence(strand, genes.get(i));
- result += health.get(i) * occurences;
- }
- return result;
- }
- public static List<GenePair<String, Integer>> zipToPairs(List<String> as, List<Integer> bs) {
- return IntStream.range(0, as.size())
- .mapToObj(i -> new GenePair<>(as.get(i), bs.get(i)))
- .collect(Collectors.toList());
- }
- public static class GenePair<String, Integer>{
- public final String GENE;
- public final Integer HEALTH;
- public GenePair(String gene, Integer health){
- GENE = gene;
- HEALTH = health;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement