Posts
Backtracking
INFO It’s an Algorithm to permute string. Usefull to find all the possible permutations
EXAMPLE Steps Create a function which take the string, the first index and the last index: If start and end index are the same, end of the algorithm (print or return depends on what you wants) Otherwise, loop on each index between start and end: Swap string at index start and string at index i of loop Call the function with as first index the first index + 1 Swap again string at index start and string at index i of loop Code #include <iostream> #include <string> void backtrack(std::string &s, int idx, int N) { if (idx == N) std::cout << s << std::endl; else { for (int i = idx; i <= N; i++) { std::swap(s[idx], s[i]); backtrack(s, idx + 1, N); // Reset std::swap(s[idx], s[i]); } } } int main(int ac, char *av[]) { std::string s = "ABC"; int n = s.
read morePosts
Binary Search
INFO Binary Search called “Recherche Dichotomique” in French is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half.
APPROACH Find the Exact Value class Solution { public: int search(vector<int> &nums, int target) { int leftPtr = 0; int rightPtr = nums.size() - 1; while (leftPtr <= rightPtr) { int m = (leftPtr + rightPtr) / 2; if (nums[m] == target) return m; else if (nums[m] < target) leftPtr = m + 1; else rightPtr = m - 1; } return -1; } }; Find Upper bound class Solution { public: int search(vector<int> &nums, int target) { int leftPtr = 0; int rightPtr = nums.
read morePosts
Bitwise Operation
Logically, a bit-to-bit operation is a calculation manipulating the data directly at the level of the bits, according to a Boolean arithmetic
Use in Programming Advanced to be blazingly fast
OPERATORS
read morePosts
Breadth-first Search
INFO The width path algorithm allows the path of a graph or a tree as follows: we start by exploring a source node, then its successors, then the unexplored successors of successors, etc
read morePosts
Bucket Sort
BUCKET SORT Bucket sort, or bin sort, is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm.
Pseudo Code function bucketSort(array, k) is buckets = new array of k empty lists M = 1 + the maximum key value in the array for i = 0 to length(array) do insert array[i] into buckets[floor(k × array[i] / M)] for i = 0 to k do nextSort(buckets[i]) return the concatenation of buckets[0], .
read morePosts
Check Inclusion
PROBLEM Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise.
In other words, return true if one of s1’s permutations is the substring of s2.
Example1 *Input*: s1 = "ab", s2 = "eidbaooo" *Output*: true Explanation: s2 contains one permutation of s1 ("ba"). Example2 =Input=: s1 = "ab", s2 = "eidboaoo" =Output=: false SOLVING Steps Create two vectors of size 26 filled by some 0 for the two string Fill the first vector with the string s1: For each char: Increase by one at position char - 'a' (transform a-z into 0-26) Create index i and j to 0 Use j to parcor string s2 and increase by one the second vector at position s2[j] If j-i equal length of string s1 that means second vector has enough information to compare two vector for permutation If j-i inferior at length of string s1 that means second vector doesn’t has enough information to compare two vector for permutation, so only increase j Otherwise increase i and j and decrease s2[i] because not in the scoop of search anymore If previous compare didn’t work return false Code class Solution { private: bool areVectorsEqual(vector<int> a, vector<int> b) { for (int i = 0; i < 26; i++) { if (a[i] !
read morePosts
Climbing Stairs
PROBLEM You are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Example1 *Input*: n = 2 *Output*: 2 Explanation: There are two ways to climb to the top. 1. 1 step + 1 step 2. 2 steps Example2 *Input*: n = 3 *Output*: 3 Explanation: There are three ways to climb to the top.
read morePosts
Data Structure
INFORMATION A data structure is a specialized format for organizing, processing, retrieving and storing data. There are several basic and advanced types of data structures, all designed to arrange data to suit a specific purpose. Data structures make it easy for users to access and work with the data they need in appropriate ways.
read morePosts
Defuse The Bomb
PROBLEM You have a bomb to defuse, and your time is running out! Your informer will provide you with a circular array code of length of n and a key k.
To decrypt the code, you must replace every number. All the numbers are replaced simultaneously.
If k > 0, replace the ith number with the sum of the next k numbers. If k < 0, replace the ith number with the sum of the previous k numbers.
read morePosts
Depth-first Search
INFO The deep path algorithm is a tree path algorithm, and more generally a graph path. He naturally describes himself in a recursive way. Its simplest application is to determine if there is a path from one summit to another
read more