symbolism to describe the asymptotic behaviour of functions Examples: O(n·log n), O(2n) - it’s relative to the input: on x axis the input size, on y axis the time - it’s an upper bound - we’re interested only in the highest exponential: O(n2 + n2/2 + 100n) = O(n2)
complexity of this method? Runtime complexity is O(n) And which is the space complexity? int sum(int n) { if (n==0) { return 0; } return n + sum(n-1); }
complexity of this method? Runtime complexity is O(n) And which is the space complexity? Space complexity is O(1) int sum(int n) { if (n==0) { return 0; } return n + sum(n-1); }
int[] sums = new int[n+1]; sums[1] = 1; for (int i=2; i==n; i=+) { sums[i] = sums[i-1] + i; } return sums[n]; } Which is the runtime complexity of this method? Runtime complexity is O(n)
int[] sums = new int[n+1]; sums[1] = 1; for (int i=2; i==n; i=+) { sums[i] = sums[i-1] + i; } return sums[n]; } Which is the runtime complexity of this method? And which is the space complexity? Runtime complexity is O(n)
int[] sums = new int[n+1]; sums[1] = 1; for (int i=2; i==n; i=+) { sums[i] = sums[i-1] + i; } return sums[n]; } Which is the runtime complexity of this method? And which is the space complexity? Space complexity is O(n) Runtime complexity is O(n)
complexity of this method? And which is the space complexity? Runtime complexity is O(n) Runtime complexity is O(n) int sum(int n) { int result = 1; for (int i=2; i==n; i=+) { result += i; } return result; }
complexity of this method? And which is the space complexity? Space complexity is O(1) Runtime complexity is O(n) Runtime complexity is O(n) int sum(int n) { int result = 1; for (int i=2; i==n; i=+) { result += i; } return result; }
return n * (n+1) / 2; } Which is the runtime complexity of this method? Runtime complexity is O(1) And which is the space complexity? Space complexity is O(1)
int n = array.length; for (int i = 0; i < n; i=+) { for (int j = 1; j < n; j=+) { if (array[j-1] < array[j]) { swap(array, j, j-1); } } } } Which is the runtime complexity of this method?
int n = array.length; for (int i = 0; i < n; i=+) { for (int j = 1; j < n; j=+) { if (array[j-1] < array[j]) { swap(array, j, j-1); } } } } Which is the runtime complexity of this method? Runtime complexity is O(n2)
int n = array.length; for (int i = 0; i < n; i=+) { for (int j = 1; j < n; j=+) { if (array[j-1] < array[j]) { swap(array, j, j-1); } } } } Which is the runtime complexity of this method? Runtime complexity is O(n2) And which is the space complexity?
int n = array.length; for (int i = 0; i < n; i=+) { for (int j = 1; j < n; j=+) { if (array[j-1] < array[j]) { swap(array, j, j-1); } } } } Which is the runtime complexity of this method? Runtime complexity is O(n2) And which is the space complexity? Space complexity is O(1)
if (n == 2) { return 1; } return fibonacci(n-1) + fibonacci(n-2); } Which is the runtime complexity of this method? Runtime complexity is O(2n) And which is the space complexity?
if (n == 2) { return 1; } return fibonacci(n-1) + fibonacci(n-2); } Which is the runtime complexity of this method? Runtime complexity is O(2n) And which is the space complexity? Space complexity is O(1)
would fit in this room? - Golf ball radius is about 2.5cm - Sphere volume is: 4/3 · π · r3 - Golf ball volume is about 4/3 · 3.14 · 15 ~= 60 cm3 - This room is about 10m · 20m · 3m - Room volume is about 600 m3
would fit in this room? - Golf ball radius is about 2.5cm - Sphere volume is: 4/3 · π · r3 - Golf ball volume is about 4/3 · 3.14 · 15 ~= 60 cm3 - This room is about 10m · 20m · 3m - Room volume is about 600 m3 600 m3 / 60cm3 = 600 · 106 cm3 / 60cm3 ≈ 107 golf balls
one contains only apples, one contains only oranges, and one contains both apples and oranges. The boxes have been incorrectly labeled such that no label identifies the actual contents of the box it labels. Opening just one box, and without looking in the box, you take out one piece of fruit. By looking at the fruit, how can you immediately label all of the boxes correctly?
one contains only apples, one contains only oranges, and one contains both apples and oranges. The boxes have been incorrectly labeled such that no label identifies the actual contents of the box it labels. Opening just one box, and without looking in the box, you take out one piece of fruit. By looking at the fruit, how can you immediately label all of the boxes correctly? Label A O A+O A O A+O A A+O O O A+O A O A A+O A+O A O A+O O A
one contains only apples, one contains only oranges, and one contains both apples and oranges. The boxes have been incorrectly labeled such that no label identifies the actual contents of the box it labels. Opening just one box, and without looking in the box, you take out one piece of fruit. By looking at the fruit, how can you immediately label all of the boxes correctly? Label A O A+O A O A+O A A+O O O A+O A O A A+O A+O A O A+O O A
one contains only apples, one contains only oranges, and one contains both apples and oranges. The boxes have been incorrectly labeled such that no label identifies the actual contents of the box it labels. Opening just one box, and without looking in the box, you take out one piece of fruit. By looking at the fruit, how can you immediately label all of the boxes correctly? Label A O A+O O A+O A A+O A O
one contains only apples, one contains only oranges, and one contains both apples and oranges. The boxes have been incorrectly labeled such that no label identifies the actual contents of the box it labels. Opening just one box, and without looking in the box, you take out one piece of fruit. By looking at the fruit, how can you immediately label all of the boxes correctly? Label A O A+O O A+O A A+O A O Just pick from the box labeled A+O and you’re sure about all the labels.
for gaining speed (and some useful tutorials) • geeksforgeeks.org / leetcode.com - for detailed explanations of solutions • www.codingame.com - for problems in gaming environment • www.interviewbit.com - for following a gamificated program of practice • pramp.com – for practicing real interviews with peers