mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-11 15:06:07 +00:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
/**
|
||||
* File: my_heap.js
|
||||
* Created Time: 2023-02-06
|
||||
* Author: what-is-me (whatisme@outlook.jp)
|
||||
*/
|
||||
|
||||
const { printHeap } = require('../modules/PrintUtil');
|
||||
|
||||
/* Max heap class */
|
||||
class MaxHeap {
|
||||
#maxHeap;
|
||||
|
||||
/* Constructor, build empty heap or build heap from input list */
|
||||
constructor(nums) {
|
||||
// Add list elements to heap as is
|
||||
this.#maxHeap = nums === undefined ? [] : [...nums];
|
||||
// Heapify all nodes except leaf nodes
|
||||
for (let i = this.#parent(this.size() - 1); i >= 0; i--) {
|
||||
this.#siftDown(i);
|
||||
}
|
||||
}
|
||||
|
||||
/* Get index of left child node */
|
||||
#left(i) {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
|
||||
/* Get index of right child node */
|
||||
#right(i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* Get index of parent node */
|
||||
#parent(i) {
|
||||
return Math.floor((i - 1) / 2); // Floor division
|
||||
}
|
||||
|
||||
/* Swap elements */
|
||||
#swap(i, j) {
|
||||
const tmp = this.#maxHeap[i];
|
||||
this.#maxHeap[i] = this.#maxHeap[j];
|
||||
this.#maxHeap[j] = tmp;
|
||||
}
|
||||
|
||||
/* Get heap size */
|
||||
size() {
|
||||
return this.#maxHeap.length;
|
||||
}
|
||||
|
||||
/* Check if heap is empty */
|
||||
isEmpty() {
|
||||
return this.size() === 0;
|
||||
}
|
||||
|
||||
/* Access top element */
|
||||
peek() {
|
||||
return this.#maxHeap[0];
|
||||
}
|
||||
|
||||
/* Element enters heap */
|
||||
push(val) {
|
||||
// Add node
|
||||
this.#maxHeap.push(val);
|
||||
// Heapify from bottom to top
|
||||
this.#siftUp(this.size() - 1);
|
||||
}
|
||||
|
||||
/* Starting from node i, heapify from bottom to top */
|
||||
#siftUp(i) {
|
||||
while (true) {
|
||||
// Get parent node of node i
|
||||
const p = this.#parent(i);
|
||||
// When "crossing root node" or "node needs no repair", end heapify
|
||||
if (p < 0 || this.#maxHeap[i] <= this.#maxHeap[p]) break;
|
||||
// Swap two nodes
|
||||
this.#swap(i, p);
|
||||
// Loop upward heapify
|
||||
i = p;
|
||||
}
|
||||
}
|
||||
|
||||
/* Element exits heap */
|
||||
pop() {
|
||||
// Handle empty case
|
||||
if (this.isEmpty()) throw new Error('Heap is empty');
|
||||
// Delete node
|
||||
this.#swap(0, this.size() - 1);
|
||||
// Remove node
|
||||
const val = this.#maxHeap.pop();
|
||||
// Return top element
|
||||
this.#siftDown(0);
|
||||
// Return heap top element
|
||||
return val;
|
||||
}
|
||||
|
||||
/* Starting from node i, heapify from top to bottom */
|
||||
#siftDown(i) {
|
||||
while (true) {
|
||||
// If node i is largest or indices l, r are out of bounds, no need to continue heapify, break
|
||||
const l = this.#left(i),
|
||||
r = this.#right(i);
|
||||
let ma = i;
|
||||
if (l < this.size() && this.#maxHeap[l] > this.#maxHeap[ma]) ma = l;
|
||||
if (r < this.size() && this.#maxHeap[r] > this.#maxHeap[ma]) ma = r;
|
||||
// Swap two nodes
|
||||
if (ma === i) break;
|
||||
// Swap two nodes
|
||||
this.#swap(i, ma);
|
||||
// Loop downwards heapification
|
||||
i = ma;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
print() {
|
||||
printHeap(this.#maxHeap);
|
||||
}
|
||||
|
||||
/* Extract elements from heap */
|
||||
getMaxHeap() {
|
||||
return this.#maxHeap;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
if (require.main === module) {
|
||||
/* Consider negating the elements before entering the heap, which can reverse the size relationship, thus implementing max heap */
|
||||
const maxHeap = new MaxHeap([9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2]);
|
||||
console.log('\nAfter inputting list and building heap');
|
||||
maxHeap.print();
|
||||
|
||||
/* Check if heap is empty */
|
||||
let peek = maxHeap.peek();
|
||||
console.log(`\nHeap top element is ${peek}`);
|
||||
|
||||
/* Element enters heap */
|
||||
let val = 7;
|
||||
maxHeap.push(val);
|
||||
console.log(`\nAfter element ${val} pushes to heap`);
|
||||
maxHeap.print();
|
||||
|
||||
/* Time complexity is O(n), not O(nlogn) */
|
||||
peek = maxHeap.pop();
|
||||
console.log(`\nAfter heap top element ${peek} pops from heap`);
|
||||
maxHeap.print();
|
||||
|
||||
/* Get heap size */
|
||||
let size = maxHeap.size();
|
||||
console.log(`\nHeap size is ${size}`);
|
||||
|
||||
/* Check if heap is empty */
|
||||
let isEmpty = maxHeap.isEmpty();
|
||||
console.log(`\nIs heap empty ${isEmpty}`);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
MaxHeap,
|
||||
};
|
||||
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* File: top_k.js
|
||||
* Created Time: 2023-08-13
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
const { MaxHeap } = require('./my_heap');
|
||||
|
||||
/* Element enters heap */
|
||||
function pushMinHeap(maxHeap, val) {
|
||||
// Negate element
|
||||
maxHeap.push(-val);
|
||||
}
|
||||
|
||||
/* Element exits heap */
|
||||
function popMinHeap(maxHeap) {
|
||||
// Negate element
|
||||
return -maxHeap.pop();
|
||||
}
|
||||
|
||||
/* Access top element */
|
||||
function peekMinHeap(maxHeap) {
|
||||
// Negate element
|
||||
return -maxHeap.peek();
|
||||
}
|
||||
|
||||
/* Extract elements from heap */
|
||||
function getMinHeap(maxHeap) {
|
||||
// Negate element
|
||||
return maxHeap.getMaxHeap().map((num) => -num);
|
||||
}
|
||||
|
||||
/* Find the largest k elements in array based on heap */
|
||||
function topKHeap(nums, k) {
|
||||
// Python's heapq module implements min heap by default
|
||||
// Note: We negate all heap elements to simulate min heap using max heap
|
||||
const maxHeap = new MaxHeap([]);
|
||||
// Enter the first k elements of array into heap
|
||||
for (let i = 0; i < k; i++) {
|
||||
pushMinHeap(maxHeap, nums[i]);
|
||||
}
|
||||
// Starting from the (k+1)th element, maintain heap length as k
|
||||
for (let i = k; i < nums.length; i++) {
|
||||
// If current element is greater than top element, top element exits heap, current element enters heap
|
||||
if (nums[i] > peekMinHeap(maxHeap)) {
|
||||
popMinHeap(maxHeap);
|
||||
pushMinHeap(maxHeap, nums[i]);
|
||||
}
|
||||
}
|
||||
// Return elements in heap
|
||||
return getMinHeap(maxHeap);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
const nums = [1, 7, 6, 3, 2];
|
||||
const k = 3;
|
||||
const res = topKHeap(nums, k);
|
||||
console.log(`The largest ${k} elements are`, res);
|
||||
Reference in New Issue
Block a user