Skip to content

AbhishekNayak-24/leetcode----1912

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

leetcode----1912

Design Movie Rental System //code in java class MovieRentingSystem { public MovieRentingSystem(int n, int[][] entries) { for (int[] e : entries) { final int shop = e[0]; final int movie = e[1]; final int price = e[2]; unrented.putIfAbsent(movie, new TreeSet<>(comparator)); unrented.get(movie).add(new Entry(price, shop, movie)); shopAndMovieToPrice.put(new Pair<>(shop, movie), price); } }

public List search(int movie) { return unrented.getOrDefault(movie, Collections.emptySet()) .stream() .limit(5) .map(e -> e.shop) .collect(Collectors.toList()); }

public void rent(int shop, int movie) { final int price = shopAndMovieToPrice.get(new Pair<>(shop, movie)); unrented.get(movie).remove(new Entry(price, shop, movie)); rented.add(new Entry(price, shop, movie)); }

public void drop(int shop, int movie) { final int price = shopAndMovieToPrice.get(new Pair<>(shop, movie)); unrented.get(movie).add(new Entry(price, shop, movie)); rented.remove(new Entry(price, shop, movie)); }

public List<List> report() { return rented.stream().limit(5).map(e -> List.of(e.shop, e.movie)).collect(Collectors.toList()); }

private record Entry(int price, int shop, int movie) {}

private Comparator comparator = Comparator.comparingInt(Entry::price) .thenComparingInt(Entry::shop) .thenComparingInt(Entry::movie);

// {movie: (price, shop)} private Map<Integer, Set> unrented = new HashMap<>();

// {(shop, movie): price} private Map<Pair<Integer, Integer>, Integer> shopAndMovieToPrice = new HashMap<>();

// (price, shop, movie) private Set rented = new TreeSet<>(comparator); }

About

Design Movie Rental System

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published