Untitled

 avatar
Darin
plain_text
a year ago
2.0 kB
2
Indexable
Never
import java.util.*;

class Solution {
    static class Product {
        int price;
        boolean isbuy;
    }

    Product[] prd = new Product[30010];
    Map<String, List<Integer>> umap = new HashMap<>();
    int tagID;

    void init(int N) {
        umap.clear();
        tagID = 0;
    }

    void addProduct(int mPrice, int tagNum, String[] tagName) {
        prd[tagID] = new Product();
        prd[tagID].price = mPrice;
        prd[tagID].isbuy = false;
        List<String> tags = new ArrayList<>();
        for (int i = 0; i < tagNum; i++) {
            tags.add(tagName[i]);
            umap.computeIfAbsent(tagName[i], k -> new ArrayList<>()).add(tagID);
        }
        Collections.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++) {
                    String tag3 = tags.get(i) + " " + tags.get(j) + " " + tags.get(k);
                    umap.computeIfAbsent(tag3, k -> new ArrayList<>()).add(tagID);
                }
            }
        }
        tagID++;
    }

    int buyProduct(String tag1, String tag2, String tag3) {
        List<String> tags = new ArrayList<>();
        tags.add(tag1);
        tags.add(tag2);
        tags.add(tag3);
        Collections.sort(tags);
        String longtag = tags.get(0) + " " + tags.get(1) + " " + tags.get(2);
        int isFind = -1;
        for (int it : umap.getOrDefault(longtag, new ArrayList<>())) {
            if (!prd[it].isbuy) {
                if (isFind == -1 || prd[it].price < prd[isFind].price) {
                    isFind = it;
                }
            }
        }
        if (isFind == -1)
            return -1;
        else {
            prd[isFind].isbuy = true;
            return prd[isFind].price;
        }
    }

    void adjustPrice(String tag1, int changePrice) {
        for (int it : umap.getOrDefault(tag1, new ArrayList<>())) {
            prd[it].price += changePrice;
        }
    }

}