### Dynamic Programming: Tabulation Example Source: https://github.com/teivah/algodeck/blob/master/site/index.html Demonstrates the tabulation technique in dynamic programming, a bottom-up approach that starts with the smallest solutions and builds up to the final problem. It iteratively computes and stores results. ```pseudocode tabFib(n) { mem[0] = 0 mem[1] = 1 for i = 2...n mem[i] = mem[i-2] + mem[i-1] return mem[n] } ``` -------------------------------- ### Dynamic Programming: Tabulation Example Source: https://github.com/teivah/algodeck/blob/master/docs/index.md Demonstrates the tabulation technique in dynamic programming, a bottom-up approach where solutions to smaller subproblems are iteratively built up to solve the larger problem. This example calculates Fibonacci numbers. ```pseudocode tabFib(n) { mem[0] = 0 mem[1] = 1 for i = 2...n mem[i] = mem[i-2] + mem[i-1] return mem[n] } ``` -------------------------------- ### Dynamic Programming: Memoization Example Source: https://github.com/teivah/algodeck/blob/master/site/index.html Illustrates the memoization technique in dynamic programming, where results of subproblems are cached to avoid redundant computations. This top-down approach starts with a complex problem and breaks it down. ```pseudocode f(x) { if (mem[x] is undefined) mem[x] = f(x-1) + f(x-2) return mem[x] } ``` -------------------------------- ### HTTP POST Request Structure Source: https://github.com/teivah/algodeck/blob/master/docs/designdeck.md An example of a standard HTTP POST request message, illustrating the method, URL, protocol version, headers, and the message body. ```http POST https://example.com HTTP/1.0 Host: example.com User-Agent: Mozilla/4.0 Content-Length: 5 Hello ``` -------------------------------- ### Dynamic Programming: Memoization Example Source: https://github.com/teivah/algodeck/blob/master/docs/index.md Illustrates the memoization technique in dynamic programming, a top-down approach where results of subproblems are cached to avoid recomputation. This example shows a recursive function that utilizes a memoization table. ```pseudocode f(x) { if (mem[x] is undefined) mem[x] = f(x-1) + f(x-2) return mem[x] } ``` -------------------------------- ### Comparator for Integer Ordering Source: https://github.com/teivah/algodeck/blob/master/docs/index.md Provides examples of comparator functions used for ordering integers, specifically for min-heap and max-heap implementations. These functions define the comparison logic between two elements. ```pseudocode // Ordering, min-heap: (a, b) -> a - b // Reverse ordering, max-heap: (a, b) -> b - a ``` -------------------------------- ### GLightbox Initialization with Options (JavaScript) Source: https://github.com/teivah/algodeck/blob/master/site/designdeck/index.html This JavaScript code initializes the GLightbox library with specific options for touch navigation, looping, zoomability, and drag support. It subscribes to document changes to ensure GLightbox is initialized after the DOM is ready. ```javascript document$.subscribe(() => { const lightbox = GLightbox({ "touchNavigation": true, "loop": false, "zoomable": true, "draggable": true, "openEffect": "zoom", "closeEffect": "zoom", "slideEffect": "slide" }); }) ``` -------------------------------- ### Implement 0/1 Knapsack with Tabulation in Java Source: https://github.com/teivah/algodeck/blob/master/site/index.html Uses an iterative bottom-up approach with a 2D array to build a solution table. It calculates the maximum profit by comparing the exclusion of an item versus its inclusion plus the best result for the remaining weight. ```java public int solveKnapsack(int[] profits, int[] weights, int capacity) { int[][] a = new int[profits.length + 1][capacity + 1]; for (int row = 1; row < profits.length + 1; row++) { int value = profits[row - 1]; int weight = weights[row - 1]; for (int col = 1; col < capacity + 1; col++) { int remainingWeight = col - weight; if (remainingWeight < 0) { a[row][col] = a[row - 1][col]; } else { a[row][col] = Math.max(a[row - 1][col], value + a[row - 1][remainingWeight]); } } } return a[profits.length][capacity]; } ``` -------------------------------- ### Move zeros to the left in Java Source: https://github.com/teivah/algodeck/blob/master/docs/index.md Moves all zeros in an array to the left while maintaining the relative order of non-zero elements. Uses a two-pointer technique starting from the end of the array. ```java public void move(int[] a) { int w = a.length - 1, r = a.length - 1; while (r >= 0) { if (a[r] == 0) { r--; } else { swap(a, r--, w--); } } } ``` -------------------------------- ### Get i-th Bit (Java) Source: https://github.com/teivah/algodeck/blob/master/docs/index.md Retrieves the value of the i-th bit of an integer. It performs a bitwise AND operation with a mask that has only the i-th bit set and checks if the result is non-zero. ```java boolean getBit(final int num, final int i) { return ((num & (1 << i)) != 0); } ``` -------------------------------- ### Implement 0/1 Knapsack with Memoization in Java Source: https://github.com/teivah/algodeck/blob/master/site/index.html Uses a recursive approach with a 2D Integer array to cache results of subproblems. It tracks remaining capacity and item index to achieve O(n * c) time and space complexity. ```java public int knapsack(int[] profits, int[] weights, int capacity) { Integer[][] a = new Integer[capacity][profits.length]; return knapsack(profits, weights, capacity, 0, 0, a); } public int knapsack(int[] profits, int[] weights, int capacity, int i, int sum, Integer[][] a) { if (i == profits.length || capacity == 0) { return sum; } if (a[capacity - 1][i] != null) { return a[capacity][i]; } int sum1 = knapsack(profits, weights, capacity, i + 1, sum, a); int sum2 = 0; if (weights[i] <= capacity) { sum2 = knapsack(profits, weights, capacity - weights[i], i + 1, sum + profits[i], a); } a[capacity - 1][i] = Math.max(sum1, sum2); return a[capacity - 1][i]; } ``` -------------------------------- ### Move zeros to the left Source: https://github.com/teivah/algodeck/blob/master/site/index.html Moves all zeros in an array to the left while maintaining the relative order of non-zero elements using a two-pointer approach starting from the end. ```java public void move(int[] a) { int w = a.length - 1, r = a.length - 1; while (r >= 0) { if (a[r] == 0) { r--; } else { swap(a, r--, w--); } } } ``` -------------------------------- ### Initialize GLightbox Source: https://github.com/teivah/algodeck/blob/master/site/anki/index.html Initializes the GLightbox library for image viewing with specific touch, zoom, and drag settings. This script subscribes to the document event stream to ensure the lightbox is ready upon page load. ```javascript document$.subscribe(() => { const lightbox = GLightbox({ "touchNavigation": true, "loop": false, "zoomable": true, "draggable": true, "openEffect": "zoom", "closeEffect": "zoom", "slideEffect": "slide" }); }); ``` -------------------------------- ### Kafka Partition Distribution Hash Functions Source: https://github.com/teivah/algodeck/blob/master/site/designdeck/index.html Illustrates how Kafka clients distribute messages across partitions using hash functions based on message keys. It specifies default hash implementations in Java and Go, and the behavior for empty keys. ```java // Default hash in Java: murmur2 ``` ```go // Default hash in Go: FNV-1a ``` ```code // If key is empty: round-robin ``` -------------------------------- ### Set i-th Bit (Java) Source: https://github.com/teivah/algodeck/blob/master/docs/index.md Sets the i-th bit of an integer to 1. This is done using a bitwise OR operation with a mask that has the i-th bit set. ```java int setBit(final int num, final int i) { return num | (1 << i); } ``` -------------------------------- ### 0/1 Knapsack Tabulation Space Optimization (Java) Source: https://github.com/teivah/algodeck/blob/master/docs/index.md Demonstrates space optimization for the 0/1 Knapsack problem using tabulation. Instead of a full 2D DP table, it uses only two rows to store the necessary states, reducing space complexity. ```java int[][] dp = new int[2][c + 1]; ``` -------------------------------- ### Mergesort Algorithm Implementation in Java Source: https://github.com/teivah/algodeck/blob/master/docs/index.md Implements the Mergesort algorithm, which recursively divides a collection into halves, sorts them, and then merges them. It uses a helper array for the merge operation. ```java void mergeSort(int[] a) { int[] helper = new int[a.length]; mergeSort(a, helper, 0, a.length - 1); } void mergeSort(int a[], int helper[], int lo, int hi) { if (lo < hi) { int mid = (lo + hi) / 2; mergeSort(a, helper, lo, mid); mergeSort(a, helper, mid + 1, hi); merge(a, helper, lo, mid, hi); } } private void merge(int[] a, int[] helper, int lo, int mid, int hi) { // Copy into helper for (int i = lo; i <= hi; i++) { helper[i] = a[i]; } int p1 = lo; // Pointer on the first half int p2 = mid + 1; // Pointer on the second half int index = lo; // Index of a // Copy the smallest values from either the left or the right side back to the original array while (p1 <= mid && p2 <= hi) { if (helper[p1] <= helper[p2]) { a[index] = helper[p1]; p1++; } else { a[index] = helper[p2]; p2++; } index++; } // Copy the eventual rest of the left side of the array into the target array while (p1 <= mid) { a[index] = helper[p1]; index++; p1++; } } ``` -------------------------------- ### Traverse binary tree level-by-level using BFS in Java Source: https://github.com/teivah/algodeck/blob/master/site/index.html This algorithm uses a queue to perform a breadth-first search, processing nodes level by level. It tracks the queue size at each iteration to ensure nodes are grouped correctly into their respective levels. ```java public static List> traverse(TreeNode root) { List> result = new LinkedList<>(); Queue queue = new LinkedList<>(); queue.add(root); while (!queue.isEmpty()) { List level = new ArrayList<>(); int levelSize = queue.size(); for (int i = 0; i < levelSize; i++) { TreeNode current = queue.poll(); level.add(current.val); if (current.left != null) { queue.add(current.left); } if (current.right != null) { queue.add(current.right); } } result.add(level); } return result; } ``` -------------------------------- ### Reverse an array in Java Source: https://github.com/teivah/algodeck/blob/master/docs/index.md Reverses the elements of an array in-place using a two-pointer approach. It swaps elements from both ends moving towards the center until the pointers meet. ```java int i = 0; int j = a.length - 1; while (i < j) { swap(a, i++, j--); } ``` -------------------------------- ### Top K Elements (Biggest and Smallest) - Min Heap and Max Heap Source: https://github.com/teivah/algodeck/blob/master/site/index.html Techniques for finding the K largest or smallest elements in a collection using min-heap and max-heap data structures. For K biggest, a min-heap is used; for K smallest, a max-heap is used. Elements are added to the heap, and then compared with the heap's root to maintain the top K elements. -------------------------------- ### String Permutation Generation in Java Source: https://github.com/teivah/algodeck/blob/master/docs/index.md Generates all possible permutations of a given string using recursion and backtracking. It involves swapping characters and making recursive calls. ```java void permute(String s) { permute(s, 0); } void permute(String s, int index) { if (index == s.length() - 1) { System.out.println(s); return; } for (int i = index; i < s.length(); i++) { s = swap(s, index, i); permute(s, index + 1); s = swap(s, index, i); // backtrack } } ```