Created
September 27, 2023 23:24
-
-
Save TheSaviourEking/1fa2deae3eb098299be71476b55789ad to your computer and use it in GitHub Desktop.
A file to test my understanding of Space and Time Complexities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
animals = [ | |
'alligator', 'bear', 'cat', 'dog', 'elephant', | |
'fish', 'goose', 'hamster', 'iguana', 'jaguar', 'kangaroo' | |
]; | |
// Count how many animals are in the list | |
// Time complexity: O(?) // O(n) | |
// Space complexity: O(?) // o(1) | |
function countAnimals(animals) { | |
let count = 0; | |
for (let i = 0; i < animals.length; i++) { | |
count++; | |
} | |
return count; | |
} | |
// Count how many animals are in the list | |
// Time complexity: O(?) // o(1); | |
// Space complexity: O(?) // O(1) | |
function countAnimals2(animals) { | |
return animals.length; | |
} | |
// Print the first 10 animals in the list | |
// Time complexity: O(?) // O(1) | |
// Space complexity: O(?) // O(1) | |
function printTenAnimals(animals) { | |
if (animals.length < 10) { | |
throw Error("not enough animals") | |
} | |
console.log(animals[0]); | |
console.log(animals[1]); | |
console.log(animals[2]); | |
console.log(animals[3]); | |
console.log(animals[4]); | |
console.log(animals[5]); | |
console.log(animals[6]); | |
console.log(animals[7]); | |
console.log(animals[8]); | |
console.log(animals[9]); | |
} | |
// Print out all the animals | |
// Time complexity: O(?) // O(n) | |
// Space complexity: O(?) // O(1) | |
function printAnimals(animals) { | |
for (let i = 0; i < animals.length; i++) { | |
console.log(animals[i]); | |
} | |
} | |
// Print out all the animals twice | |
// Time complexity: O(?) // O(n) | |
// Space complexity: O(?) // O(1) | |
function printAnimalsTwice(animals) { | |
for (let i = 0; i < animals.length; i++) { | |
console.log(animals[i]); | |
} | |
for (let j = 0; j < animals.length; j++) { | |
console.log(animals[j]); | |
} | |
} | |
// Print all possible pairs of animals | |
// Time complexity: O(?) // O(n^2) | |
// Space complexity: O(?) // O(1) | |
function printAnimalPairs(animals) { | |
for (let i = 0; i < animals.length; i++) { | |
for (let j = 0; j < animals.length; j++) { | |
console.log(`${animals[i]} - ${animals[j]}`); | |
} | |
} | |
} | |
// Return an array containing all possible pairs of animals | |
// Time complexity: O(?) // O(n^2) | |
// Space complexity: O(?) // O(n) | |
function getAnimalPairs(animals) { | |
const pairs = []; | |
for (let i = 0; i < animals.length; i++) { | |
for (let j = 0; j < animals.length; j++) { | |
pairs.push([animals[i], animals[j]]); | |
} | |
} | |
return pairs; | |
} | |
// Return an array containing all possible pairs of animals | |
// Time complexity: O(?) // O(n^3) | |
// Space complexity: O(?) // O(n) | |
function getAnimalTriples(animals) { | |
const triples = []; | |
for (let i = 0; i < animals.length; i++) { | |
for (let j = 0; j < animals.length; j++) { | |
for (let k = 0; k < animals.length; k++) { | |
triples.push([animals[i], animals[j], animals[k]]); | |
} | |
} | |
} | |
return triples; | |
} | |
// Returns the index of the animal if it is in the array | |
// Returns -1 if it is not in the array | |
// Time complexity: O(?) // O(n) | |
// Space complexity: O(?) // O(1) | |
function findAnimal(animals, target) { | |
for (let i = 0; i < animals.length; i++) { | |
if (animals[i] === target) return i; | |
} | |
return -1; | |
} | |
/** | |
Here's Bing Ai's evaluation of the Complexities, Please confirm if it's right! | |
1. `countAnimals(animals)`: Time complexity is **O(n)** because you're iterating through the list once, and space complexity is **O(1)** because you're only using a single variable to store the count. | |
2. `countAnimals2(animals)`: Time complexity is **O(1)** because you're directly accessing the length property of the array, and space complexity is also **O(1)** because no additional space is used. | |
3. `printTenAnimals(animals)`: Both time and space complexities are **O(1)** because you're only printing a fixed number of elements. | |
4. `printAnimals(animals)`: Time complexity is **O(n)** because you're iterating through all the animals, and space complexity is **O(1)** because no additional space is used. | |
5. `printAnimalsTwice(animals)`: Time complexity is **O(n)** because you're iterating through all the animals twice, but since constants are ignored in Big O notation, it remains O(n). Space complexity is **O(1)**. | |
6. `printAnimalPairs(animals)`: Time complexity is **O(n^2)** because for each animal, you're printing pairs with every other animal. Space complexity is **O(1)** because no additional space is used. | |
7. `getAnimalPairs(animals)`: Time complexity is **O(n^2)** for the same reason as above, but space complexity is now **O(n^2)** because you're storing all pairs in an array. | |
8. `getAnimalTriples(animals)`: Time complexity is **O(n^3)** because you're generating triples of animals, and space complexity is also **O(n^3)** because you're storing all triples in an array. | |
9. `findAnimal(animals, target)`: Time complexity is **O(n)** because in the worst case, you might have to look at every animal in the list. Space complexity is **O(1)**. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment