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;
- class Result {
- /*
- * Complete the 'minPrice' function below.
- *
- * The function is expected to return an INTEGER.
- * The function accepts 2D_INTEGER_ARRAY cost as parameter.
- */
- public static int minPrice(List<List<Integer>> cost) {
- int totalCost = 0;
- HashMap<Integer, Integer> minCosts = new HashMap<>();
- HashMap<Integer, Integer> minIndexes = new HashMap<>();
- // System.out.println();
- // System.out.println();
- // System.out.println();
- // System.out.println();
- for(int i = 0; i < cost.size(); i++){
- List<Integer> currentLine = cost.get(i);
- System.out.println(currentLine);
- int minC = Collections.min(currentLine);
- int minI = currentLine.indexOf(minC);
- minCosts.put(i,minC);
- minIndexes.put(i,minI);
- }
- boolean allUnique = false;
- while(!allUnique){
- boolean currentlyUnique = true;
- for(int i = 1; i < cost.size(); i++){
- int previousIdx = minIndexes.get(i-1);
- int currentIdx = minIndexes.get(i);
- List<Integer> originalPrevious = new ArrayList<>(cost.get(i-1));
- List<Integer> originalCurrent = new ArrayList<>(cost.get(i));
- if(currentIdx == previousIdx){
- currentlyUnique = false;
- List<Integer> alteredPrevious = new ArrayList<>(cost.get(i-1));
- List<Integer> alteredCurrent = new ArrayList<>(cost.get(i));
- alteredPrevious.remove(previousIdx);
- alteredCurrent.remove(currentIdx);
- int newPreviousMin = Collections.min(alteredPrevious);
- int newCurrentMin = Collections.min(alteredCurrent);
- // System.out.println(newPreviousMin + " " + newCurrentMin);
- if(newPreviousMin < newCurrentMin){
- // System.out.println(originalPrevious);
- // System.out.println(originalPrevious.indexOf(newPreviousMin));
- minIndexes.put(i-1, originalPrevious.indexOf(newPreviousMin));
- minCosts.put(i-1, newPreviousMin);
- } else {
- minIndexes.put(i, originalCurrent.indexOf(newCurrentMin));
- minCosts.put(i, newCurrentMin);
- }
- }
- }
- if(currentlyUnique){
- allUnique = true;
- }
- }
- for(int i = 0; i < minCosts.size(); i++){
- totalCost += minCosts.get(i);
- }
- return totalCost;
- }
- }
- public class Solution {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement