I was asked this 2 questions. Couldn't solve it in optimal TC/SC. Can anyone please explain?
Also, please suggest from where to learn how to calculate TC? As I was struggling a bit.
0 0 0
0 0 0
0 0 0
0 0 0
arr = [[0,0], [1,1], [2,2]]
we can mark 0,0 as 1,
1 0 0
0 0 0
0 0 0
0 0 0
We can still reach the last row using 0,1 and 0,2.
we can mark 1,1 as 1.
1 0 0
0 1 0
0 0 0
0 0 0
we can still use 0,2 to reach last row.
mark 2,2 as 1
1 0 0
0 1 0
0 0 1
0 0 0
There's no way to reach the last row.
So ans is 2.
I suggested Brute Force way of marking every int[] from list of indices as 1 and check if we can still reach last row. What would be the TC? I said it was exponential, but he said to reconsider.
e.g
s = aaabb
p = ab
arr = [3,2,0,4]
'.' shows a char is deleted here.
we can delete s[3] i.e b, so now s = aaa.b, p is still a subsequence of s, ans = 1
we can delete s[2] i.e a, so now s = aa..b, p is still a subsequence of s, ans = 2
we can delete s[0] i.e b, so now s = .a..b, p is still a subsequence of s, ans = 3
we can delete s[4] i.e b, so now s = .a.., p is not a subsequence of s, ans = 3
return 3Again, gave a Brute Force solution that for each index in the arr we can delete it and check if it's a subsequence.
Please help.