Advertisement
YChalk

DNA Health

Mar 24th, 2022 (edited)
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.59 KB | None | 0 0
  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.function.*;
  8. import java.util.regex.*;
  9. import java.util.stream.*;
  10. import static java.util.stream.Collectors.joining;
  11. import static java.util.stream.Collectors.toList;
  12. import java.util.concurrent.atomic.AtomicInteger;
  13.  
  14.  
  15.  
  16. public class Solution {
  17.     public static void main(String[] args) throws IOException {
  18.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  19.  
  20.         int n = Integer.parseInt(bufferedReader.readLine().trim());
  21.  
  22.         List<String> genes = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
  23.             .collect(toList());
  24.  
  25.         List<Integer> health = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
  26.             .map(Integer::parseInt)
  27.             .collect(toList());
  28.  
  29.         int s = Integer.parseInt(bufferedReader.readLine().trim());
  30.        
  31.         //Map<String, Integer> mappings = mapping(genes, health);
  32.         List<GenePair<String,Integer>> map = zipToPairs(genes, health);
  33.        
  34.         AtomicInteger min = new AtomicInteger(Integer.MAX_VALUE);
  35.         AtomicInteger max = new AtomicInteger(0);
  36.  
  37.         IntStream.range(0, s).forEach(sItr -> {
  38.             try {
  39.                 String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
  40.  
  41.                 int first = Integer.parseInt(firstMultipleInput[0]);
  42.  
  43.                 int last = Integer.parseInt(firstMultipleInput[1]);
  44.  
  45.                 String d = firstMultipleInput[2];
  46.                
  47.                 int weight = weight(genes, health, first, last, d);
  48.                 if (weight < min.intValue()){
  49.                     min.set(weight);
  50.                 }
  51.                 if (weight > max.intValue()){
  52.                     max.set(weight);
  53.                 }
  54.                
  55.             } catch (IOException ex) {
  56.                 throw new RuntimeException(ex);
  57.             }
  58.         });
  59.  
  60.         bufferedReader.close();
  61.        
  62.        
  63.         System.out.println(min + " " + max);
  64.     }
  65.    
  66.     public static int geneOccurence(String strand, String gene){
  67.         int result = 0;
  68.        
  69.         /*
  70.         *did not improve on: 0 1 10 11 12 29  
  71.         */
  72.         if (gene.length() == 1){
  73.             char[] gChar = gene.toCharArray();
  74.             return (int)strand.chars().filter(c -> c == gChar[0]).count();
  75.         }
  76.        
  77.         int index = 0;
  78.        
  79.         while (true) {
  80.             index = strand.indexOf(gene, index);
  81.             if (index == -1){
  82.                 break;
  83.             }
  84.             result++;
  85.             index++;
  86.         }
  87.         return result;
  88.     }
  89.    
  90.     /*
  91.     *map the genes to corresponding health value
  92.     *in weight() method, retrieve the value of each gene
  93.     *does not work due to duplicate values
  94.     */
  95.     /*public static Map<String, Integer> mapping(List<String> genes, List<Integer> health){
  96.         return IntStream.range(0, genes.size()).boxed()
  97.         .collect(Collectors.toMap(genes::get, health::get));
  98.     }*/
  99.    
  100.     public static int weight(List<String> genes, List<Integer> health, int first, int last, String strand){
  101.  
  102.         /*
  103.         *create custom Pair class
  104.         *merge the Genes and Health into List<Pair>
  105.         *by using zipToPairs() method
  106.         *create a stream in the range first - last
  107.         */
  108.         AtomicInteger result = new AtomicInteger(0);
  109.        
  110.         map.stream().skip(first).limit(last-first+1).forEach(p ->{
  111.             int occurence = geneOccurence(strand, p.GENE);
  112.             result.addAndGet(occurence*p.HEALTH);
  113.         });
  114.        
  115.         return result.intValue();
  116.  
  117.  
  118.         /*
  119.         *stream genes and set range between first and last
  120.         *iterate over stream with forEach
  121.         *use index to retrieve corresponding health value
  122.         *passes: 0 1 10 11 12 29
  123.         */
  124.         AtomicInteger index = new AtomicInteger(first);
  125.         AtomicInteger result = new AtomicInteger(0);
  126.        
  127.         genes.stream().skip(first).limit(last-first+1).forEach(gene ->{
  128.                 int occurence = geneOccurence(strand, gene);
  129.                 int weight = health.get(index.intValue());
  130.                 //System.out.println("Occurence: " + occurence);
  131.                 //System.out.println("Weight: " + weight);
  132.                 result.addAndGet(weight*occurence);              
  133.                 index.incrementAndGet();
  134.         });      
  135.  
  136.        
  137.         return result.intValue();
  138.  
  139.  
  140.         /*
  141.         *create subList of genes and health
  142.         *iterate of genes with forEach
  143.         *retrieve corresponding values from health
  144.         *using index
  145.         *passes: 0 1 10 11 12 29  
  146.         */
  147.         AtomicInteger result = new AtomicInteger(0);
  148.         AtomicInteger index = new AtomicInteger(0);
  149.        
  150.         List<String> gSubList = genes.subList(first, last+1);
  151.         List<Integer> hSubList = health.subList(first, last+1);
  152.        
  153.         gSubList.forEach(gene -> {
  154.             int occurence = geneOccurence(strand, gene);
  155.             int weight = hSubList.get(index.intValue());
  156.             result.addAndGet(occurence*weight);
  157.             index.incrementAndGet();
  158.         });
  159.        
  160.         return result.intValue();
  161.        
  162.        
  163.         /*
  164.         *see mappings method above for explanation
  165.         */
  166.         List<String> subList = genes.subList(first, last+1);
  167.         AtomicInteger result = new AtomicInteger(0);
  168.         subList.forEach(gene ->{
  169.             int occurence = geneOccurence(strand, gene);
  170.             int weight = mappings.get(gene);
  171.             result.addAndGet(occurence*weight);
  172.         });
  173.        
  174.         return result.intValue();
  175.        
  176.        
  177.         /*
  178.         *use forEach() to iterate over entire list
  179.         *use index to ignore indexes outside of range
  180.         *and to access relevant value in health List
  181.         *passes: 0 1 10 11 12 29    
  182.         */
  183.         AtomicInteger index = new AtomicInteger(0);
  184.         AtomicInteger result = new AtomicInteger(0);
  185.         genes.forEach(gene -> {
  186.             if (index.intValue() >= first && index.intValue() <= last){
  187.                 int occurence = geneOccurence(strand, gene);
  188.                 int weight = health.get(index.intValue());
  189.                 //System.out.println("Occurence: " + occurence);
  190.                 //System.out.println("Weight: " + weight);
  191.                 result.addAndGet(weight*occurence);                
  192.             }
  193.             index.incrementAndGet();
  194.         });
  195.        
  196.         return result.intValue();
  197.        
  198.         /*
  199.         *use regular for loop
  200.         *to iterate over relevant genes
  201.         *and to retrieve corresponding health
  202.         *too slow
  203.         */
  204.         int result = 0;
  205.         int occurences;
  206.         for (int i = first; i <= last; i++){
  207.             occurences = geneOccurence(strand, genes.get(i));
  208.             result += health.get(i) * occurences;
  209.         }
  210.        
  211.         return result;
  212.     }
  213.  
  214.     public static List<GenePair<String, Integer>> zipToPairs(List<String> as, List<Integer> bs) {
  215.         return IntStream.range(0, as.size())
  216.             .mapToObj(i -> new GenePair<>(as.get(i), bs.get(i)))
  217.             .collect(Collectors.toList());
  218.     }      
  219.    
  220.     public static class GenePair<String, Integer>{
  221.         public final String GENE;
  222.         public final Integer HEALTH;
  223.        
  224.         public GenePair(String gene, Integer health){
  225.             GENE = gene;
  226.             HEALTH = health;
  227.         }
  228.     }
  229. }
  230.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement