mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-14 12:40:58 +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,64 @@
|
||||
/**
|
||||
* File: heap.cs
|
||||
* Created Time: 2023-02-06
|
||||
* Author: zjkung1123 (zjkung1123@gmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_heap;
|
||||
|
||||
public class heap {
|
||||
void TestPush(PriorityQueue<int, int> heap, int val) {
|
||||
heap.Enqueue(val, val); // Element enters heap
|
||||
Console.WriteLine($"\nAfter element {val} pushes to heap\n");
|
||||
PrintUtil.PrintHeap(heap);
|
||||
}
|
||||
|
||||
void TestPop(PriorityQueue<int, int> heap) {
|
||||
int val = heap.Dequeue(); // Time complexity is O(n), not O(nlogn)
|
||||
Console.WriteLine($"\nAfter heap top element {val} pops from heap\n");
|
||||
PrintUtil.PrintHeap(heap);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Initialize heap */
|
||||
// Python's heapq module implements min heap by default
|
||||
PriorityQueue<int, int> minHeap = new();
|
||||
// Initialize max heap (modify Comparer using lambda expression)
|
||||
PriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y.CompareTo(x)));
|
||||
Console.WriteLine("Following test cases are for max heap");
|
||||
|
||||
/* Element enters heap */
|
||||
TestPush(maxHeap, 1);
|
||||
TestPush(maxHeap, 3);
|
||||
TestPush(maxHeap, 2);
|
||||
TestPush(maxHeap, 5);
|
||||
TestPush(maxHeap, 4);
|
||||
|
||||
/* Check if heap is empty */
|
||||
int peek = maxHeap.Peek();
|
||||
Console.WriteLine($"Heap top element is {peek}");
|
||||
|
||||
/* Time complexity is O(n), not O(nlogn) */
|
||||
// Dequeued elements form a descending sequence
|
||||
TestPop(maxHeap);
|
||||
TestPop(maxHeap);
|
||||
TestPop(maxHeap);
|
||||
TestPop(maxHeap);
|
||||
TestPop(maxHeap);
|
||||
|
||||
/* Get heap size */
|
||||
int size = maxHeap.Count;
|
||||
Console.WriteLine($"Heap size is {size}");
|
||||
|
||||
/* Check if heap is empty */
|
||||
bool isEmpty = maxHeap.Count == 0;
|
||||
Console.WriteLine($"Is heap empty {isEmpty}");
|
||||
|
||||
/* Input list and build heap */
|
||||
var list = new int[] { 1, 3, 2, 5, 4 };
|
||||
minHeap = new PriorityQueue<int, int>(list.Select(x => (x, x)));
|
||||
Console.WriteLine("After input list and building min heap");
|
||||
PrintUtil.PrintHeap(minHeap);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
/**
|
||||
* File: my_heap.cs
|
||||
* Created Time: 2023-02-06
|
||||
* Author: zjkung1123 (zjkung1123@gmail.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_heap;
|
||||
|
||||
/* Max heap */
|
||||
class MaxHeap {
|
||||
// Use list instead of array, no need to consider capacity expansion
|
||||
List<int> maxHeap;
|
||||
|
||||
/* Constructor, build empty heap */
|
||||
public MaxHeap() {
|
||||
maxHeap = [];
|
||||
}
|
||||
|
||||
/* Constructor, build heap from input list */
|
||||
public MaxHeap(IEnumerable<int> nums) {
|
||||
// Add list elements to heap as is
|
||||
maxHeap = new List<int>(nums);
|
||||
// Heapify all nodes except leaf nodes
|
||||
var size = Parent(this.Size() - 1);
|
||||
for (int i = size; i >= 0; i--) {
|
||||
SiftDown(i);
|
||||
}
|
||||
}
|
||||
|
||||
/* Get index of left child node */
|
||||
int Left(int i) {
|
||||
return 2 * i + 1;
|
||||
}
|
||||
|
||||
/* Get index of right child node */
|
||||
int Right(int i) {
|
||||
return 2 * i + 2;
|
||||
}
|
||||
|
||||
/* Get index of parent node */
|
||||
int Parent(int i) {
|
||||
return (i - 1) / 2; // Floor division
|
||||
}
|
||||
|
||||
/* Access top element */
|
||||
public int Peek() {
|
||||
return maxHeap[0];
|
||||
}
|
||||
|
||||
/* Element enters heap */
|
||||
public void Push(int val) {
|
||||
// Add node
|
||||
maxHeap.Add(val);
|
||||
// Heapify from bottom to top
|
||||
SiftUp(Size() - 1);
|
||||
}
|
||||
|
||||
/* Get heap size */
|
||||
public int Size() {
|
||||
return maxHeap.Count;
|
||||
}
|
||||
|
||||
/* Check if heap is empty */
|
||||
public bool IsEmpty() {
|
||||
return Size() == 0;
|
||||
}
|
||||
|
||||
/* Starting from node i, heapify from bottom to top */
|
||||
void SiftUp(int i) {
|
||||
while (true) {
|
||||
// Get parent node of node i
|
||||
int p = Parent(i);
|
||||
// If 'past root node' or 'node needs no repair', end heapify
|
||||
if (p < 0 || maxHeap[i] <= maxHeap[p])
|
||||
break;
|
||||
// Swap two nodes
|
||||
Swap(i, p);
|
||||
// Loop upward heapify
|
||||
i = p;
|
||||
}
|
||||
}
|
||||
|
||||
/* Element exits heap */
|
||||
public int Pop() {
|
||||
// Handle empty case
|
||||
if (IsEmpty())
|
||||
throw new IndexOutOfRangeException();
|
||||
// Delete node
|
||||
Swap(0, Size() - 1);
|
||||
// Remove node
|
||||
int val = maxHeap.Last();
|
||||
maxHeap.RemoveAt(Size() - 1);
|
||||
// Return top element
|
||||
SiftDown(0);
|
||||
// Return heap top element
|
||||
return val;
|
||||
}
|
||||
|
||||
/* Starting from node i, heapify from top to bottom */
|
||||
void SiftDown(int i) {
|
||||
while (true) {
|
||||
// If node i is largest or indices l, r are out of bounds, no need to continue heapify, break
|
||||
int l = Left(i), r = Right(i), ma = i;
|
||||
if (l < Size() && maxHeap[l] > maxHeap[ma])
|
||||
ma = l;
|
||||
if (r < Size() && maxHeap[r] > maxHeap[ma])
|
||||
ma = r;
|
||||
// If 'node i is largest' or 'past leaf node', end heapify
|
||||
if (ma == i) break;
|
||||
// Swap two nodes
|
||||
Swap(i, ma);
|
||||
// Loop downwards heapification
|
||||
i = ma;
|
||||
}
|
||||
}
|
||||
|
||||
/* Swap elements */
|
||||
void Swap(int i, int p) {
|
||||
(maxHeap[i], maxHeap[p]) = (maxHeap[p], maxHeap[i]);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public void Print() {
|
||||
var queue = new Queue<int>(maxHeap);
|
||||
PrintUtil.PrintHeap(queue);
|
||||
}
|
||||
}
|
||||
|
||||
public class my_heap {
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Consider negating the elements before entering the heap, which can reverse the size relationship, thus implementing max heap */
|
||||
MaxHeap maxHeap = new([9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2]);
|
||||
Console.WriteLine("\nAfter inputting list and building heap");
|
||||
maxHeap.Print();
|
||||
|
||||
/* Check if heap is empty */
|
||||
int peek = maxHeap.Peek();
|
||||
Console.WriteLine($"Heap top element is {peek}");
|
||||
|
||||
/* Element enters heap */
|
||||
int val = 7;
|
||||
maxHeap.Push(val);
|
||||
Console.WriteLine($"After element {val} pushes to heap");
|
||||
maxHeap.Print();
|
||||
|
||||
/* Time complexity is O(n), not O(nlogn) */
|
||||
peek = maxHeap.Pop();
|
||||
Console.WriteLine($"After heap top element {peek} pops from heap");
|
||||
maxHeap.Print();
|
||||
|
||||
/* Get heap size */
|
||||
int size = maxHeap.Size();
|
||||
Console.WriteLine($"Heap size is {size}");
|
||||
|
||||
/* Check if heap is empty */
|
||||
bool isEmpty = maxHeap.IsEmpty();
|
||||
Console.WriteLine($"Is heap empty {isEmpty}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* File: top_k.cs
|
||||
* Created Time: 2023-06-14
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_heap;
|
||||
|
||||
public class top_k {
|
||||
/* Find the largest k elements in array based on heap */
|
||||
PriorityQueue<int, int> TopKHeap(int[] nums, int k) {
|
||||
// Python's heapq module implements min heap by default
|
||||
PriorityQueue<int, int> heap = new();
|
||||
// Enter the first k elements of array into heap
|
||||
for (int i = 0; i < k; i++) {
|
||||
heap.Enqueue(nums[i], nums[i]);
|
||||
}
|
||||
// Starting from the (k+1)th element, maintain heap length as k
|
||||
for (int 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] > heap.Peek()) {
|
||||
heap.Dequeue();
|
||||
heap.Enqueue(nums[i], nums[i]);
|
||||
}
|
||||
}
|
||||
return heap;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] nums = [1, 7, 6, 3, 2];
|
||||
int k = 3;
|
||||
PriorityQueue<int, int> res = TopKHeap(nums, k);
|
||||
Console.WriteLine("The largest " + k + " elements are");
|
||||
PrintUtil.PrintHeap(res);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user