Skip to content

Commit 6104ebc

Browse files
author
Wakidur Rahaman
committed
polynomialRule
1 parent f6c2be8 commit 6104ebc

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/big-O-complexities/big-o-rule.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/**
2+
* Big-O is important for analyzing and comparing the efficiencies of algorithms.
23
* Big-O Rule
34
*
45
* 1. Coefficient rule
@@ -98,7 +99,7 @@ function sumRule(boxes: string[], items: number[]) {
9899
*/
99100

100101
/**
101-
* Product Rule
102+
* 3. Product Rule
102103
* The product rule simply states how Big-Os can be multiplied.
103104
*/
104105

@@ -129,3 +130,20 @@ function productRuleTwo(n: number) {
129130
* O(n^2)
130131
* O(n^2) is called Quadratic Time
131132
*/
133+
134+
/**
135+
* 4. Polynomial Rule
136+
* If f(n) is a polynomial of degree k, then f(n) is O(nˆk).
137+
* The following code block has only one for loop with quadratic time complexity f(n) = nˆ2 because line 4 runs n*n iterations
138+
*/
139+
140+
function polynomialRule(n: number) {
141+
let count = 0;
142+
for (let i = 0; i < n * n; i++) {
143+
count += 1;
144+
}
145+
return count;
146+
}
147+
148+
// O(n^2)
149+
// O(n^n)

0 commit comments

Comments
 (0)