From 28542dffabcebb81a5f26bf469e0e522f6912bf2 Mon Sep 17 00:00:00 2001 From: Chitwant Date: Mon, 31 Jul 2017 15:52:57 -0700 Subject: [PATCH 1/2] big o sol --- big_o_exercise/bigOExcercise.txt | 71 ++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 big_o_exercise/bigOExcercise.txt diff --git a/big_o_exercise/bigOExcercise.txt b/big_o_exercise/bigOExcercise.txt new file mode 100644 index 0000000..9a943d8 --- /dev/null +++ b/big_o_exercise/bigOExcercise.txt @@ -0,0 +1,71 @@ +# Big O Notation Exercises + +### Part 1 + +Simplify the following big O expressions as much as possible: + +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(nlog(n)) +8. O(2^n + n^2) > O(n^2) +9. O(5 + 3 + 1) > O(1) +10. O(n + n^(1/2) + n^2 + n * log(n)^10) > O(nlog(n)^10) + +### Part 2 + +Determine the time and space complexities for each of the following functions. If you're not sure what these functions do, copy and paste them into the console and experiment with different inputs! + + +// 1. + +function logUpTo(n) { + for (var i = 1; i <= n; i++) { + console.log(i); + } +} + +// 2. + +function logAtMost10(n) { + for (var i = 1; i <= Math.min(n, 10); i++) { + console.log(i); + } +} + +// 3. + +function logAtLeast10(n) { + for (var i = 1; i <= Math.max(n, 10); i++) { + console.log(i); + } +} + +// 4. + +function onlyElementsAtEvenIndex(array) { + var newArray = Array(Math.ceil(array.length / 2)); + for (var i = 0; i < array.length; i++) { + if (i % 2 === 0) { + newArray[i / 2] = array[i]; + } + } + return newArray; +} + +// 5. + +function subtotals(array) { + var subtotalArray = Array(array.length); + for (var i = 0; i < array.length; i++) { + var subtotal = 0; + for (var j = 0; j <= i; j++) { + subtotal += array[j]; + } + subtotalArray[i] = subtotal; + } + return subtotalArray; +} From 68d52fc3e2eb406aa9a45b385cc2447071b80b07 Mon Sep 17 00:00:00 2001 From: Chitwant Date: Mon, 31 Jul 2017 16:09:57 -0700 Subject: [PATCH 2/2] big o sol modified --- big_o_exercise/bigOExcercise.txt | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/big_o_exercise/bigOExcercise.txt b/big_o_exercise/bigOExcercise.txt index 9a943d8..e183a8d 100644 --- a/big_o_exercise/bigOExcercise.txt +++ b/big_o_exercise/bigOExcercise.txt @@ -11,9 +11,9 @@ Simplify the following big O expressions as much as possible: 5. O(n + n + n + n) > O(n) 6. O(1000 * log(n) + n) > O(n) 7. O(1000 * n * log(n) + n) > O(nlog(n)) -8. O(2^n + n^2) > O(n^2) +8. O(2^n + n^2) > O(2^n) 9. O(5 + 3 + 1) > O(1) -10. O(n + n^(1/2) + n^2 + n * log(n)^10) > O(nlog(n)^10) +10. O(n + n^(1/2) + n^2 + n * log(n)^10) > O^2) ### Part 2 @@ -28,6 +28,9 @@ function logUpTo(n) { } } +Time Complexity: O(n) +Space Complexity: O(1) + // 2. function logAtMost10(n) { @@ -36,6 +39,9 @@ function logAtMost10(n) { } } +Time Complexity: O(1) +Space Complexity: O(1) + // 3. function logAtLeast10(n) { @@ -44,6 +50,9 @@ function logAtLeast10(n) { } } +Time Complexity: O(1) if n < 10. O(n) if n > 10 +Space Complexity: O(1) + // 4. function onlyElementsAtEvenIndex(array) { @@ -56,6 +65,9 @@ function onlyElementsAtEvenIndex(array) { return newArray; } +Time Complexity: O(n) +Space Complexity: O(n) + // 5. function subtotals(array) { @@ -69,3 +81,6 @@ function subtotals(array) { } return subtotalArray; } + +Time Complexity: O(n^2) +Space Complexity: O(n)