This file contains hidden or 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
| /** | |
| * Floyd's Cycle Finding Algorithm | |
| * If a cycle is present in the linked list, returns a pointer to the node | |
| * where that cycle begins. | |
| * If no cycle is present, returns nullptr. | |
| * | |
| * @param head A pointer to the head of the linked list. | |
| * | |
| * @return A pointer to the node where the cycle begins. | |
| */ |
This file contains hidden or 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
| #include <iostream> | |
| #include <vector> | |
| /** | |
| * Boyer-Moore Majority Vote Algorithm. | |
| * Returns the majority element. The majority element is | |
| * the element which appears more than half the time (> n/2). | |
| * | |
| * @param nums A list of numbers. | |
| * |
This file contains hidden or 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
| #include <iostream> | |
| #include <vector> | |
| /** | |
| * Creates the n+1 x W+1 dp table. | |
| * | |
| * @param n The number of items. | |
| * @param W The capacity of the bag. | |
| * @param weights The list of weights of each item. | |
| * @param values The list of values of each item. |
This file contains hidden or 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
| #include <vector> | |
| /** | |
| * Dutch National Flag Problem | |
| * Given a list of elements, where each element can only be one of three | |
| * distinct values, this algorithm arranges the elements of the input | |
| * list into the three groups. | |
| * | |
| * @param nums A list of numbers where each number can only be one of | |
| * three unique values. |
This file contains hidden or 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
| /** | |
| * Trick #1: Check if an integer is even or odd. | |
| */ | |
| bool isEven(int x) { | |
| return ((x&1) == 0); | |
| } | |
| /** | |
| * Trick #2: Multiply and divide by 2^k. |