Created
May 25, 2021 12:49
-
-
Save react-ram/bdfdfe563c5b35e24aff86346f36e663 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const _mergeArrays = (a, b) => { | |
const c = [] | |
while (a.length && b.length) { | |
if( a[0][0] == b[0][0] ) { | |
if( a[0][1] < b[0][1] ) { | |
c.push(a.shift()); | |
} else { | |
c.push(b.shift()); | |
} | |
} else { | |
if( a[0][0] < b[0][0] ) { | |
c.push(a.shift()); | |
} else { | |
c.push(b.shift()); | |
} | |
} | |
} | |
//if we still have values, let's add them at the end of `c` | |
while (a.length) { | |
c.push(a.shift()) | |
} | |
while (b.length) { | |
c.push(b.shift()) | |
} | |
return c | |
} | |
const mergeSort = (a) => { | |
if (a.length < 2) return a | |
const middle = Math.floor(a.length / 2) | |
const a_l = a.slice(0, middle) | |
const a_r = a.slice(middle, a.length) | |
const sorted_l = mergeSort(a_l) | |
const sorted_r = mergeSort(a_r) | |
return _mergeArrays(sorted_l, sorted_r) | |
} | |
const sortArray = (array) => { | |
freqItems = {}; | |
// O(n) to count frequency of items | |
for( let i = 0 ; i < array.length; i++) { | |
let item = array[i]; | |
if( item in freqItems ) { | |
freqItems[item] = freqItems[item] + 1; | |
} else { | |
freqItems[item] = 1; | |
} | |
} | |
items = [] | |
for(const [key, value] of Object.entries(freqItems)) { | |
items.push([value, parseInt(key)]); | |
} | |
sorted_items = mergeSort(items); | |
ans = [] | |
for( let i = 0 ; i < sorted_items.length; i++ ) { | |
while(sorted_items[i][0]-- ) { | |
ans.push(sorted_items[i][1]); | |
} | |
} | |
console.log(ans); | |
} | |
input = [3, 6, 4, 5, 4, 5]; | |
sortArray(input); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HistoryList{ | |
constructor(maxCapacity) { | |
this.maxSize = maxCapacity; | |
this.cache = {} | |
this.stack = []; | |
} | |
addToHistory(item) { | |
if( item in this.cache ) { | |
this.stack.pop(item); | |
} | |
if( this.stack.length == this.maxSize) { | |
this.stack.shift(); | |
} | |
this.stack.push(item); | |
} | |
getMostRecent() { | |
if( this.stack.length == 0 ) { | |
return undefined; | |
} else { | |
return this.stack[this.stack.length - 1 ]; | |
} | |
} | |
printList() { | |
console.log(this.stack); | |
} | |
} | |
let lru = new HistoryList(2); | |
lru.addToHistory("one"); | |
console.log(lru.getMostRecent()); | |
lru.addToHistory("two"); | |
console.log(lru.getMostRecent()); | |
lru.printList(); | |
lru.addToHistory("one"); | |
console.log(lru.getMostRecent()); | |
lru.addToHistory("five"); | |
lru.printList(); | |
console.log(lru.getMostRecent()); | |
lru.addToHistory("one"); | |
console.log(lru.getMostRecent()); | |
lru.printList(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment