diff --git a/lowest_common_ancestor/uva_12238_ants_colony/README.md b/lowest_common_ancestor/uva_12238_ants_colony/README.md index 7352cfd..c010a45 100644 --- a/lowest_common_ancestor/uva_12238_ants_colony/README.md +++ b/lowest_common_ancestor/uva_12238_ants_colony/README.md @@ -6,6 +6,9 @@ ## Solution +* Weighted Lowest Common Ancestor / Range Minimum Query +* Input: Tree with edge weights +* Output: LCA of 2 nodes in tree and shortest distance b/w 2 nodes * **Key Idea:** Since each ant hill is connected is only one of previous ant hills, ant hill network is a tree * Build an Euler Tour Representation of ant hills [E[], L[], H[]] @@ -13,14 +16,15 @@ * Shortest path between 2 ant hills is computed as Dist(A, LCA(A, B)) + Dist(B, LCA(A, B)) * To efficiently compute Distance between a node A and its LCA(A, B) with B, store distance of every node from root of anthills tree -## Space complexity +## Complexity -* Weighted Lowest Common Ancestor / Range Minimum Query -* Input: Tree with edge weights -* Output: LCA of 2 nodes in tree and shortest distance b/w 2 nodes -* Space complexity: O(3 * (2N-1)) + O(3 * N) + O(2 * (2N-1)-1) = O(6N-3 + 3N + 4N-3) = O(13N - 6) +* N - Number of ant hills + +### Space Complexity + +* **Space complexity:** O(3 * (2N-1)) + O(3 * N) + O(2 * (2N-1)-1) = O(6N-3 + 3N + 4N-3) = O(13N - 6) -## Run complexity +### Run complexity * Build: EulerTour: O(2N-1), SegmentTreeBuild: O(4N-2) * LCA Query: O(log(2N-1)) diff --git a/priority_queues/argus/README.md b/priority_queues/argus/README.md index e2e6fc4..3a17989 100644 --- a/priority_queues/argus/README.md +++ b/priority_queues/argus/README.md @@ -20,8 +20,18 @@ * Update its nexttick * Push back to Priority Queue -**Space Complexity:** 3 integers for storing a Query ~ O(3N) -**Query Complexity:** O(KlogN) [K - Top K queries to be executed, N - Number of queries registered] +## Complexity + +* N - Number of registered queries +* K - Top K queries to be executed + +### Space Complexity + +* **Space Complexity:** 3 integers for storing a Query ~ O(3N) + +### Run Complexity + +* **Query Complexity:** O(K * logN) ## Testcases diff --git a/priority_queues/hackerearth_monkniq/README.md b/priority_queues/hackerearth_monkniq/README.md index 4029081..288c3f0 100644 --- a/priority_queues/hackerearth_monkniq/README.md +++ b/priority_queues/hackerearth_monkniq/README.md @@ -12,7 +12,19 @@ * Add student and lastIQ * Push back to Priority Queue -**Space Complexity:** 4 integers for storing a Course ~ O(4C) -**Query Complexity:** O(PlogC) [P - Monk and his friends, C - Number of courses] +## Complexity + +* P - Monk and his friends +* C - Number of courses + +### Space Complexity + +* **Space Complexity:** O(4C) + * 1 Course requires 4 integers + +### Run Complexity + +* **Query Complexity:** O(2P * logC) + - 2 operations per person in Monk and his friends group [1 pop and 1 insert] diff --git a/segment_trees/help_alice/README.md b/segment_trees/help_alice/README.md index b06ec0a..7e3f5e2 100644 --- a/segment_trees/help_alice/README.md +++ b/segment_trees/help_alice/README.md @@ -18,8 +18,17 @@ * else, set diff to 0 * Update the sum-value of all nodes with range [L, R] in segment tree where `L <= i <= R` with +diff -**Space Complexity:** O(5N) (~ for segmentTree(2^ceil(log2(2N)))) -**Query Complexity:** O(logN) per query [N - Number of items in array] +## Complexity + +* N - Number of numbers in array + +### Space Complexity + +* **Space Complexity:** O(5N) (~ for segmentTree(2^ceil(log2(2N)))) + +### Run Complexity + +* **Query Complexity:** O(logN) per query ## Testcases diff --git a/segment_trees/newyork/README.md b/segment_trees/newyork/README.md index 04df630..7967df1 100644 --- a/segment_trees/newyork/README.md +++ b/segment_trees/newyork/README.md @@ -29,12 +29,21 @@ * Else, Dont do anything * Sum all overlapCosts and print solution +## Complexity -**Numer of buildings:** N -**Numer of endpoints:** 2N -**Space Complexity:** O(10N) (~ for segmentTree(2^ceil(log2(4N)))) + O(2N) for priority queue = O(12N) -**AddBuilding Complexity:** O(log 4N) per query -**Total Complexity:** (4N) * O(log 4N) [To add N buildings to Segment Tree and compute overlap cost] +* N - Number of buildings +* 2N - Number of ends of buildings (each building has 2 ends - left and right) + +### Space Complexity + +* **Space Complexity:** O(10N) (~ for segmentTree(2^ceil(log2(4N)))) + O(2N) for priority queue = O(12N) + +### Run Complexity + +* **AddBuilding Complexity:** O(log 4N) per insert into segment tree + * There are 2N buidling ends. Hence 4N-1 segment tree nodes. So insert takes log 4N4 +* **Total Complexity:** (4N) * O(log 4N) + * To add N buildings to Segment Tree and compute overlap cost ## Testcases diff --git a/segment_trees/rangesum/README.md b/segment_trees/rangesum/README.md index e69c92f..2bfda68 100644 --- a/segment_trees/rangesum/README.md +++ b/segment_trees/rangesum/README.md @@ -15,8 +15,17 @@ * For every update applied to range [i, j] `(both i and j included)` * Update the sum-value of all nodes with range [L, R] in segment tree where the segment tree node range falls completely inside [i, j] `i <= (L, R) <= j` +## Complexity + +* N - Number of numbers in array + +### Space Complexity + **Space Complexity:** O(5N) (~ for segmentTree(2^ceil(log2(2N)))) -**Query Complexity:** O(logN) per query [N - Number of items in array] + +### Run Complexity + +* **Query Complexity:** O(logN) per query ## Testcases diff --git a/segment_trees/special_ops/README.md b/segment_trees/special_ops/README.md index def63e8..2ebea05 100644 --- a/segment_trees/special_ops/README.md +++ b/segment_trees/special_ops/README.md @@ -28,9 +28,19 @@ * Cache the results in a sum[] array for kth op * We can only perform kth op after 0 .. k-1 ops. So if new input k is less than max k seen till now, just read the sum from cached sum[] array -**Space Complexity:** O(3 * 5N) (~ for segmentTree(2^ceil(log2(2N))) contains sum, min_index and max_index) -**1 op Complexity:** 5 * O(logN) per op [N - Number of items in array, 2 reads, 2 deletes, 1 insert per op] -**K ops Complexity:** 5K * O(logN) +## Complexity + +* N - Number of numbers in array + +### Space Complexity + +* **Space Complexity:** O(3 * 5N) (~ for segmentTree(2^ceil(log2(2N))) contains sum, min_index and max_index) + +### Run Complexity + +* **1 op Complexity:** 5 * O(logN) per op + * 2 reads, 2 deletes, 1 insert per op -> So 5 ops in total +* **K ops Complexity:** 5K * O(logN) ## Testcases diff --git a/segment_trees/uva_11235_freqvalues/README.md b/segment_trees/uva_11235_freqvalues/README.md index b9750fe..721f636 100644 --- a/segment_trees/uva_11235_freqvalues/README.md +++ b/segment_trees/uva_11235_freqvalues/README.md @@ -14,7 +14,19 @@ * Else, A[i] may repeat till some index k, then k to s[j] from which A[j] will start and go on till j * Count = Max(itemICount, segmentTree.Rmq(k, s[j]-1),itemJCount) -**Space Complexity:** O(N) (for c[N]) + O(N) (for s[N]) + O(5N) (for segmentTree(2^ceil(log2(2N)))) ~ O(7N) -**Query Complexity:** O(logN) +## Complexity + +* N - Number of numbers in array + +### Space Complexity + +* **Space Complexity:** O(7N) + * c[N] : O(N) + * s[N] : O(N) + * segmentTree : O(5N) [2^ceil(log2(2N))] + +### Run Complexity + +* **Query Complexity:** O(logN) diff --git a/tries/cellphone_typing/README.md b/tries/cellphone_typing/README.md index ec9b2c7..9d07495 100644 --- a/tries/cellphone_typing/README.md +++ b/tries/cellphone_typing/README.md @@ -25,10 +25,18 @@ * Else If current letter has siblings or is an EndOfWord, we need user to type a keystroke (so count++) * Else, automcomplet would automatically prompt the only letter at this stage -**Max word length:** L -**Number of words:** N -**Space Complexity:** O(ALPHABET_SIZE * L * N) -**Insert/Search/CountKeyStrokes Complexity:** O(L) per query +## Complexity + +* **Max word length:** L +* **Number of words:** N + +### Space Complexity + +* **Space Complexity:** O(ALPHABET_SIZE * L * N) + +### Run Complexity + +* **Insert/Search/CountKeyStrokes Complexity:** O(L) per query ## Testcases diff --git a/tries/signups/README.md b/tries/signups/README.md index e9b2aae..6dbf08e 100644 --- a/tries/signups/README.md +++ b/tries/signups/README.md @@ -22,10 +22,18 @@ * Else if found, find the first whole number 0,1,..9,10,11,..21,..., which is present in trie * To convert the number to string, use the optimized number conversion function [Otherwise you might get a TLE] -**Max string length of user_id:** L -**Number of users:** N -**Space Complexity:** O(ALPHABET_SIZE * L * N) -**Insert and search complexity:** O(L) +## Complexity + +* **Max string length of user_id:** L [Including user_id number] +* **Number of users:** N + +### Space Complexity + +* **Space Complexity:** O(ALPHABET_SIZE * L * N) + +### Run Complexity + +* **Insert and search complexity:** O(L) ## Testcases diff --git a/union_find/split_wisely/README.md b/union_find/split_wisely/README.md index 2cd23ab..327f66f 100644 --- a/union_find/split_wisely/README.md +++ b/union_find/split_wisely/README.md @@ -20,9 +20,20 @@ **Note:** We've a O(N^2) loop in the solution, but we skip if a parent set has already been processed. So in effect, each person is only checked once and each person's set is only processed once. -**Space Complexity:** O(3N) [p[N], r[N], owe[N]] -**Query Complexity (for M queries):** O(M * Ackermans(N)) [~ O(M * 4) for M queries ~~ O(1) amortized time per find query] -**Total time per set:** O(N * Ackermans(N)) ~~ O(N * 4) [To find root of every person and check if that root's set has sum of owe[] as 0] +## Complexity + +* N - Number of persons + +### Space Complexity + +* **Space Complexity:** O(3N) [p[N], r[N], owe[N]] + +### Run Complexity + +* **Query Complexity (for M queries):** O(M * Ackermans(N)) + * O(M * 4) for M queries ~~ O(1) amortized time per find query +* **Total time per set:** O(N * Ackermans(N)) + * O(N * 4) To find root of every person and check if that root's set has sum of owe[] as 0 ## Testcases diff --git a/union_find/uva_10158_war/README.md b/union_find/uva_10158_war/README.md index d5ce2a7..257cc9c 100644 --- a/union_find/uva_10158_war/README.md +++ b/union_find/uva_10158_war/README.md @@ -19,8 +19,18 @@ In this enemy relationships are also modelled as friendship relationships and we can use a single UFDS set to solve this problem -**Space Complexity:** O(2N) -**Union Time Complexity:** O(1) -**Find Time Complexity for M queries:** O(M * Ackermans(N)) ~ O(M * 4) [Amortized O(1) per find query] +## Complexity + +* N - Number of citizens + +### Space Complexity + +* **Space Complexity:** O(2N) + +### Run Complexity + +* **Union Time Complexity:** O(1) +* **Find Time Complexity for M queries:** O(M * Ackermans(N)) + * O(M * 4) Amortized O(1) per find query