Esercizi orale Flashcards
(40 cards)
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]]
Problem: coin change
There are n types of coins 🪙, of which we have an unlimited number. The values of these n coins are stored in an array C=[c1, … ,cn].
Given an amount of money k, we want to count how many ways we have to reach that amount of money k using the different coins.
Equazione di ricorrenza:
M[i][j] = M[i - 1] [j] + M[i][j - Ci]
Problem: Largest independent set on trees 🟢
Given a tree T with n nodes, find one of its largest independent sets.
A set of nodes is defined as “independent”, if for each node in the set there is no edge connecting it to the other nodes in the set.
equazione di ricorrenza LIS(u) = max( 1 + Lis(u.grandchildren), List(u.children))
Problem: Longest Increasing Subsequence (LIS)
Given an array of n integers, S, we want to find the length of its longest increasing subarray. We could have more than one subarray with the same length.
DAG
Nˆ2 LIS[i] = 1+ max k<i ( LIS[k] tale che S[k] < S[i])
n log n creiamo BBST dove inseriamo elemento, da quello aggiungiamo 1 cercando predecessore, poi se successore ha LIS minore possiamo eliminare successore.
Problem: puzzle
In this problem we have n pairs of coins:
Select k coins to maximize the total value, but you can select the second coin of the pair Ci,2 only if the first one Ci,1 is already select.
Problem: Maximum path sum 🟢
Given a binary tree T with n nodes where every node contains a weight w (just a value stored into the node), find the maximum sum of a path from one leaf to another leaf. In other words, for every possible pair of leaf nodes we want to report the maximum sum of the path between those nodes into the pair, so given two leaves we want to consider every possible path among them and report the maximum sum.
Per capire best path partiamo dal concetto del lowest common ancestor, per calcolarlo ritorno due valori, best so far e best to a leaf. Effettuo chiamata ricorsiva a sinistra e destra e verifico qual’é il best to a left, e se é maggiore quello oppure il path destra sinistra
Problem: Frogs and mosquitoes (Dynamic 1D Point Location) 🟢
In this problem we have n frogs 🐸lying on the axis x. Each frog has a value t which is the length of its tongue 👅. Then, there are m mosquitos 🦟; each mosquitos j lands at the position bj and has a value aj.
A frog i is able to eat a mosquitos j if bj is in the range [xi , xi+ti].
Ordino rane, applico sweep line e le addo a bbst, quando addo verifico se ci sono overlaps. O(n log n)
Verifico mosche che possono essere mangiate, quando mangio mosca effettuo increasing e verifico se posso mangiare uneaten O((n + m) log n)
Creo array delle mosche non mangiate
O(m log m)
Problem: Longest K-good segments (w/ Two Pointers) 🟢
Given A[1,n] of integers and a value k, a subarray is defined as “k-good” if it contains no more than k different integers. In this problem we want to find the longest k-good subarray.
Two pointers
Problem #1: Ilya and Queries
You’ve got string s=s1, s2 , … , sn (n is the length of the string), consisting only of characters “a” and “b” and q queries. Each query is described by a pair of integers (i,j, 1 ≤ i ≤ j ≤ n ).
The answer to the query(i,j) is the number of such integers k[i,j] such that Sk=Sk+1.
Prefix sum, 1 se Sk=Sk+1
R(i,j) = Prefix(j) - Prefix(i - 1)