Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

week 1, day 1 hw - big O exercises #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions big_o_exercise/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

Simplify the following big O expressions as much as possible:

1. `O(n + 10)`
2. `O(100 * n)`
3. `O(25)`
4. `O(n^2 + n^3)`
5. `O(n + n + n + n)`
6. `O(1000 * log(n) + n)`
7. `O(1000 * n * log(n) + n)`
8. `O(2^n + n^2)`
9. `O(5 + 3 + 1)`
10. `O(n + n^(1/2) + n^2 + n * log(n)^10)`
1. `O(n + 10)` -> O(n)
2. `O(100 * n)` -> O(n)
3. `O(25)` -> O(1)
4. `O(n^2 + n^3)` -> O(n^3)
5. `O(n + n + n + n)` -> O(n)
6. `O(1000 * log(n) + n)` -> O(n)
7. `O(1000 * n * log(n) + n)` -> O(n)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

careful with this one - which grows faster, n or nlog(n)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, right. I overlooked the '*' between n and log(n)

8. `O(2^n + n^2)` -> O(n^2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2^n grows more quickly than n^2, but we didn't talk about exponential growth in class so i wouldn't sweat this one too much.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... oh wow! I totally missed that.

9. `O(5 + 3 + 1)` -> O(1)
10. `O(n + n^(1/2) + n^2 + n * log(n)^10)` -> O(n^2)

### Part 2

Expand All @@ -28,22 +28,28 @@ function logUpTo(n) {
console.log(i);
}
}
// TIME: O(n)
// SPACE: O(1)

// 2.
// 2.

function logAtMost10(n) {
for (var i = 1; i <= Math.min(n, 10); i++) {
console.log(i);
}
}
// TIME: O(1)
// SPACE: O(1)

// 3.
// 3.

function logAtLeast10(n) {
for (var i = 1; i <= Math.max(n, 10); i++) {
console.log(i);
}
}
// TIME: O(n)
// SPACE: O(1)

// 4.

Expand All @@ -56,8 +62,10 @@ function onlyElementsAtEvenIndex(array) {
}
return newArray;
}
// TIME: O(n)
// SPACE: O(n)

// 5.
// 5.

function subtotals(array) {
var subtotalArray = Array(array.length);
Expand All @@ -70,4 +78,8 @@ function subtotals(array) {
}
return subtotalArray;
}

subtotals([0,1,2,3,4,5])
// TIME: O(n^2)
// SPACE: O(n)
```