Skip to content

Instantly share code, notes, and snippets.

@jhusain
Created August 13, 2018 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhusain/26382a1b2267691284fbe0bcdc44e519 to your computer and use it in GitHub Desktop.
Save jhusain/26382a1b2267691284fbe0bcdc44e519 to your computer and use it in GitHub Desktop.
kth largest card (heap remove broken)
/*
Bar Problem
N friends are playing a game. Each of them has a list of numbers in front of himself.
Each of N friends chooses a number from his list and reports it to the game administrator. Then the game administrator sorts the reported numbers and shouts the K-th largest number.
You want to know the count all possible numbers that the game administrator can shout.
Input Format:
First line of the input contain an integer T, the number of testcases. Then follow T testcases. For each test case the input is of the following format.
In the first line there are two numbers N and K. In each of next N lines there is an integer a_i followed by a_i integers, ith line describes the list for ith person.
All the numbers in all the lists are unique.
Output Format:
For each testcase print in a separate line the count of numbers that the game administrator can shout.
Sample Input
2
3 3
3 2 5 3
3 8 1 6
3 7 4 9
20 11
1 3
1 2
11 1 4 55 6 80 8 9 19 11 12 13
15 14 177 16 17 18 10 20 21 22 37 24 25 26 27 28
7 190 30 31 32 33 34 35
12 81 23 195 39 40 41 42 43 49 45 46 47
15 48 44 50 51 52 53 54 5 121 57 58 59 98 61 62
3 63 64 65
10 66 67 68 69 70 71 72 73 74 75
4 76 91 29 79
11 7 36 82 83 84 85 86 96 88 89 90
17 77 92 93 172 95 87 97 60 99 100 101 102 103 135 186 106 107
10 108 109 110 111 112 113 114 115 116 117
1 118
8 119 120 56 122 123 124 125 126
9 127 128 129 130 131 132 133 134 104
11 136 137 138 139 140 141 142 143 144 145 146
20 147 148 149 150 151 152 153 154 159 156 157 158 155 180 161 162 163 164 165 166
18 167 168 169 170 171 94 173 174 175 176 15 178 179 160 181 182 183 184
17 185 105 187 188 189 78 191 192 193 194 38 196 197 198 199 200 201
Sample Output
6
89
*/
const players = [
[2,5,3],
[8,1,6],
[7,4,9]
]
function* permute(players){
const indices=players.map(_=>0)
while(true){
yield indices.map((index,playerIndex)=>players[playerIndex][index])
for(let count=players.length-1; count>=0; count--){
indices[count]++;
if (indices[count]>=players[count].length){
if (count===0) return;
indices[count] = 0;
}
else break;
}
}
}
class Heap {
constructor(){
this.data=[]
}
add(x){
this.data.push(x)
this._bubble()
}
remove(){
const result=this.data[0]
this.data[0]=this.data.pop()
this._descend()
return result
}
_bubble(){
let index=this.data.length-1
let current=this.data[index]
let parentIndex;
do{
parentIndex=this._parent(index)
if(current>this.data[parentIndex]){
this._swap(index,parentIndex)
index=parentIndex
}
else break;
}while(index!=0)
}
_descend(){
let index=0
let current=this.data[index]
do{
let leftIndex=this._left(index)
let left=this.data[leftIndex]
let rightIndex=this._right(index)
let right=this.data[rightIndex]
console.log(left, ",", right)
if(left!==undefined && left>right && current<left){
this._swap(index,leftIndex)
index=leftIndex
continue;
}
if(right!==undefined && current<right){
this._swap(index,rightIndex)
index=rightIndex
continue;
}
else break;
}while(index!=0)
}
_swap(from,to){
console.log("before", JSON.stringify(this))
const temp=this.data[from]
this.data[from]=this.data[to]
this.data[to]=temp
console.log("after", JSON.stringify(this))
}
_left(index){
return index*2 +1
}
_right(index){
return index*2 +2
}
_parent(index){
return Math.floor(index/2)
}
toJSON(){
return this.data
}
}
//console.log(JSON.stringify(Array.from(permute(players))))
const heap=new Heap()
for(const x of[2,3,6,1,9]){
heap.add(x)
// console.log(JSON.stringify(heap))
}
console.log("removing 9")
heap.remove()
console.log("removing 6")
heap.remove()
console.log(JSON.stringify(heap))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment