|
| 1 | +package problems.hard; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.util.stream.Collectors; |
| 5 | + |
| 6 | +/** |
| 7 | + * Why Did you create this class? what does it do? |
| 8 | + */ |
| 9 | +public class NumberOfAtoms { |
| 10 | + |
| 11 | + public static void main(String[] args) { |
| 12 | + System.out.println(countOfAtoms("K4(ON(SO3)2)2")); |
| 13 | + } |
| 14 | + |
| 15 | + // 2(2(3OS)NO)4K |
| 16 | + static public String countOfAtoms(String formula) { |
| 17 | + if (formula == null || formula.length() == 0) |
| 18 | + return ""; |
| 19 | + StringBuilder builder = new StringBuilder(formula).reverse(); |
| 20 | + Map<String, Integer> map = new HashMap<>(); |
| 21 | + Stack<Integer> stack = new Stack<>(); |
| 22 | + String elem = ""; |
| 23 | + int sum = 1; |
| 24 | + for (int i = 0; i < builder.length(); i++) { |
| 25 | + char c = builder.charAt(i); |
| 26 | + if (Character.isDigit(c)) { |
| 27 | + int j = i; |
| 28 | + while (j < builder.length() && Character.isDigit(builder.charAt(j))) |
| 29 | + j++; |
| 30 | + if (j != i) { |
| 31 | + stack.add(Integer.parseInt(builder.substring(i, j))); |
| 32 | + sum *= stack.peek(); |
| 33 | + j = i = 1; |
| 34 | + } |
| 35 | + } else if (c == '(') { |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + } |
| 40 | + return map.keySet().stream().sorted().map(s -> s + map.get(s)).collect(Collectors.joining()); |
| 41 | + } |
| 42 | + |
| 43 | + private static int getNextDigit(StringBuilder builder, int i) { |
| 44 | + |
| 45 | + return 1; |
| 46 | + } |
| 47 | + |
| 48 | + // public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) { |
| 49 | + // if (price.size() == 0 || needs.size() == 0) |
| 50 | + // return 0; |
| 51 | + // int actual = 0; |
| 52 | + // for (int i = 0; i < needs.size(); i++) { |
| 53 | + // actual += needs.get(i) * price.get(i); |
| 54 | + // } |
| 55 | + // int mini = -1; |
| 56 | + // int result = 0; |
| 57 | + // for (int i = 0; i < special.size(); i++) { |
| 58 | + // List<Integer> offer = special.get(i); |
| 59 | + // int offerCost = 0; |
| 60 | + // int actualCost = 0; |
| 61 | + // for (int j = 0; j < needs.size(); j++) { |
| 62 | + // if (offer.get(i) > needs.get(i)) { |
| 63 | + // offerCost = 0; |
| 64 | + // break; |
| 65 | + // } |
| 66 | + // offerCost += offer.get(i) * needs.get(i); |
| 67 | + // actualCost += price.get(i) * needs.get(i); |
| 68 | + // } |
| 69 | + // |
| 70 | + // } |
| 71 | + // return result; |
| 72 | + // } |
| 73 | + |
| 74 | + public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) { |
| 75 | + if (needs.size() == 0) |
| 76 | + return 0; |
| 77 | + int sumNeed = 0; |
| 78 | + int sumCost = 0; |
| 79 | + for (int i = 0; i < needs.size(); i++) { |
| 80 | + sumNeed += needs.get(i); |
| 81 | + sumCost += price.get(i) * needs.get(i); |
| 82 | + } |
| 83 | + if (sumNeed == 0) |
| 84 | + return 0; |
| 85 | + int total = 0; |
| 86 | + for (List<Integer> list : special) { |
| 87 | + int offerCost = 0; |
| 88 | + int actualCost = 0; |
| 89 | + List<Integer> afterOffer = new ArrayList<>(); |
| 90 | + for (int i = 0; i < list.size(); i++) { |
| 91 | + if (list.get(i) > needs.get(i)) { |
| 92 | + offerCost = actualCost = 0; |
| 93 | + break; |
| 94 | + } |
| 95 | + offerCost += needs.get(i) * list.get(i); |
| 96 | + actualCost += needs.get(i) * price.get(i); |
| 97 | + afterOffer.set(i, needs.get(i) - list.get(i)); |
| 98 | + } |
| 99 | + if (offerCost < actualCost) { |
| 100 | + int useOfferCost = shoppingOffers(price, special, afterOffer); |
| 101 | + } else { |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + } |
| 106 | + return total; |
| 107 | + |
| 108 | + } |
| 109 | +} |
0 commit comments