Skip to content

Commit fd71f9e

Browse files
authored
Merge pull request #1286 from Hitesh4278/find-the-n-th-value-after-k-seconds
Added the Solution of Find the N-th Value After K Seconds - Issue No #1227
2 parents d52d502 + 47fec14 commit fd71f9e

File tree

2 files changed

+241
-0
lines changed

2 files changed

+241
-0
lines changed

dsa-problems/leetcode-problems/3100-3199.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,12 @@ export const problems = [
417417
"difficulty": "Medium",
418418
"leetCodeLink": "https://leetcode.com/problems/better-compression-of-string",
419419
"solutionLink": "#"
420+
},
421+
{
422+
"problemName": "3179.Find the N-th Value After K Seconds",
423+
"difficulty": "Medium",
424+
"leetCodeLink": "https://leetcode.com/problems/find-the-n-th-value-after-k-seconds/",
425+
"solutionLink": "/dsa-solutions/lc-solutions/3100-3189/find-the-n-th-value-after-k-seconds"
420426
}
421427
];
422428

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
---
2+
id: find-the-n-th-value-after-k-seconds
3+
title: Find the N-th Value After K Seconds
4+
sidebar_label: 3179. Find the N-th Value After K Seconds
5+
tags:
6+
- Array
7+
- Math
8+
- Simulation
9+
- Combinatorics
10+
- Prefix Sum
11+
description: "This is a solution to the Find the N-th Value After K Seconds problem on LeetCode."
12+
---
13+
14+
## Problem Description
15+
You are given two integers n and k.
16+
17+
Initially, you start with an array a of n integers where `a[i] = 1 for all 0 <= i <= n - 1`. After each second, you simultaneously update each element to be the sum of all its preceding elements plus the element itself. For example, after one second, a[0] remains the same, a[1] becomes a[0] + a[1], a[2] becomes a[0] + a[1] + a[2], and so on.
18+
19+
Return the value of a[n - 1] after k seconds.
20+
21+
Since the answer may be very large, return it modulo 109 + 7.
22+
23+
### Examples
24+
25+
**Example 1:**
26+
27+
```
28+
Input: n = 4, k = 5
29+
30+
Output: 56
31+
32+
Explanation:
33+
34+
Second State After
35+
0 [1,1,1,1]
36+
1 [1,2,3,4]
37+
2 [1,3,6,10]
38+
3 [1,4,10,20]
39+
4 [1,5,15,35]
40+
5 [1,6,21,56]
41+
42+
43+
```
44+
45+
**Example 2:**
46+
```
47+
Input: n = 5, k = 3
48+
49+
Output: 35
50+
51+
Explanation:
52+
53+
Second State After
54+
0 [1,1,1,1,1]
55+
1 [1,2,3,4,5]
56+
2 [1,3,6,10,15]
57+
3 [1,4,10,20,35]
58+
59+
```
60+
61+
### Constraints
62+
63+
- `1 <= n, k <= 1000`
64+
65+
## Solution for Find the N-th Value After K Seconds Problem
66+
### Approach
67+
The problem essentially involves calculating the prefix sum of an array iteratively over a given number of seconds K. Each element in the array represents a value that is derived from summing up all previous elements in the array (including itself) from the previous iteration. The goal is to determine the value of the N-th element in the array after K seconds.
68+
### Steps
69+
- Start with an array of size N where all elements are initialized to 1. This represents the array at time t=0.
70+
- For each second from 1 to K, update the array by calculating the prefix sum for each element. This means that each element at position i (0-indexed) will be updated to the sum of all elements from position 0 to i of the previous iteration.
71+
- After K seconds, return the value of the N-th element in the array.
72+
73+
<Tabs>
74+
<TabItem value="Solution" label="Solution">
75+
76+
#### Implementation
77+
```jsx live
78+
function Solution(arr) {
79+
function valueAfterKSeconds(n, k) {
80+
let arr = new Array(n).fill(1);
81+
const mod = 1000000007;
82+
83+
for (let i = 1; i <= k; i++) {
84+
let prefix = new Array(n).fill(0);
85+
prefix[0] = arr[0] % mod;
86+
for (let j = 1; j < n; j++) {
87+
prefix[j] = (prefix[j - 1] + arr[j]) % mod;
88+
}
89+
arr = prefix;
90+
}
91+
return arr[n - 1];
92+
}
93+
const input = 4;
94+
const k = 5
95+
const output = valueAfterKSeconds(input , k) ;
96+
return (
97+
<div>
98+
<p>
99+
<b>Input: </b>
100+
{JSON.stringify(input)}
101+
</p>
102+
<p>
103+
<b>Output:</b> {output.toString()}
104+
</p>
105+
</div>
106+
);
107+
}
108+
```
109+
110+
#### Complexity Analysis
111+
112+
- Time Complexity: $ O(N*K) $ because of Nested Loops
113+
- Space Complexity: $ O(N) $ because of prefix array
114+
115+
## Code in Different Languages
116+
<Tabs>
117+
<TabItem value="JavaScript" label="JavaScript">
118+
<SolutionAuthor name="@hiteshgahanolia"/>
119+
```javascript
120+
function valueAfterKSeconds(n, k) {
121+
let arr = new Array(n).fill(1);
122+
const mod = 1000000007;
123+
124+
for (let i = 1; i <= k; i++) {
125+
let prefix = new Array(n).fill(0);
126+
prefix[0] = arr[0] % mod;
127+
for (let j = 1; j < n; j++) {
128+
prefix[j] = (prefix[j - 1] + arr[j]) % mod;
129+
}
130+
arr = prefix;
131+
}
132+
return arr[n - 1];
133+
}
134+
135+
```
136+
137+
</TabItem>
138+
<TabItem value="TypeScript" label="TypeScript">
139+
<SolutionAuthor name="@hiteshgahanolia"/>
140+
```typescript
141+
class Solution {
142+
valueAfterKSeconds(n: number, k: number): number {
143+
let arr: number[] = new Array(n).fill(1);
144+
const mod: number = 1000000007;
145+
146+
for (let i = 1; i <= k; i++) {
147+
let prefix: number[] = new Array(n).fill(0);
148+
prefix[0] = arr[0] % mod;
149+
for (let j = 1; j < n; j++) {
150+
prefix[j] = (prefix[j - 1] + arr[j]) % mod;
151+
}
152+
arr = prefix;
153+
}
154+
return arr[n - 1];
155+
}
156+
}
157+
158+
```
159+
</TabItem>
160+
<TabItem value="Python" label="Python">
161+
<SolutionAuthor name="@hiteshgahanolia"/>
162+
```python
163+
class Solution:
164+
def value_after_k_seconds(self, n: int, k: int) -> int:
165+
arr = [1] * n
166+
mod = 10**9 + 7
167+
168+
for i in range(1, k + 1):
169+
prefix = [0] * n
170+
prefix[0] = arr[0] % mod
171+
for j in range(1, n):
172+
prefix[j] = (prefix[j - 1] + arr[j]) % mod
173+
arr = prefix
174+
175+
return arr[n - 1]
176+
```
177+
</TabItem>
178+
<TabItem value="Java" label="Java">
179+
<SolutionAuthor name="@hiteshgahanolia"/>
180+
```java
181+
import java.util.Arrays;
182+
183+
public class Solution {
184+
public int valueAfterKSeconds(int n, int k) {
185+
int[] arr = new int[n];
186+
Arrays.fill(arr, 1);
187+
int mod = 1000000007;
188+
189+
for (int i = 1; i <= k; i++) {
190+
int[] prefix = new int[n];
191+
prefix[0] = arr[0] % mod;
192+
for (int j = 1; j < n; j++) {
193+
prefix[j] = (prefix[j - 1] + arr[j]) % mod;
194+
}
195+
arr = prefix;
196+
}
197+
return arr[n - 1];
198+
}
199+
}
200+
201+
```
202+
203+
</TabItem>
204+
<TabItem value="C++" label="C++">
205+
<SolutionAuthor name="@hiteshgahanolia"/>
206+
```cpp
207+
class Solution {
208+
public:
209+
int valueAfterKSeconds(int n, int k) {
210+
vector<int> arr(n, 1);
211+
int mod = 1e9 + 7;
212+
for (int i = 1; i <= k; i++) {
213+
vector<int> prefix(n, 0);
214+
prefix[0] = arr[0]%mod;
215+
for (int j = 1; j < n; j++) {
216+
prefix[j] = (prefix[j - 1] + arr[j]) % mod;
217+
}
218+
arr = prefix;
219+
}
220+
return arr[n-1];
221+
}
222+
};
223+
```
224+
</TabItem>
225+
</Tabs>
226+
227+
</TabItem>
228+
</Tabs>
229+
230+
## References
231+
232+
- **LeetCode Problem**: [Find the N-th Value After K Seconds](https://leetcode.com/problems/find-the-n-th-value-after-k-seconds/)
233+
234+
- **Solution Link**: [LeetCode Solution](https://leetcode.com/problems/find-the-n-th-value-after-k-seconds/solutions)
235+

0 commit comments

Comments
 (0)