目录
- 速记
- scala使用scala.collection.mutable.PriorityQueue
- scala用java.util.PriorityQueue也行
速记
https://stackoverflow.com/questions/14927395/how-to-use-priority-queues-in-scala
http://allaboutscala.com/tutorials/chapter-7-beginner-tutorial-using-scala-mutable-collection/scala-tutorial-learn-use-mutable-priorityqueue/#:~:text=This priority is then used,elements in their priority order. 每个元素是一个对象,对象有若干属性
我推荐下面这个
scala> import scala.collection.mutable.PriorityQueue
// import scala.collection.mutable.PriorityQueue
scala> def diff(t2: (Int,Int)) = math.abs(t2._1 - t2._2)
// diff: (t2: (Int, Int))Int
scala> val x = new PriorityQueue[(Int, Int)]()(Ordering.by(diff))
// x: scala.collection.mutable.PriorityQueue[(Int, Int)] = PriorityQueue()
scala> x.enqueue(1 -> 1)
scala> x.enqueue(1 -> 2)
scala> x.enqueue(1 -> 3)
scala> x.enqueue(1 -> 4)
scala> x.enqueue(1 -> 0)
scala> x
// res5: scala.collection.mutable.PriorityQueue[(Int, Int)] = PriorityQueue((1,4), (1,3), (1,2), (1,1), (1,0))
x.dequeue()
scala使用scala.collection.mutable.PriorityQueue
//新版,较为易懂
import scala.collection.mutable._
class Event(_start:Int,_end:Int,_value:Int){
var start:Int=_start;
var end:Int=_end;
var value:Int=_value;
}
object Solution {
def maxTwoEvents(_events: Array[Array[Int]]): Int = {
var events=_events.sortWith((a,b)=>a(0)-b(0)<0); //先开始的在前面
def diff(x:Event)= -x.end;
var queue = new PriorityQueue[Event]()(Ordering.by(diff));//结束时间晚的在队首,结束时间早的在队尾
var ans = 0;
var max = 0;
for (i<-Range(0,events.length,1)) {
var Array(start,end,value)=events(i);
while ( (!queue.isEmpty) && queue.head.end < start) {
var cur = queue.dequeue();
max = Math.max(max, cur.value);
}
ans = Math.max(value + max, ans);
queue.enqueue(new Event(_start=start,_end=end,_value=value));
}
return ans;
}
}
//Scala 优先级队列
scala用java.util.PriorityQueue也行
import java.util.TreeSet;
import java.util.PriorityQueue;
import scala.util.control.Breaks._;
class ExamRoom(_n: Int) {
var n=_n;
var seats=new TreeSet[Int]();
var pq=new PriorityQueue[Array[Int]](
new Ordering[Array[Int]] {
def compare(a: Array[Int], b: Array[Int]): Int = {
val d1 = a(1) - a(0)
val d2 = b(1) - b(0)
if (d1 / 2 < d2 / 2 || (d1 / 2 == d2 / 2 && a(0) > b(0))) 1
else -1
}
}
);
def seat(): Int = {
if (seats.isEmpty()) {
seats.add(0);
return 0;
}
var left = seats.first();var right = n - 1 - seats.last();
breakable({
while (seats.size() >= 2) {
var p = pq.peek();
if (seats.contains(p(0)) && seats.contains(p(1)) && seats.higher(p(0)) == p(1)) { // 不属于延迟删除的区间
var d = p(1) - p(0);
if (d / 2 < right || d / 2 <= left) { // 最左或最右的座位更优
break;
}
pq.poll();
pq.offer(Array(p(0), p(0) + d / 2));
pq.offer(Array(p(0) + d / 2, p(1)));
seats.add(p(0) + d / 2);
return p(0) + d / 2;
}
pq.poll(); // leave 函数中延迟删除的区间在此时删除
}
});
if (right > left) { // 最右的位置更优
pq.offer(Array(seats.last(), n - 1));
seats.add(n - 1);
return n - 1;
} else {
pq.offer(Array(0, seats.first()));
seats.add(0);
return 0;
}
}
def leave(p: Int) {
if (p != seats.first() && p != seats.last()) {
var prev = seats.lower(p);
var next = seats.higher(p);
pq.offer(Array(prev,next));
}
seats.remove(p);
}
}
//Scala 延迟删除 + 有序集合 + 优先队列