So i am a part of the organizing team for a coding contest in my college. We are planning to use hackerrank for hosting the contest. This is an online contest which can be given by the participants from their homes. So in this, we are going to need any software,application etc that will help us find out whether the solution sent has been copied from a website or something. So if anybody has any idea on how to deal with this issue, kindly let me know
Given two strings s and t, return the number of distinct subsequences of s which equals t. For example, if s = "rabbbit" and t = "rabbit", then the answer is 3 as there are 3 ways you can generate "rabbit" from s.
I came up with following dynamic programming solution with 2D array for memoization:
def numDistinct(self, s: str, t: str) -> int:
s_len = len(s)
t_len = len(t)
dp = [[0 for _ in range(s_len + 1)] for _ in range(t_len + 1)]
for j in range(s_len+1): dp[0][j] = 1
for i in range(1, t_len+1):
for j in range(1, s_len+1):
if t[i-1] == s[j-1]:
dp[i][j] = dp[i-1][j-1] + dp[i][j-1]
else:
dp[i][j] = dp[i][j-1]
return dp[t_len][s_len]
This follows bottom up approach. That is we start building target string from empty string to full target string "checking if string traversed so far is derivable". So we start index 1 onwards. I was guessing if there can be top down approach for the same. That is starting from indexes t_len+1 and s_len+1 down to 0 while "checking if remaining target string can be derived". I am not able to guess what will be the recurrence relation for such top down approach.
PS:
I am able to build below recursive solution which goes from indexes t_len+1 and s_len+1 down to 0. But it is really a bottom up approach disguising as top down. The fact that the problem is symmetric makes easy to start from either ends of indexes and come up with solution which is bottom up. That is why I asked the question for iterative solution, which possibly makes difference between top down and bottom up approach more clear.
from functools import cache
class Solution:
def numDistinct(self, s: str, t: str) -> int:
@cache
def aux(i, j):
if j == -1: return 1
if i == -1: return 0
if s[i] == t[j]:
return aux(i-1,j-1) + aux(i-1, j)
else:
return aux(i-1, j)
return aux(len(s)-1, len(t)-1)
Given two strings s and t, return the number of distinct subsequences of s which equals t. For example, if s = "rabbbit" and t = "rabbit", then the answer is 3 as there are 3 ways you can generate "rabbit" from s.
I tried following bottom up DP approach:
// bottom up - working
def numDistinct(self, s: str, t: str) -> int:
@cache # (i,j): number of distinct subsequences in s[i:] that equal t[j:]
def aux(i, j):
if j == len(t): return 1
if i == len(s): return 0
if s[i] == t[j]:
return aux(i+1, j+1) + aux(i+1, j)
else:
return aux(i+1, j)
return aux(0,0)
This gets accepted. I also tried follow top down DP solution:
// top down - not working
def numDistinct(self, s: str, t: str) -> int:
@cache
def aux(i, j):
if i == -1: return 0
if j == -1: return 1
if s[i] == t[j]:
return aux(i-1,j-1) + aux(i-1, j)
else:
return aux(i-1, j)
return aux(len(s)-1, len(t)-1)
It fails. For strings s = rabbbit and t = rabbit, it gives output = 0, when the output should be 3 (we can form bb in 3 ways from bbb).
I dry run top down approach and realized that I need extra check while returning value from first if:
In above image, each node label is s<sub>i</sub>t<sub>j</sub>. Each edge label is ij. I realized:
for leaf (-1,-1), I should return 1 as it represents both s and t getting exhausted together.
for leaf (-1,0), I should return 0 as it represents s getting exhausted before t
So I changed the return value of first if a bit:
// top down - working
def numDistinct(self, s: str, t: str) -> int:
@cache
def aux(i, j):
if i == -1: return 1 if j == -1 else 0 # if s exhausts before t, return 0, else 1
if j == -1: return 1
if s[i] == t[j]:
return aux(i-1,j-1) + aux(i-1, j)
else:
return aux(i-1, j)
return aux(len(s)-1, len(t)-1)
And this solution gets accepted. Now I was guessing why such check (1 if j == -1 else 0) was not necessary in bottom up DP solution. I tried dry for bottom-up approach too:
And looking at leaves of above image, I feel here too I need similar check in first if. But then how bottom up approach is working without similar check in the first if's return value? What I am missing here?
In other words, how bottom up approach is working with if i==len(s): return 0 and does not need if i==len(s): return (1 if j==len(s) else 0)?
I've had dividends doing this in solving many programs in my own time, but things that seem hard still feel daunting to me.
Maybe I just have to grit through it for a couple of years and it will get better? I never really tried doing like say a hard problem every day or anything. And I notice now that medium problems are significantly easier now than they were 4 years ago. But hard problems generally feel unsolvable for me still, even if I know what techniques I'll likely have to use.
Hello, I am looking for programming challenges related to randomization or approximation. Any practical coding-oriented problems are fine, Even if not exactly alike contests format.
After reading on randomized algorithms and math, I wish to get my hands dirty with coding.
Hey, I'm trying to start a competitive programing club in my Uni and we're looking for resources to learn as we're all new to it, what books, pages or courses would you recommend for us to start with?
Thanks a lot, I hope we can be part of this wonderful community!
I am a dev at a large tech company. I am trying to improve my cognition relating to my programming skills. This could include working memory, executive function etc. The problem is trying to find a way to improve these in a transferable way to programming. I was looking into Dual NBack, thinking it could help in keeping tack of complex pointer movements, though I am unconvinced it is transferable. What mental exercises , apps or programs can do daily that would help me improve?
Given a MxN (M,N<=300) farm. To hydrate the farm, we have to replace dirt cells with water. A dirt cell is hydrated if there's at least one water cell share a same side with it.
We have to hydrate the entire farm. What is the maximum number of dirt cells remain?
Here is the thing, I am looking for a competitive replacement to playing Apex Legends. I love competition and I love competition that I can participate in daily. I want to switch from Apex Legends to coding because I want a competitive outlet that will actually help me in the long run to be able to solve real world problems and prepare me for my career. (Unfortunately the skills from Apex are not as transferable as I would like them to be) However, I heard that some coding competitions are focused on obscure algorithms that may or may not help much in preparing for a career in software development or other related fields.
So does anyone know of an online competitive programming platform that offers daily real world challenges?
I just wanted to ask you whether it makes a huge difference in using a slower langiuage like python in codejam/kickstart type competitions??You sent about a minute ago.Are the runtimes adjusted so that the slower languages also pass the test cases if the code has efficiency or do python programmers have a handicap here??
So I am fairly new to CP but my interest has been growing exponentially in it. And I am now ready to go full force with it!
I am looking for a person who is interested in doing virtual contests (at least once a day) and create something like a study group. I live in the Middle East, so it would be preferable if he/she was living nearby. It doesn't matter the timezone as long as we manage to coordinate the time difference.
Hi, I was wondering if anybody knows any good STEM and Technology competitions, as I am trying to improve my resume. Any suggestions would be extremely helpful! I am mostly looking for competitions that do with solutions in technology, and maybe some business aspects. If anyone knows any good STEM internships, scholarships or Coding summer camps that would be helpful as well. Thanks!
A Codeforces parser for a specific contest identified by Id. It can be used for uninterrupted and smooth participation in a virtual contest, upsolving a practice contest or even a live contest.
Have you ever thought of practicing a particular round - say Div3 and felt lazy about creating a proper directory structure for the contest, having proper files with the names of the problems?
Have you ever thought of getting a visual representation of the Accepted ratio of the problems in a CF contest before you virtually participate in it or even normally practice it?
If you have, then my python script is at your disposal. It will save you a huge amount of unnecessary time wasted.