Untitled

 avatar
Darin
plain_text
a year ago
1.7 kB
2
Indexable
Never
import java.util.*;
 
 
class UserSolution {
    Map<String, List<Product>> products;
 
 
    void init(int N) {
        products = new HashMap<>();
    }
 
 
    void addProduct(int mPrice, int tagNum, char tagName[][]) {
        Product product = new Product();
        product.price = mPrice;
        String tags[] = new String[tagNum];
        for (int i = 0; i < tagNum; i++) {
            tags[i] = string(tagName[i]);
            products.computeIfAbsent(tags[i], m -> new ArrayList<>()).add(product);
        }
        Arrays.sort(tags);
        for (int i = 0; i < tagNum; i++)
            for (int j = i + 1; j < tagNum; j++)
                for (int k = j + 1; k < tagNum; k++)
                    products.computeIfAbsent(tags[i] + tags[j] + tags[k], m -> new ArrayList<>()).add(product);
    }
 
 
    int buyProduct(char tag1[], char tag2[], char tag3[]) {
        String tags[] = { string(tag1), string(tag2), string(tag3) };
        Arrays.sort(tags);
        String tag = tags[0] + tags[1] + tags[2];
        products.computeIfAbsent(tag, k -> new ArrayList<>()).removeIf(x -> x.isRemoved);
        if (products.get(tag).isEmpty())
            return -1;
        Product min = Collections.min(products.get(tag), (a, b) -> a.price - b.price);
        min.isRemoved = true;
        return min.price;
    }
 
 
    void adjustPrice(char tag1[], int changePrice) {
        for (Product product : products.get(string(tag1)))
            product.price += changePrice;
    }
 
 
    String string(char s[]) {
        int n = -1;
        while (s[++n] != 0);
        return new String(s, 0, n);
    }
}
 
 
class Product {
    int price;
    boolean isRemoved;
}