[英]Reducing time complexity for the given task
我有一件商品,它有名稱和價格。
現在我程序的輸入包含一些命令列表,表明我可以添加新項目或 select 以下格式的項目:
Add, item-name, item-price --> 將 item-name 和 price item-price 添加到我的列表中。 Select, , --> select 在索引 k 處具有最低價格的項目名稱,並打印到 output。如果多個項目具有相同的價格,則 select 名稱按升序排列。
示例 1:
輸入:
Add, Apple, 4
Add, Ball, 3
Select, ,
Add, Toy, 5
Add, Pen, 1
Select, ,
Output:
Ball, Ball
解釋:
首先,我們添加 Apple 和 Ball。 所以項目按價格排序 [Ball(3), Apple(4)]。 將k初始化為0,此時Select已經出現。 因此,獲取索引 k=0 處的項目,即 Ball。 現在,已選擇k至1作為選擇。 然后我們添加玩具和筆。 所以物品按價格排序 [Pen(1), Ball(3), Apple(4), Toy(5)]。 現在k為1。那么Select就出現了。 因此,再次獲取索引 k=1 處的項目,即 Ball 。 現在,已選擇k至2作為選擇。
So output is Ball, Ball.
示例 2:
輸入:
Add, Apple, 4
Add, Ball, 3
Select, ,
Select, ,
Add, Toy, 5
Select, ,
Output:
Ball, Apple, Toy
解釋:
首先,我們添加 Apple 和 Ball。 所以項目按價格排序 [Ball(3), Apple(4)]。 將k初始化為0,此時Select已經出現。 因此,獲取索引 k=0 處的項目,即 Ball。 現在,已選擇k至1作為選擇。 然后Select出現了。 因此,獲取索引 k=1 處的項目,即 Apple。 現在,已選擇k至2作為選擇。 然后我們添加玩具。 所以物品按價格排序 [Ball(3), Apple(4), Toy(5)]。 現在k是2。那么Select已經出現了。 因此,獲取索引 k=2 處的項目,即玩具。 現在,已選擇k至3作為選擇。
So output is Ball, Apple, Toy.
這是我試過的代碼:
public static void main(String[] args) {
System.out.println(process(Arrays.asList(Arrays.asList("Add", "Apple", "4"), Arrays.asList("Add", "Ball", "3"),
Arrays.asList("Select", "", ""), Arrays.asList("Add", "Toy", "5"), Arrays.asList("Add", "Pen", "1"),
Arrays.asList("Select", "", ""))));
System.out.println(process(Arrays.asList(Arrays.asList("Add", "Apple", "4"), Arrays.asList("Add", "Ball", "3"),
Arrays.asList("Select", "", ""), Arrays.asList("Select", "", ""), Arrays.asList("Add", "Toy", "5"),
Arrays.asList("Select", "", ""))));
}
public static List<String> process(List<List<String>> input) {
List<String> list = new ArrayList<>();
PriorityQueue<Item> pq = new PriorityQueue<>();
int k = 0;
for (List<String> e : input) {
if ("Add".equals(e.get(0))) {
Item a = new Item(e.get(1), e.get(2));
pq.add(a);
} else {
List<Item> sorted = new ArrayList<>();
for (int i = 0; i <= k; i++) {
sorted.add(pq.poll());
}
Item itemAtK = sorted.get(sorted.size() - 1);
list.add(itemAtK.name);
pq.addAll(sorted);
k++;
}
}
return list;
}
}
class Item implements Comparable<Item> {
int price;
String name;
public Item(String name, String p) {
this.name = name;
this.price = Integer.parseInt(p);
}
public int compareTo(Item item) {
int c = price - item.price;
if (c == 0)
c = name.compareTo(item.name);
return c;
}
我猜這里的時間復雜度是n^2*log(n)
如何降低這段代碼的時間復雜度。
根據stef注釋修改代碼:
public static List<String> process(List<List<String>> input) {
List<String> list = new ArrayList<>();
PriorityQueue<Item> pq = new PriorityQueue<>();
for (List<String> e : input) {
if ("Add".equals(e.get(0))) {
Item a = new Item(e.get(1), e.get(2));
pq.add(a);
} else {
Item itemAtK = pq.poll();
list.add(itemAtK.name);
}
}
return list;
}
輸入:
Add, Apple, 4
Add, Ball, 3
Select, ,
Add, Toy, 5
Add, Pen, 1
Select, ,
預計 Output:
Ball, Ball
這個程序給出錯誤的output:
Ball, Pen
正如有人評論的那樣,也許您需要的不是PriorityQueue
。
這是一個不平衡 BST 的實現:
public static List<String> process(List<List<String>> input) {
List<String> list = new ArrayList<>();
Item root = null;
int k = 0;
for (List<String> e : input) {
if ("Add".equals(e.get(0))) {
Item a = new Item(e.get(1), e.get(2));
root = Item.insert(root, a);
} else {
Item first = Item.itemAt(root, k);
list.add(first.name);
++k;
}
}
return list;
}
class Item implements Comparable<Item> {
int count;
private Item left;
private Item right;
int price;
String name;
public Item(String name, String p) {
this.name = name;
this.price = Integer.parseInt(p);
}
@Override
public int compareTo(Item item) {
int c = price - item.price;
if (c == 0)
c = name.compareTo(item.name);
return c;
}
public static Item insert(Item root, Item item) {
if (root == null) {
item.count = 1;
return item;
}
int c = item.compareTo(root);
if (c < 0) {
root.left = insert(root.left, item);
} else {
root.right = insert(root.right, item);
}
++root.count;
return root;
}
public static Item itemAt(Item root, int ix) {
if (root == null) {
return null;
}
if (root.left != null) {
if (ix < root.left.count) {
return itemAt(root.left, ix);
}
ix -= root.left.count;
}
if (ix == 0) {
return root;
}
--ix;
if (root.right != null) {
if (ix < root.right.count) {
return itemAt(root.right, ix);
}
}
return null;
}
}
平均復雜度為 O(log(n)),但由於樹是不平衡的,因此存在退化情況(當項目按排序輸入時)。
勘誤表:O(n*log(n))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.