snippets 0.1.0
|
Classes | |
class | BigInteger |
Implements a class for representing and manipulating large integers beyond the native integer range. More... | |
class | CircularDeque |
class | DisjointSet |
class | DominantTracker |
struct | Interval |
class | IntervalMap |
class | IntervalSet |
struct | ListNode |
struct | Maximum |
class | MonotonicStack |
struct | Override |
class | RationalNumber |
class | SegmentTree |
class | SlidingWindowMax |
struct | TreeNode |
Concepts | |
concept | numeric |
Typedefs | |
template<typename T > | |
using | MonotonicDecreasingStack = MonotonicStack<T, std::less_equal<T>> |
template<typename T > | |
using | MonotonicIncreasingStack = MonotonicStack<T, std::greater_equal<T>> |
Functions | |
static TreeNode * | new_binary_tree (const std::vector< std::optional< int > > v) |
static std::vector< std::optional< int > > | binary_tree_to_vector (TreeNode *root) |
static void | delete_binary_tree (TreeNode *root) |
static int | get_binary_tree_depth (TreeNode *root) |
static void | inorder (TreeNode *root, std::function< void(int)> func) |
static void | preorder (TreeNode *root, std::function< void(int)> func) |
static void | postorder (TreeNode *root, std::function< void(int)> func) |
static std::unordered_map< int, std::vector< int > > | binary_tree_to_adjacency_list (TreeNode *root) |
static std::vector< std::vector< std::pair< int, int > > > | make_weighted_directed_adjacency_list (int n, const std::vector< std::vector< int > > &edges) |
static std::vector< std::vector< int > > | make_unweighted_undirected_adjacency_list (int n, const std::vector< std::vector< int > > &edges) |
static std::vector< std::vector< std::pair< int, int > > > | make_weighted_undirected_adjacency_list (int n, const std::vector< std::vector< int > > &edges) |
static std::vector< std::vector< int > > | make_unweighted_directed_adjacency_list (int n, const std::vector< std::vector< int > > &edges) |
static void | breadth_first_search (std::vector< std::vector< int > > &adjacency_list, int root, std::function< void(int, int)> callback) |
static void | breadth_first_search (std::unordered_map< int, std::vector< int > > &adjacency_list, int root, std::function< void(int, int)> callback) |
static void | depth_first_search (std::vector< std::vector< int > > &adjacency_list, int root, std::function< void(int, int)> callback) |
static int | dijkstra (int n, const std::vector< std::vector< std::pair< int, int > > > &adjacency_list, int src, int dst) |
static std::vector< int > | find_euler_path_directed (const std::vector< std::vector< int > > &edges) |
static std::vector< std::vector< int > > | find_connected_components (std::unordered_map< int, std::vector< int > > &adj) |
static int | count_connected_components (const std::unordered_map< int, std::vector< int > > &adj) |
template<std::integral T> | |
static bool | operator< (const Interval< T > &interval, T x) |
template<std::integral T> | |
static bool | operator< (T x, const Interval< T > &interval) |
template<std::integral T> | |
static bool | operator< (const Interval< T > &interval1, const Interval< T > &interval2) |
static int | get_linked_list_length (ListNode *head) |
static ListNode * | make_linked_list (const std::vector< int > &v) |
static std::vector< int > | linked_list_to_vector (ListNode *head) |
static void | linked_list_delete (ListNode *head) |
static ListNode * | linked_list_remove (ListNode *head, size_t begin, size_t end) |
static ListNode * | get_linked_list_ith_node (ListNode *head, size_t i) |
constexpr int | modular_add (int x, int y) |
constexpr int | additive_inverse (int x) |
constexpr int | modular_subtract (int x, int y) |
constexpr int | modular_multiply (int x, int y) |
constexpr int | modular_square (int x) |
constexpr int | modular_cube (int x) |
static int | modular_pow2 (size_t exponent) |
template<typename T > | |
std::vector< T > | nextGreaterElement (const std::vector< T > &nums, std::function< T()> no_greater) |
static int | largestRectangleInHistogram (const std::vector< int > &heights) |
template<std::integral T> | |
int | numDigits (T num) |
template<std::integral T> | |
int | numBits (T num) |
static std::vector< int > | SieveOfEratosthenes (int n) |
static bool | isPerfectSquare (std::int32_t num) |
template<std::integral T> | |
static std::vector< T > | getPrefixMax (const std::vector< T > &v) |
Computes the prefix maximums of a given vector. | |
template<std::integral T> | |
static std::vector< T > | getSuffixMax (const std::vector< T > &v) |
Computes the suffix maximums of a given vector. | |
template<std::integral T> | |
static std::vector< T > | getPrefixMin (const std::vector< T > &v) |
Computes the prefix minimums of a given vector. | |
template<std::integral T> | |
static std::vector< T > | getSuffixMin (const std::vector< T > &v) |
Computes the suffix minimums of a given vector. | |
template<std::integral T> | |
static std::vector< T > | getPrefixSum (const std::vector< T > &v) |
Computes the prefix sums of a given vector. | |
template<std::integral T> | |
static std::vector< T > | getSuffixSum (const std::vector< T > &v) |
Computes the suffix sums of a given vector. | |
static int | subarraysWithAtMostKDistinct (const std::vector< int > &nums, int k) |
template<std::totally_ordered T> | |
int | numOfGreaterElements (const std::vector< T > &v, T value) |
template<std::integral T> | |
bool | containsInRange (const std::vector< T > &vec, T start, T end) |
template<typename T , std::integral I = int> | |
std::optional< I > | deferred_binary_search (I low, I high, const T &value, auto &&func) |
template<typename T , std::integral I = int> | |
I | deferred_upper_bound (I low, I high, const T &value, auto &&func, auto &&comp) |
template<typename T , std::integral I = int> | |
I | deferred_upper_bound (I low, I high, const T &value, auto &&func) |
template<typename T , std::integral I = int> | |
I | deferred_lower_bound (I low, I high, const T &value, auto &&func, auto &&comp) |
template<typename T , std::integral I = int> | |
I | deferred_lower_bound (I low, I high, const T &value, auto &&func) |
template<std::integral T> | |
void | sortThree (T &a, T &b, T &c) |
static std::tuple< std::function< int(int)>, int > | create_compression_mapper (const std::vector< int > &nums) |
static std::vector< int > | kmpSearch (const std::string &pat, const std::string &txt) |
static bool | isPalindrome (const std::string &s) |
template<std::integral T> | |
std::string | to_string (const std::vector< T > &vec) |
template<std::integral T> | |
std::string | to_string (std::pair< T, T > p) |
template<std::integral T> | |
std::string | to_string (const std::vector< std::pair< T, T > > &vec) |
static std::string | to_string (const std::vector< std::string > &vec) |
template<std::integral T> | |
std::string | to_string (const std::vector< std::vector< T > > &vec) |
static std::vector< std::string > | string_split (const std::string &s, char delimiter) |
static std::string | string_repeat (const std::string &s, size_t n) |
static std::string | string_repeat (char c, size_t n) |
static std::string | string_join (const std::vector< std::string > &strings, const std::string &delimiter) |
template<numeric T> | |
bool | lessThanAll (T x, T val) |
template<numeric T, numeric... Args> | |
bool | lessThanAll (T x, T val, Args... args) |
template<numeric T> | |
bool | lessThanOrEqualToAll (T x, T val) |
template<numeric T, numeric... Args> | |
bool | lessThanOrEqualToAll (T x, T val, Args... args) |
template<numeric T> | |
bool | greaterThanAll (T x, T val) |
template<numeric T, numeric... Args> | |
bool | greaterThanAll (T x, T val, Args... args) |
template<numeric T> | |
bool | greaterThanOrEqualToAll (T x, T val) |
template<numeric T, numeric... Args> | |
bool | greaterThanOrEqualToAll (T x, T val, Args... args) |
template<numeric T> | |
T | sum (T x) |
template<numeric T, numeric... Args> | |
T | sum (T x, Args... args) |
Variables | |
static constexpr int | MODULO = static_cast<int>(1e9 + 7) |
using hsc_snippets::MonotonicDecreasingStack = MonotonicStack<T, std::less_equal<T>> |
using hsc_snippets::MonotonicIncreasingStack = MonotonicStack<T, std::greater_equal<T>> |
|
constexpr |
|
static |
Converts a binary tree to an adjacency list representation. All node values in the binary tree must be unique. The adjacency list is bidirectional, meaning for each parent-child relation, both the parent's list contains the child and the child's list contains the parent.
root | A pointer to the root node of the binary tree. If the tree is empty (nullptr), the function returns an empty adjacency list. |
|
static |
Converts a binary tree to a vector of optional integers.
root | A pointer to the root node of the binary tree. |
|
static |
Performs a breadth-first search (BFS) on an undirected graph starting from a given root node. It uses a queue to explore nodes level by level, ensuring each node is visited exactly once.
adjacency_list | The graph represented as an adjacency list, where each key-value pair corresponds to a node and its list of adjacent nodes. |
root | The starting node for the BFS. |
callback | A function to be called for each visited node. It takes the distance from the root and the node itself as arguments. |
|
static |
Performs a breadth-first search (BFS) traversal on a graph represented by an adjacency list, starting from the specified root node.
adjacency_list | The adjacency list representation of the graph. Each element represents a node and contains the indices of its adjacent nodes. |
root | The index of the root node from which the BFS traversal starts. |
callback | A callback function invoked for each visited node during the BFS traversal. It takes two parameters: the distance of the current node from the root and the index of the current node. |
bool hsc_snippets::containsInRange | ( | const std::vector< T > & | vec, |
T | start, | ||
T | end ) |
|
static |
Counts the number of connected components in an undirected graph.
adj | The graph represented as an adjacency list, where keys are node identifiers and values are lists of adjacent nodes. |
|
static |
Creates a compression mapper function for the given vector of integers.
This function performs coordinate compression on the input vector nums
, mapping each unique value to a continuous index in ascending order starting from 0. The returned tuple contains a function for efficient lookup of the compressed index corresponding to any value in the original vector and the count of unique elements.
nums | The input vector of integers to be compressed. The vector can contain duplicate values. |
nums
.nums
.std::optional< I > hsc_snippets::deferred_binary_search | ( | I | low, |
I | high, | ||
const T & | value, | ||
auto && | func ) |
Performs a binary search on a function-generated sequence of values to find a specific value. This function assumes that the values generated by func
are in a non-decreasing order within the specified range of indices.
T | The type of the values generated by the function and the type of value . |
I | The integral type used for indexing, with a default of int. |
low | The lowest index of the search range (inclusive). |
high | The highest index of the search range (inclusive). |
value | The target value to search for within the function-generated sequence. |
func | A callable that takes an index of type I and returns a value of type T. This function must return values in a non-decreasing order over the range [low, high]. |
func(index)
, or std::nullopt if no such index exists. I hsc_snippets::deferred_lower_bound | ( | I | low, |
I | high, | ||
const T & | value, | ||
auto && | func ) |
Finds the first position in [low, high] where func(position) is greater than or equal to value.
low | The start of the search range (inclusive). |
high | The end of the search range (inclusive). |
value | The target value for comparison. |
func | A callable that returns values in a non-decreasing order. |
I hsc_snippets::deferred_lower_bound | ( | I | low, |
I | high, | ||
const T & | value, | ||
auto && | func, | ||
auto && | comp ) |
Finds the first position in [low, high] where func(position) is greater than or equal to value, using a custom comparator.
low | The start of the search range (inclusive). |
high | The end of the search range (inclusive). |
value | The target value for comparison. |
func | A callable that returns values in a non-decreasing order. |
comp | A comparator to compare func(position) and value. |
I hsc_snippets::deferred_upper_bound | ( | I | low, |
I | high, | ||
const T & | value, | ||
auto && | func ) |
Finds the first position in [low, high] where func(position) is strictly greater than value.
low | The start of the search range (inclusive). |
high | The end of the search range (inclusive). |
value | The target value for comparison. |
func | A callable that returns values in a non-decreasing order. |
I hsc_snippets::deferred_upper_bound | ( | I | low, |
I | high, | ||
const T & | value, | ||
auto && | func, | ||
auto && | comp ) |
Finds the first position in [low, high] where func(position) is strictly greater than value, using a custom comparator.
low | The start of the search range (inclusive). |
high | The end of the search range (inclusive). |
value | The target value for comparison. |
func | A callable that returns values in a non-decreasing order. |
comp | A comparator to compare func(position) and value. |
|
static |
Deletes all nodes of a binary tree to free memory.
root | A pointer to the root node of the binary tree. If the pointer is nullptr, the function does nothing, safely handling the case of an empty tree or reaching the end of a branch. |
|
static |
Performs a depth-first search (DFS) traversal on a graph represented by an adjacency list, starting from the specified root node.
adjacency_list | The adjacency list representation of the graph. Each element represents a node and contains the indices of its adjacent nodes. |
root | The index of the root node from which the DFS traversal starts. |
callback | A callback function invoked for each visited node during the DFS traversal. It takes two parameters: the distance of the current node from the root and the index of the current node. |
|
static |
Dijkstra's algorithm to find the shortest path from source to destination in a graph.
n | Number of vertices in the graph. |
adjacency_list | Adjacency list representation of the graph where each element is a pair representing an edge (to, weight). |
src | Source vertex. |
dst | Destination vertex. |
|
static |
Finds and returns all connected components in an undirected graph. It iterates over all nodes, using a BFS starting from each unvisited node to discover all nodes in the same connected component.
adj | The graph represented as an adjacency list, where keys are node identifiers and values are lists of adjacent nodes. |
|
static |
Finds an Eulerian path or circuit in a directed graph.
This function assumes that the given graph has an Eulerian path or circuit, which means the graph is connected and either:
edges | A vector of pairs representing directed edges in the graph. Each pair (a, b) represents a directed edge from a to b. |
|
static |
Calculates the depth of a binary tree.
root | A pointer to the root node of the binary tree. If the tree is empty (nullptr), the depth is considered to be 0. |
Retrieves a pointer to the ith element in the linked list.
head | A pointer to the head of the linked list. |
i | The index of the element to retrieve. |
|
static |
Calculates the length of a linked list.
head | A pointer to the head of the linked list. |
|
static |
Computes the prefix maximums of a given vector.
This function returns a vector where each element at index i
contains the maximum value in the inclusive subarray v[0]
to v[i]
.
T | The integral type of the elements in the input vector. |
v | The input vector of integral type T. |
v
, where each element at index i
is the maximum value of the inclusive subarray v[0]
to v[i]
.
|
static |
Computes the prefix minimums of a given vector.
This function returns a vector where each element at index i
contains the minimum value in the inclusive subarray v[0]
to v[i]
.
T | The integral type of the elements in the input vector. |
v | The input vector of integral type T. |
v
, where each element at index i
is the minimum value of the inclusive subarray v[0]
to v[i]
.
|
static |
Computes the prefix sums of a given vector.
This function returns a vector where each element at index i
contains the sum of the elements in the inclusive subarray v[0]
to v[i]
.
T | The integral type of the elements in the input vector. |
v | The input vector of integral type T. |
v
, where each element at index i
is the sum of the elements in the inclusive subarray v[0]
to v[i]
.
|
static |
Computes the suffix maximums of a given vector.
This function returns a vector where each element at index i
contains the maximum value in the inclusive subarray v[i]
to v[n-1]
, where n
is the size of the vector.
T | The integral type of the elements in the input vector. |
v | The input vector of integral type T. |
v
, where each element at index i
is the maximum value of the inclusive subarray v[i]
to v[n-1]
.
|
static |
Computes the suffix minimums of a given vector.
This function returns a vector where each element at index i
contains the minimum value in the inclusive subarray v[i]
to v[n-1]
, where n
is the size of the vector.
T | The integral type of the elements in the input vector. |
v | The input vector of integral type T. |
v
, where each element at index i
is the minimum value of the inclusive subarray v[i]
to v[n-1]
.
|
static |
Computes the suffix sums of a given vector.
This function returns a vector where each element at index i
contains the sum of the elements in the inclusive subarray v[i]
to v[n-1]
, where n
is the size of the vector.
T | The integral type of the elements in the input vector. |
v | The input vector of integral type T. |
v
, where each element at index i
is the sum of the elements in the inclusive subarray v[i]
to v[n-1]
. bool hsc_snippets::greaterThanAll | ( | T | x, |
T | val ) |
bool hsc_snippets::greaterThanAll | ( | T | x, |
T | val, | ||
Args... | args ) |
bool hsc_snippets::greaterThanOrEqualToAll | ( | T | x, |
T | val ) |
bool hsc_snippets::greaterThanOrEqualToAll | ( | T | x, |
T | val, | ||
Args... | args ) |
|
static |
In-order traversal of a binary tree.
root | Root node of the binary tree. |
func | Function to process each node's value. |
|
static |
Checks if a given string is a palindrome.
s | The string to check for palindromicity. |
|
static |
Checks if a given number is a perfect square using a precomputed array of squares.
num | The number to check if it is a perfect square. |
|
static |
Searches for all occurrences of a pattern within a text using the Knuth-Morris-Pratt (KMP) algorithm.
The KMP algorithm pre-processes the pattern to construct an array of longest proper prefixes which are also suffixes (LPS array). It then uses this LPS array to skip redundant comparisons when a mismatch occurs, thereby improving the search efficiency.
pat | The pattern string to search for within the text. |
txt | The text string in which to search for the pattern. |
|
static |
LeetCode 84: Largest Rectangle in Histogram This function calculates the maximum area of a rectangle that can be formed within a given histogram. Each bar's height is given in the 'heights' vector, where each element represents the height of a bar.
heights | A vector of integers where each integer represents the height of a histogram bar. |
bool hsc_snippets::lessThanAll | ( | T | x, |
T | val ) |
bool hsc_snippets::lessThanAll | ( | T | x, |
T | val, | ||
Args... | args ) |
bool hsc_snippets::lessThanOrEqualToAll | ( | T | x, |
T | val ) |
bool hsc_snippets::lessThanOrEqualToAll | ( | T | x, |
T | val, | ||
Args... | args ) |
|
static |
Deletes all nodes in a linked list to free memory.
head | A pointer to the head of the linked list to be deleted. |
|
static |
Removes nodes from a linked list in the specified range.
head | Pointer to the head of the linked list. |
begin | The starting index of the range to remove nodes (inclusive). |
end | The ending index of the range to remove nodes (exclusive). |
|
static |
Converts a linked list to a vector of integers.
head | A pointer to the head of the linked list. |
|
static |
Creates a linked list from a vector of integers.
v | A reference to a vector of integers from which to create the linked list. |
|
static |
Constructs an unweighted, directed adjacency list representation of a graph from a given set of edges. Nodes are indexed from 0 to n-1.
n | The number of nodes in the graph. |
edges | A vector of vectors representing edges, where each inner vector contains two integers: [from, to], denoting a directed edge from node 'from' to node 'to'. |
|
static |
Constructs an unweighted, undirected adjacency list representation of a graph from a given set of edges. Nodes are indexed from 0 to n-1.
n | The number of nodes in the graph. |
edges | A vector of vectors representing edges, where each inner vector contains two integers: [from, to], denoting an edge from node 'from' to node 'to'. |
|
static |
Constructs a weighted, directed adjacency list representation of a graph from a given set of edges. Nodes are indexed from 0 to n-1.
n | The number of nodes in the graph. |
edges | A vector of vectors representing edges, where each inner vector contains three integers: [from, to, weight], denoting a weighted edge from node 'from' to node 'to' with the given weight. |
|
static |
Constructs a weighted, undirected adjacency list representation of a graph from a given set of edges. Nodes are indexed from 0 to n-1.
n | The number of nodes in the graph. |
edges | A vector of vectors representing edges, where each inner vector contains three integers: [from, to, weight], denoting a weighted edge between node 'from' and node 'to' with the given weight. |
|
constexpr |
|
constexpr |
|
constexpr |
|
static |
|
constexpr |
|
constexpr |
|
static |
Constructs a binary tree from a vector of optional integers.
v | A vector of std::optional<int>, where each element represents a potential node in the binary tree. An std::nullopt value indicates the absence of a node at that position. |
std::vector< T > hsc_snippets::nextGreaterElement | ( | const std::vector< T > & | nums, |
std::function< T()> | no_greater ) |
Finds the next greater element for each element in a sequence.
T | The type of elements in the sequence. |
nums | The sequence of elements. |
no_greater | Function to call when no greater element is found. |
nums
. int hsc_snippets::numBits | ( | T | num | ) |
Calculates the number of bits required to represent an integer in binary. For negative numbers, it returns the number of bits in the type (e.g., 32 for int), assuming two's complement representation.
T | The type of the input number, constrained to integral types. |
num | The input number whose binary bit count is to be calculated. |
int hsc_snippets::numDigits | ( | T | num | ) |
Calculates the number of digits in the decimal representation of an integer.
T | The type of the input number, constrained to integral types. |
num | The input number whose number of decimal digits is to be calculated. |
int hsc_snippets::numOfGreaterElements | ( | const std::vector< T > & | v, |
T | value ) |
|
static |
|
static |
|
static |
|
static |
Post-order traversal of a binary tree.
root | Root node of the binary tree. |
func | Function to process each node's value. |
|
static |
Pre-order traversal of a binary tree.
root | Root node of the binary tree. |
func | Function to process each node's value. |
|
static |
Implements the Sieve of Eratosthenes algorithm to find all prime numbers up to a given limit n.
n | The upper limit (inclusive) up to which prime numbers are to be found. |
void hsc_snippets::sortThree | ( | T & | a, |
T & | b, | ||
T & | c ) |
Sorts three elements in non-decreasing order using a simple comparison and swap algorithm.
T | The type of the elements to be sorted. Must be an integral type. |
a | Reference to the first element, will be the smallest after sorting. |
b | Reference to the second element, will be the middle element after sorting. |
c | Reference to the third element, will be the largest after sorting. |
|
static |
Joins a vector of strings into a single string, using a specified delimiter between each string.
strings | The vector of strings to be joined. |
delimiter | The string used as the delimiter between each element in the vector. |
strings
, joined by delimiter
.
|
static |
Repeats a given character c
for n
times, producing a new string.
This function creates a string consisting of the character c
repeated n
times. It efficiently initializes the string with the required size and fills it with the character c
.
c | The character to be repeated. |
n | The number of times to repeat the character. |
c
repeated n
times. If n
is 0, returns an empty string.
|
static |
Repeats a given string s
for n
times efficiently.
This function creates a new string by repeating the input string s
for n
times. It pre-allocates the required memory and employs a doubling strategy to minimize the number of concatenations, which enhances performance, especially for large n
.
s | The string to be repeated. |
n | The number of times to repeat the string. |
s
repeated n
times.
|
static |
Splits a given string into a vector of substrings based on a specified delimiter.
s | The input string to be split. |
delimiter | The character used as the delimiter to split the string. |
|
static |
Counts subarrays with at most k
distinct integers using a sliding window approach.
Related LeetCode Problem: https://leetcode.com/problems/subarrays-with-k-different-integers/description/
nums | A vector of integers. |
k | The maximum number of distinct integers allowed in a subarray. |
T hsc_snippets::sum | ( | T | x | ) |
T hsc_snippets::sum | ( | T | x, |
Args... | args ) |
std::string hsc_snippets::to_string | ( | const std::vector< std::pair< T, T > > & | vec | ) |
Convert a vector of pairs of integral type T to its string representation.
T | The integral type of the elements in the pairs within the vector. |
vec | The vector of pairs to convert to a string representation. |
|
static |
Convert a vector of strings to its string representation, with elements separated by commas. The resulting string is enclosed in square brackets.
vec | The vector of strings to convert. Each element in the vector is expected to be a string. |
std::string hsc_snippets::to_string | ( | const std::vector< std::vector< T > > & | vec | ) |
Convert a vector of vectors of integral type T to its string representation.
T | The integral type of elements in the vectors. |
vec | The vector of vectors to convert. |
std::string hsc_snippets::to_string | ( | const std::vector< T > & | vec | ) |
Convert a vector of integral type T to its string representation.
T | The integral type of elements in the vector. |
vec | The vector to convert. |
std::string hsc_snippets::to_string | ( | std::pair< T, T > | p | ) |
Convert a pair of integral type T to its string representation.
T | The integral type of the pair elements. |
p | The pair to convert to a string representation. |
|
staticconstexpr |