priority queue
from queue import PriorityQueue
class PqElement(object):
def __init__(self, value: int):
self.val = value
#Custom Compare Function (less than or equsal)
def __lt__(self, other):
"""self < obj."""
return self.val > other.val
#Print each element function
def __repr__(self):
return f'PQE:{self.val}'
#Usage-
pq = PriorityQueue()
pq.put(PqElement(v)) #Add Item - O(Log(n))
topValue = pq.get() #Pop top item - O(1)
topValue = pq.queue[0].val #Get top value - O(1)
3.67
3
std::priority_queue<int, std::vector<int>, std::greater<int>>
Thank you!
3
0
3.67
9
function PriorityQueue() {
this.collection = [];
this.printCollection = function () {
console.log(this.collection);
};
// Only change code below this line
this.enqueue = function (newitem) {
if (this.isEmpty()) {
return this.collection.push(newitem);
}
this.collection = this.collection.reverse();
var found_index = this.collection.findIndex(function (item) {
return newitem[1] >= item[1];
});
if (found_index === -1) {
this.collection.push(newitem);
} else {
this.collection.splice(found_index, 0, newitem);
}
this.collection = this.collection.reverse();
};
this.dequeue = function () {
if (!this.isEmpty()) {
return this.collection.shift()[0];
} else {
return "The queue is empty.";
}
};
this.size = function () {
return this.collection.length;
};
this.front = function () {
return this.collection[0][0];
};
this.isEmpty = function () {
return this.size() > 0 ? false : true;
};
// Only change code above this line
}
Thank you!
9
0
4.2
10
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int main()
{
priority_queue<int>pq;
int n=5;
while(n--)
{
int val;
cout<<"enter the value you want to insert:"<<endl;
cin>>val;
pq.push(val);
}
priority_queue<int>p;
p.push(100);
p.push(1000);
p.push(3000);
p.push(5000);
pq.swap(p);
while(!pq.empty())
{
cout<<pq.top()<<" ";
pq.pop();
}
return 0;
}
Thank you!
10
0
0
9
Defect priority is the amount of urgency
to fixing that defect. Has fast bug has
to be fixed. We were discussing in grooming meeting.
Developers and PO determines the level of priority
Thank you!
9
0
Are there any code examples left?
New code examples in category Javascript