3179. Find the N-th Value After K Seconds | Pascal's Triangle | DP | Bottom Up Optimised DP
HTML-код
- Опубликовано: 30 сен 2024
- In this video, I'll talk about how to solve Leetcode 3179. Find the N-th Value After K Seconds | Pascal's Triangle | DP | Bottom Up Optimised DP
Let's Connect:
📱Discord (Join Community) : / discord
📝Linkedin: / aryan-mittal-0077
📸 Instagram: / codewitharyanbhai
💻 Twitter - / aryan_mittal007
🤖 Github: github.com/ary...
About Me:
I am Aryan Mittal - A Software Engineer in Goldman Sachs, Speaker, Creator & Educator. During my free time, I create programming education content on this channel & also how to use that to grow :)
✨ Hashtags ✨
#programming #Interviews #leetcode #faang #maang #datastructures #algorithms
Great Video.
cant we just do the same using for loops
vectorarr(n,1);
while(k--)
{
for(int i=1;i
heyy @Aryan Mittal this was the code i had written during contest.
int valueAfterKSeconds(int n, int k) {
vector nums(n,1);
for(int i = 0;i
actually we dont need to take two vectors , only one is enough
actually we don't need a vector if we do it math in O(n) complexity.
class Solution:
def valueAfterKSeconds(self, n: int, k: int) -> int:
ans = 1
mod = 1000000007
for i in range(0,k) :
ans = (ans*(n+i))%mod
ans = (ans*pow(i+1,mod-2,mod))%mod
return ans
I came with this solution in the contest in O(n) time complexity aryan if wonder if you could explain about modular inverse concept
cauz the actual logic i thought is this
class Solution:
def valueAfterKSeconds(self, n: int, k: int) -> int:
ans = 1
mod = 1000000007
for i in range(0,k) :
ans = (ans*(n+i))%mod
ans = (ans//(i+1))
return ans
but due to some reasons i got wrong answer I want to know why
and i end up using this solution after hearing about modular inverse concept
class Solution:
def valueAfterKSeconds(self, n: int, k: int) -> int:
ans = 1
mod = 1000000007
for i in range(0,k) :
ans = (ans*(n+i))%mod
ans = (ans*pow(i+1,mod-2,mod))%mod
return ans
aryan you wrote code worng
class Solution {
public:
int valueAfterKSeconds(int n, int k) {
int MOD=1e9+7;
vector a(n,1);
for(int i=0;i
koi mathematical formula?
nice!