Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Coupons = [
- // {"CategoryName":"Comforter Sets", "CouponName":"Comforters Sale"},
- // {"CategoryName":"Bedding", "CouponName":"Savings on Bedding"},
- // {"CategoryName":"Bed & Bath", "CouponName":"Low price for Bed & Bath"}
- // ]
- // Categories = [
- // {"CategoryName":"Comforter Sets", "CategoryParentName":"Bedding"},
- // {"CategoryName":"Bedding", "CategoryParentName":"Bed & Bath"},
- // {"CategoryName":"Bed & Bath", "CategoryParentName":null},
- // {"CategoryName":"Soap Dispensers", "CategoryParentName":"Bathroom Accessories"},
- // {"CategoryName":"Bathroom Accessories", "CategoryParentName":"Bed & Bath"},
- // {"CategoryName":"Toy Organizers", "CategoryParentName":"Baby And Kids"},
- // {"CategoryName":"Baby And Kids", "CategoryParentName":null}
- // ]
- // # tests: input(CategoryName) => output(CouponName)
- // "Comforter Sets" => "Comforters Sale"
- // "Bedding" => "Savings on Bedding"
- // "Bathroom Accessories" => "Low price for Bed & Bath"
- // "Soap Dispensers" => "Low price for Bed & Bath"
- // "Toy Organizers" => null
- /*
- child -> parent
- a -> [c,d,e]
- / \
- c [e,f] d
- / \
- e f
- Meta Data (Table)
- a -> [c,d]
- c -> [e,f]
- a
- /
- e c d
- p[a] = null
- p[c] = a
- p[d] = a
- p[e] = c
- coupon(e) -> coupon(c)
- Coupons (cat) -> coupon
- tree out of category
- parent[CategoryName] -> CategoryName
- input
- 1 query
- Time = height of tree (n)
- Coupons
- {"CategoryName":"Comforter Sets", "CouponName":"Comforters Sale"},
- Categories
- // Categories = [
- // {"CategoryName":"Comforter Sets", "CategoryParentName":"Bedding"},
- */
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class Main {
- static Map<String, String> couponMap = new HashMap<>();
- static Map<String, String> categoryParent = new HashMap<>();
- static Map<String, String> categoryToCoupon = new HashMap<>();
- static String processGetCouponFromCategory(String category) {
- if (category == null) {
- return null;
- }
- if (couponMap.get(category) != null) {
- return couponMap.get(category);
- }
- else {
- return processGetCouponFromCategory(categoryParent.get(category));
- }
- }
- void preProcessAllCategory(List<String> categoryList) {
- for (int i=0; i<categoryList.size(); i++) {
- String cat = categoryList.get(i);
- categoryToCoupon.put(cat, processGetCouponFromCategory(cat));
- }
- return ;
- }
- String getCoupon(String category) {
- if (categoryToCoupon.get(category) != null) {
- return categoryToCoupon.get(category);
- }
- return null;
- }
- public static void main(String[] args) {
- // Coupons = [
- // {"CategoryName":"Comforter Sets", "CouponName":"Comforters Sale"},
- // {"CategoryName":"Bedding", "CouponName":"Savings on Bedding"},
- // {"CategoryName":"Bed & Bath", "CouponName":"Low price for Bed & Bath"}
- // ]
- couponMap.put("Comforter Sets", "Comforters Sale");
- couponMap.put("Bedding", "Savings on Bedding");
- couponMap.put("Bed & Bath", "Low price for Bed & Bath");
- // Categories = [
- // {"CategoryName":"Comforter Sets", "CategoryParentName":"Bedding"},
- // {"CategoryName":"Bedding", "CategoryParentName":"Bed & Bath"},
- // {"CategoryName":"Bed & Bath", "CategoryParentName":null},
- // {"CategoryName":"Soap Dispensers", "CategoryParentName":"Bathroom Accessories"},
- // {"CategoryName":"Bathroom Accessories", "CategoryParentName":"Bed & Bath"},
- // {"CategoryName":"Toy Organizers", "CategoryParentName":"Baby And Kids"},
- // {"CategoryName":"Baby And Kids", "CategoryParentName":null}
- // ]
- categoryParent.put("Comforter Sets", "Bedding");
- categoryParent.put("Bedding", "Bed & Bath");
- categoryParent.put("Bed & Bath", null);
- categoryParent.put("Soap Dispensers", "Bathroom Accessories");
- categoryParent.put("Bathroom Accessories", "Bed & Bath");
- categoryParent.put("Toy Organizers", "Baby And Kids");
- categoryParent.put("Baby And Kids", null);
- // # tests: input(CategoryName) => output(CouponName)
- // "Comforter Sets" => "Comforters Sale"
- // "Bedding" => "Savings on Bedding"
- // "Bathroom Accessories" => "Low price for Bed & Bath"
- // "Soap Dispensers" => "Low price for Bed & Bath"
- // "Toy Organizers" => null
- String coupon = processGetCouponFromCategory("Toy Organizers");
- System.out.println(coupon);
- // // Press Opt+Enter with your caret at the highlighted text to see how
- //
- // // IntelliJ IDEA suggests fixing it.
- // System.out.printf("Hello and welcome!");
- //
- // // Press Ctrl+R or click the green arrow button in the gutter to run the code.
- // for (int i = 1; i <= 5; i++) {
- //
- // // Press Ctrl+D to start debugging your code. We have set one breakpoint
- // // for you, but you can always add more by pressing Cmd+F8.
- // System.out.println("i = " + i);
- // }
- }
- }
Add Comment
Please, Sign In to add comment