SHARE
TWEET

Untitled

a guest Dec 7th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  
  13.  
  14.  
  15. class Result {
  16.  
  17.     /*
  18.      * Complete the 'minPrice' function below.
  19.      *
  20.      * The function is expected to return an INTEGER.
  21.      * The function accepts 2D_INTEGER_ARRAY cost as parameter.
  22.      */
  23.  
  24.     public static int minPrice(List<List<Integer>> cost) {
  25.        
  26.         int totalCost = 0;
  27.  
  28.         HashMap<Integer, Integer> minCosts = new HashMap<>();
  29.         HashMap<Integer, Integer> minIndexes = new HashMap<>();
  30.         // System.out.println();
  31.         // System.out.println();
  32.         // System.out.println();
  33.         // System.out.println();
  34.  
  35.         for(int i = 0; i < cost.size(); i++){
  36.             List<Integer> currentLine = cost.get(i);
  37.  
  38.             System.out.println(currentLine);
  39.             int minC = Collections.min(currentLine);
  40.             int minI = currentLine.indexOf(minC);
  41.  
  42.             minCosts.put(i,minC);
  43.             minIndexes.put(i,minI);
  44.  
  45.         }
  46.  
  47.         boolean allUnique = false;
  48.  
  49.         while(!allUnique){
  50.  
  51.             boolean currentlyUnique = true;
  52.  
  53.             for(int i = 1; i < cost.size(); i++){
  54.  
  55.                 int previousIdx = minIndexes.get(i-1);
  56.                 int currentIdx = minIndexes.get(i);
  57.  
  58.                 List<Integer> originalPrevious = new ArrayList<>(cost.get(i-1));
  59.                 List<Integer> originalCurrent = new ArrayList<>(cost.get(i));
  60.  
  61.                 if(currentIdx == previousIdx){
  62.                    
  63.                     currentlyUnique = false;
  64.  
  65.                     List<Integer> alteredPrevious = new ArrayList<>(cost.get(i-1));
  66.                     List<Integer> alteredCurrent = new ArrayList<>(cost.get(i));
  67.  
  68.                     alteredPrevious.remove(previousIdx);
  69.                     alteredCurrent.remove(currentIdx);
  70.  
  71.                     int newPreviousMin = Collections.min(alteredPrevious);
  72.                     int newCurrentMin = Collections.min(alteredCurrent);
  73.                    
  74.                     // System.out.println(newPreviousMin + " " + newCurrentMin);
  75.  
  76.                     if(newPreviousMin < newCurrentMin){
  77.                         // System.out.println(originalPrevious);
  78.                         // System.out.println(originalPrevious.indexOf(newPreviousMin));
  79.                        
  80.                         minIndexes.put(i-1, originalPrevious.indexOf(newPreviousMin));
  81.                         minCosts.put(i-1, newPreviousMin);
  82.  
  83.                     } else {
  84.                         minIndexes.put(i, originalCurrent.indexOf(newCurrentMin));
  85.                         minCosts.put(i, newCurrentMin);
  86.                     }
  87.                 }
  88.             }
  89.  
  90.             if(currentlyUnique){
  91.                 allUnique = true;
  92.             }
  93.         }
  94.  
  95.         for(int i = 0; i < minCosts.size(); i++){
  96.             totalCost += minCosts.get(i);
  97.         }
  98.        
  99.         return totalCost;
  100.     }
  101.  
  102. }
  103.  
  104. public class Solution {
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top