What is the worst case time complexity of the following code:int memo[101][101];int findMinPath(vector<vector<int> >& V, int r, int c) { int R = V.size(); int C = V[0].size(); if (r >= R || c >= C) return 100000000; // Infinity if (r == R - 1 && c == C - 1) return 0; if (memo[r][c] != -1) return memo[r][c]; memo[r][c] = V[r][c] + min(findMinPath(V, r + 1, c), findMinPath(V, r, c + 1)); return memo[r][c];}Callsite : memset(memo, -1, sizeof(memo));findMinPath(V, 0, 0);Assume R = V.size() and C = V[0].size() and V has positive elements
Question
What is the worst case time complexity of the following code:int memo[101][101];int findMinPath(vector<vector<int> >& V, int r, int c) { int R = V.size(); int C = V[0].size(); if (r >= R || c >= C) return 100000000; // Infinity if (r == R - 1 && c == C - 1) return 0; if (memo[r][c] != -1) return memo[r][c]; memo[r][c] = V[r][c] + min(findMinPath(V, r + 1, c), findMinPath(V, r, c + 1)); return memo[r][c];}Callsite : memset(memo, -1, sizeof(memo));findMinPath(V, 0, 0);Assume R = V.size() and C = V[0].size() and V has positive elements
Solution
The worst case time complexity of the given code is O(R*C).
Here's why:
The function findMinPath is a recursive function that is called at most twice for each cell in the 2D grid V. However, due to the use of memoization, each cell is actually computed only once and then stored in the memo table. Therefore, the time complexity is proportional to the number of cells in the grid, which is R*C.
In the worst case, every cell in the grid will be computed once, hence the time complexity is O(R*C).
Similar Questions
What is the worst case time complexity of the following code:int findMinPath(vector<vector<int> > &V, int r, int c) { int R = V.size(); int C = V[0].size(); if (r >= R || c >= C) return 100000000; // Infinity if (r == R - 1 && c == C - 1) return 0; return V[r][c] + min(findMinPath(V, r + 1, c), findMinPath(V, r, c + 1));}Assume R = V.size() and C = V[0].size().
What is the worst case time complexity of the following code :/* * V is sorted * V.size() = N * The function is initially called as searchNumOccurrence(V, k, 0, N-1) */int searchNumOccurrence(vector<int> &V, int k, int start, int end) { if (start > end) return 0; int mid = (start + end) / 2; if (V[mid] < k) return searchNumOccurrence(V, k, mid + 1, end); if (V[mid] > k) return searchNumOccurrence(V, k, start, mid - 1); return searchNumOccurrence(V, k, start, mid - 1) + 1 + searchNumOccurrence(V, k, mid + 1, end);}NOTE : This question involves recursion which will be explained later in topic Backtracking. So, if you are not able to approach this question now, you can give it a try later.
What is the worst-case running time of this algorithm when the input vector nums has size 𝑛n?C++12345678910bool containsDuplicate(const std::vector<int>& nums) { std::set<int> seen; for (int x : nums) { if (seen.contains(x)) { return true; } seen.insert(x); } return false;}Θ(log𝑛)Θ(logn)Θ(𝑛2)Θ(n 2 )Θ(𝑛log𝑛)Θ(nlogn)Θ(𝑛)Θ(n)Submit
Which of the following operations has the WORST time complexity for an array of N elements? Accessing an element by index. Inserting an element at the beginning. Deleting an element from the middle. Searching for an element using linear search.
The time complexity of the following code snippet isc=0; while (n>=1) {for(i=1;i>=n;i++){ c++;}n=n/2;}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.