题目描述
请你设计一个租房系统。支持以下几个函数:
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;
}
}
}