Esercizi orale Flashcards
Sliding Windows Maxima: Given an array containing n elements and a value that represents the window capacity, find 𝐴 𝑘
the maximum element for each contiguous window.
Trivial solution:
Better solution 1: using Heap
Better solution 2: using BBST
Optimal solution
Proving Correctness
Problem: through the desert: The goal of this problem is to determine the minimum amount of fuel needed at the start of the journey in order to reach the end of a street of length units.
O(k log n), k numero stazioni
Inversion count: Count the number of elements to sort, A[I] > A[j] and I < J
Per riportarle, insertion sort (#inversioni + n)
Per contarle merge sort (n log n)
Limite teorico del sorting basato su confronti
O(n log n)
Linear time sorting
Counting sort O(n + m)
Linear time sorting per integers
Radix sort O(n + k) dove k è ordine di grandezza per valore max in quell’ordine
Max Segment Intersection: Dati k segmenti ed un array di dimensione n, lo scopo è trovare il punto con massimo numero di intersezioni tra i segmenti.
O(n log n) creando struttura dati dove inseriamo segmenti.
Fibonacci Numbers
Calculating the n-th term of the Fibonacci series is the “hello world!” of dynamic programming.
Recall that, the series can be calculated in this way:
Soluzione base
Bottom up
Top down
Problem: Rod cutting
Given a rod of length n cm and an array of prices P that includes prices of all pieces of size smaller than n. Determine the maximum value obtainable by cutting up the rod and selling the pieces.
Equazione di ricorrenza k varia, R(n) = max( R(k) + P n - k)
DAG
Facebook Robber
Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police 👮.
Using Dag
Equazione di ricorrenza f(i) = max(f(i - 1) + val, f(i -2))
Snake e ladders
Minimum cost path Given a matrix M (n m) with integers, the goal is to compute the path with the minimum cost which goes from the top-left corner to the bottom-right corner by moving only down or right. The target time complexity must be (n*m).
Creo Matrice M1 che contiene in ogni casella dimensione min path.
eq ricorrenza W[i][j] = M[i][j] + min ( W[i - 1] [j], W[i][j - 1])
Problem: IWGBS - 0110SS 🟢
Giggi has to count in how many ways he can make N digit numbers that are formed by ones and zeroes. But zeroes can not be next to each other. Help him in how many different numbers he can make.
Problem: Longest Common Subsequence (LCS) 🟢
Given two strings S1, S2 where len(S1) = n and len(S2) = m find the longest common subsequence. The target time complexity is (n*m).
Matrice che contiene chars increasing.
Problem: 0/1 knapsack problem 🟢
We have n items and a knapsack with a capacity C. Each item i has a value vi and a weight wi.
The goal is to select a subset of items, with a total weight that is at most C maximizing the overall value. Note that each item can be taken only once, this is the reason why this problem is denoted as “0/1”. The target time complexity (nC).
Equazione di ricorrenza: M[i][j] = max( M[i-1][j]. M[i-1][j - wi])
Problem: subset sum problem 🟢
We have a set S of n integers and a target (integer) v. We want to know if there is a subset of S whose sum is equal to v; each element can be picked once.
Equazione di ricorrenza M[i][j] = M[i-1][j] or M[i-1][j - S[i]]