Skip to content

Commit

Permalink
📚 Updated readme [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
kidambisrinivas committed Apr 19, 2019
1 parent 8339e36 commit 83f2cb1
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 37 deletions.
16 changes: 10 additions & 6 deletions lowest_common_ancestor/uva_12238_ants_colony/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@

## 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[]]
* Build a MIN segment tree on levels array L[] to give min_index of node with minimum level between first occurence of 2 nodes in Euler tour array E[]
* 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))
Expand Down
14 changes: 12 additions & 2 deletions priority_queues/argus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 14 additions & 2 deletions priority_queues/hackerearth_monkniq/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]


13 changes: 11 additions & 2 deletions segment_trees/help_alice/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 14 additions & 5 deletions segment_trees/newyork/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 10 additions & 1 deletion segment_trees/rangesum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 13 additions & 3 deletions segment_trees/special_ops/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 14 additions & 2 deletions segment_trees/uva_11235_freqvalues/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


16 changes: 12 additions & 4 deletions tries/cellphone_typing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 12 additions & 4 deletions tries/signups/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 14 additions & 3 deletions union_find/split_wisely/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 13 additions & 3 deletions union_find/uva_10158_war/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


0 comments on commit 83f2cb1

Please sign in to comment.