LeetCode1

5 篇文章 0 订阅
5 篇文章 0 订阅

题目描述

请你设计一个租房系统。支持以下几个函数:

boolean addRoom(int id, int area, int price, int rooms, int[] address):向系统中加入房号为 id 的房子。id:
房间编号,area:房间面积,price:房间租金,rooms:房间对应数量,address:房间平面坐标(x坐标,y坐标;长度为2)

执行 addRoom 操作:

1)若租房系统中已经存在房号为id的房间,则更新该房间的信息为新加入的信息,同时返回false;

2)若租房系统中已经不存在房号为id的房间,则更新该房间的信息为新加入的信息,同时返回true。

boolean deleteRoom(int id):从系统中移除房号为id的房子。

1)若租房系统中已经存在房号为id的房间,则将该房间从系统移除,返回true;

2)若租房系统中不存在房号为id的房间,返回false。

int[] queryRoom(int area, int price, int rooms, int[] address, int[][] orderBy):查询满足指定规则的房间。

查询规则:

找出当前租房系统中房间面积大于等于area,租金小于等于price,房间数量等于rooms的房间,将这些房间按照orderBy指定的规则进行升序或者降序排列,orderBy的每一个元素为( parameter,order),其中parameter可能取值为1(按照面积排序) 2 (按照租金排序) 3(曼哈顿距离排序)order可能的取值1 (升序排列) -1(降序排列)
,曼哈顿距离由查询的address和满足面积大于等于area,租金小于等于price,房间数量等于rooms的房间对应address求解 |x1-x2|+|y1-y2|

函数原型:

    public class RentingSystem {
        public RentingSystem() {

        }

        public boolean addRoom(int id, int area, int price, int rooms, int[] address) {

        }

        public boolean deleteRoom(int id) {

        }

        public int[] queryRoom(int area, int price, int rooms, int[] address, int[][] orderBy) {

        }
    }


输入: ["RentingSystem","addRoom","addRoom","queryRoom","deleteRoom"]测试用例:

[[],[3,24,200,2,[0,1]],[1,10,400,2,[1,0]],[1,400,2,[1,1],[[3,1],[2,-1]]],[3]]

输出: [null,true,true,[1,3],true]

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class RentingSystem {

    private Map<Integer, Room> roomMap = new HashMap<>();

    // 按照距离排序
    private static final int ORDER_DIST = 3;

    // 按照面积排序
    private static final int ORDER_AREA = 1;

    public RentingSystem() {
    }

    public boolean addRoom(int id, int area, int price, int rooms, int[] address) {
        boolean result = !roomMap.containsKey(id);
        Room room = new Room(id, area, price, rooms, address);
        roomMap.put(id, room);
        return result;
    }

    public boolean deleteRoom(int id) {
        Room room = roomMap.remove(id);
        return (room != null);
    }

    public int[] queryRoom(int area, int price, int rooms, int[] address, int[][] orderBy) {
        List<Room> roomList = roomMap.values()
            .stream()
            .filter(room -> room.getArea() >= area && room.price <= price && room.rooms == rooms)
            .collect(Collectors.toList());

        roomList.sort((o1, o2) -> {
            for (int[] ints : orderBy) {
                int compareResult = compareRoom(o1, o2, ints[0], ints[1], address);
                if (compareResult != 0) {
                    return compareResult;
                }
            }

            return o1.getId() - o2.getId();
        });

        return roomList.stream().mapToInt(Room::getId).toArray();
    }

    private int compareRoom(Room o1, Room o2, int orderTarget, int order, int[] address) {
        if (orderTarget == ORDER_DIST) {
            int dist1 = calDist(address, o1);
            int dist2 = calDist(address, o2);
            return order * (dist1 - dist2);
        }
        if (orderTarget == ORDER_AREA) {
            return order * (o1.getArea() - o2.getArea());
        } else {
            return order * (o1.getPrice() - o2.getPrice());
        }

    }

    private int calDist(int[] address, Room room) {
        int[] add = room.getAddress();
        int x = Math.abs(add[0] - address[0]);
        int y = Math.abs(add[1] - address[1]);

        return x + y;
    }

    class Room {
        private int id;

        private int area;

        private int price;

        private int rooms;

        private int[] address;

        public Room(int id, int area, int price, int rooms, int[] address) {
            this.id = id;
            this.area = area;
            this.price = price;
            this.rooms = rooms;
            this.address = address;
        }

        public int getId() {
            return id;
        }

        public int getArea() {
            return area;
        }

        public int getPrice() {
            return price;
        }

        public int getRooms() {
            return rooms;
        }

        public int[] getAddress() {
            return address;
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值