1832. 判断句子是否为全字母句
var checkIfPangram = function ( sentence) {
let set = new Set ( ) ;
let arr = sentence. split ( "" ) ;
for ( let i= 0 ; i< arr. length; i++ ) {
set . add ( arr[ i] ) ;
}
return set . size === 26 ;
} ;
1833. 雪糕的最大数量
var maxIceCream = function ( costs, coins) {
costs. sort ( ( a, b) => a- b) ;
let cnt = 0 ;
for ( let i= 0 ; i< costs. length; i++ ) {
if ( coins >= costs[ i] ) {
coins -= costs[ i] ;
cnt++ ;
} else break ;
}
return cnt;
} ;
1834. 单线程 CPU
class Heap {
constructor ( compare) {
this . A = [ ] ;
this . compare = compare;
}
size ( ) {
return this . A . length;
}
left ( i) {
return 2 * i + 1 ;
}
right ( i) {
return 2 * i + 2 ;
}
parent ( i) {
return i > 0 ? ( i - 1 ) >>> 1 : - 1 ;
}
isEmpty ( ) {
return this . size ( ) === 0 ;
}
peek ( ) {
return this . A [ 0 ] ;
}
heapifyDown ( i) {
let p = i;
const l = this . left ( i) ,
r = this . right ( i) ,
size = this . size ( ) ;
if ( l < size && this . compare ( l, p) ) p = l;
if ( r < size && this . compare ( r, p) ) p = r;
if ( p !== i) {
this . exchange ( i, p) ;
this . heapifyDown ( p) ;
}
}
heapifyUp ( i) {
const p = this . parent ( i) ;
if ( p >= 0 && this . compare ( i, p) ) {
this . exchange ( i, p) ;
this . heapifyUp ( p) ;
}
}
exchange ( x, y) {
const temp = this . A [ x] ;
this . A [ x] = this . A [ y] ;
this . A [ y] = temp;
}
compare ( ) {
throw new Error ( 'Must be implement!' ) ;
}
}
class PriorityQueue extends Heap {
constructor ( compare) {
super ( compare) ;
}
enqueue ( node) {
this . A . push ( node) ;
this . heapifyUp ( this . size ( ) - 1 ) ;
}
dequeue ( ) {
const first = this . A [ 0 ] ;
const last = this . A . pop ( ) ;
if ( first !== last) {
this . A [ 0 ] = last;
this . heapifyDown ( 0 ) ;
}
return first;
}
}
var getOrder = function ( tasks) {
tasks. forEach ( ( v, i) => v. push ( i) ) ;
tasks. sort ( ( a, b) => b[ 0 ] - a[ 0 ] ) ;
const heap = new PriorityQueue ( function ( x, y) {
return this . A [ x] [ 1 ] !== this . A [ y] [ 1 ]
? this . A [ x] [ 1 ] < this . A [ y] [ 1 ]
: this . A [ x] [ 2 ] < this . A [ y] [ 2 ]
} ) ;
const ans = [ ] ;
let time = tasks[ tasks. length - 1 ] [ 0 ] ;
while ( heap. size ( ) || tasks. length) {
if ( ! heap. isEmpty ( ) ) {
const task = heap. dequeue ( ) ;
ans. push ( task[ 2 ] ) ;
time += task[ 1 ] ;
}
addTask ( heap, tasks, time) ;
if ( heap. isEmpty ( ) && tasks. length) {
time = tasks[ tasks. length - 1 ] [ 0 ] ;
addTask ( heap, tasks, time) ;
}
}
return ans;
} ;
function addTask ( heap, tasks, time) {
while ( tasks. length && tasks[ tasks. length - 1 ] [ 0 ] <= time) {
heap. enqueue ( tasks. pop ( ) ) ;
}
}
1835. 所有数对按位与结果的异或和
var getXORSum = function ( arr1, arr2) {
let a = 0 ;
let b = 0 ;
for ( let i of arr1) a^= i;
for ( let i of arr2) b^= i;
return a& b;
} ;