1912. 设计电影租借系统

1912. 设计电影租借系统

解题思路

我们要设计一个电影租借系统,该系统具有的功能有:初始化、按价格顺序查询电影、租借、归还、按价格顺序查询已租借电影。从题意可知,初始化只会执行一次,数据结构转换的过程应该放在初始化中,设计合理的数据结构保证其他操作的效率。有两个查询都需要用价格的排序,所以整个数据必须要用价格进行排序,这里用list的方式进行排序。租借和归还都需要找到电影后修改其状态,为了减少遍历,使用商店和电影作为键记录列表数据的下标,可以快速找到电影数据并修改其租借状态。

代码实现

class MovieRentingSystem {
    public List<Movie> sort;

    public Map<String, Integer> map;

    public MovieRentingSystem(int n, int[][] entries) {
        sort = new ArrayList<>();
        for (int[] entry : entries) {
            Movie movie = new Movie(entry[0], entry[1], entry[2]);
            sort.add(movie);
        }
        sort.sort((a, b) -> {
            if (a.price != b.price) {
                return a.price - b.price;
            } else if (a.shop != b.shop) {
                return a.shop - b.shop;
            } else {
                return a.movie - b.movie;
            }
        });

        map = new HashMap<>();
        for (int i=0; i<sort.size(); i++) {
            Movie movie = sort.get(i);
            map.put(movie.shop + "-" + movie.movie, i);
        }
        System.out.println("s");
    }

    public List<Integer> search(int movie) {
        List<Integer> list = new ArrayList<>();
        for (Movie item : sort) {
            if (item.movie == movie && !item.flag) {
                list.add(item.shop);
                if (list.size() == 5) {
                    break;
                }
            }
        }
        return list;
    }

    public void rent(int shop, int movie) {
        Movie movie1 = sort.get(map.get(shop + "-" + movie));
        movie1.flag = true;
    }

    public void drop(int shop, int movie) {
        Movie movie1 = sort.get(map.get(shop + "-" + movie));
        movie1.flag = false;
    }

    public List<List<Integer>> report() {
        List<List<Integer>> list = new ArrayList<>();
        for (Movie movie : sort) {
            if (movie.flag) {
                List<Integer> item = Arrays.asList(movie.shop, movie.movie);
                list.add(item);
                if (list.size() == 5) {
                    break;
                }
            }
        }
        return list;
    }

    static class Movie {
        public Movie(int shop, int movie, int price) {
            this.shop = shop;
            this.movie = movie;
            this.price = price;
            this.flag = false;
        }

        /**
         * 商店编号
         */
        public int shop;
        /**
         * 电影编号
         */
        public int movie;
        /**
         * 价格
         */
        public int price;
        /**
         * 是否已借出
         */
        public boolean flag;
    }
}

复杂度分析

  • 时间复杂度:
    1. MovieRentingSystem(n,entries)操作:2O(n) + nlog(n)
    2. search(movie)操作:O(n)
    3. rent(shop,movie)操作:O(1)
    4. drop(shop,movie)操作:O(1)
    5. report()操作:O(n)
  • 空间复杂度2O(n)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值