From 47fec146ca7d8095235ba4ac42184383d91cf799 Mon Sep 17 00:00:00 2001 From: Hitesh4278 <97452642+Hitesh4278@users.noreply.github.com> Date: Sat, 15 Jun 2024 09:49:14 +0000 Subject: [PATCH] Added the Solution of Find the N-th Value After K Seconds --- dsa-problems/leetcode-problems/3100-3199.md | 6 + ...179-find-the-n-th-value-after-k-seconds.md | 235 ++++++++++++++++++ 2 files changed, 241 insertions(+) create mode 100644 dsa-solutions/lc-solutions/3100-3199/3179-find-the-n-th-value-after-k-seconds.md diff --git a/dsa-problems/leetcode-problems/3100-3199.md b/dsa-problems/leetcode-problems/3100-3199.md index 26b5e1fff..f05bb0620 100644 --- a/dsa-problems/leetcode-problems/3100-3199.md +++ b/dsa-problems/leetcode-problems/3100-3199.md @@ -417,6 +417,12 @@ export const problems = [ "difficulty": "Medium", "leetCodeLink": "https://leetcode.com/problems/better-compression-of-string", "solutionLink": "#" + }, + { + "problemName": "3179.Find the N-th Value After K Seconds", + "difficulty": "Medium", + "leetCodeLink": "https://leetcode.com/problems/find-the-n-th-value-after-k-seconds/", + "solutionLink": "/dsa-solutions/lc-solutions/3100-3189/find-the-n-th-value-after-k-seconds" } ]; diff --git a/dsa-solutions/lc-solutions/3100-3199/3179-find-the-n-th-value-after-k-seconds.md b/dsa-solutions/lc-solutions/3100-3199/3179-find-the-n-th-value-after-k-seconds.md new file mode 100644 index 000000000..f17d27afc --- /dev/null +++ b/dsa-solutions/lc-solutions/3100-3199/3179-find-the-n-th-value-after-k-seconds.md @@ -0,0 +1,235 @@ +--- +id: find-the-n-th-value-after-k-seconds +title: Find the N-th Value After K Seconds +sidebar_label: 3179. Find the N-th Value After K Seconds +tags: +- Array +- Math +- Simulation +- Combinatorics +- Prefix Sum +description: "This is a solution to the Find the N-th Value After K Seconds problem on LeetCode." +--- + +## Problem Description +You are given two integers n and k. + +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. + +Return the value of a[n - 1] after k seconds. + +Since the answer may be very large, return it modulo 109 + 7. + +### Examples + +**Example 1:** + +``` +Input: n = 4, k = 5 + +Output: 56 + +Explanation: + +Second State After +0 [1,1,1,1] +1 [1,2,3,4] +2 [1,3,6,10] +3 [1,4,10,20] +4 [1,5,15,35] +5 [1,6,21,56] + + +``` + +**Example 2:** +``` +Input: n = 5, k = 3 + +Output: 35 + +Explanation: + +Second State After +0 [1,1,1,1,1] +1 [1,2,3,4,5] +2 [1,3,6,10,15] +3 [1,4,10,20,35] + +``` + +### Constraints + +- `1 <= n, k <= 1000` + +## Solution for Find the N-th Value After K Seconds Problem +### Approach +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. +### Steps +- Start with an array of size N where all elements are initialized to 1. This represents the array at time t=0. +- 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. +- After K seconds, return the value of the N-th element in the array. + + + + + #### Implementation + ```jsx live + function Solution(arr) { + function valueAfterKSeconds(n, k) { + let arr = new Array(n).fill(1); + const mod = 1000000007; + + for (let i = 1; i <= k; i++) { + let prefix = new Array(n).fill(0); + prefix[0] = arr[0] % mod; + for (let j = 1; j < n; j++) { + prefix[j] = (prefix[j - 1] + arr[j]) % mod; + } + arr = prefix; + } + return arr[n - 1]; + } + const input = 4; + const k = 5 + const output = valueAfterKSeconds(input , k) ; + return ( +
+

+ Input: + {JSON.stringify(input)} +

+

+ Output: {output.toString()} +

+
+ ); + } + ``` + + #### Complexity Analysis + + - Time Complexity: $ O(N*K) $ because of Nested Loops + - Space Complexity: $ O(N) $ because of prefix array + + ## Code in Different Languages + + + + ```javascript + function valueAfterKSeconds(n, k) { + let arr = new Array(n).fill(1); + const mod = 1000000007; + + for (let i = 1; i <= k; i++) { + let prefix = new Array(n).fill(0); + prefix[0] = arr[0] % mod; + for (let j = 1; j < n; j++) { + prefix[j] = (prefix[j - 1] + arr[j]) % mod; + } + arr = prefix; + } + return arr[n - 1]; + } + + ``` + + + + + ```typescript + class Solution { + valueAfterKSeconds(n: number, k: number): number { + let arr: number[] = new Array(n).fill(1); + const mod: number = 1000000007; + + for (let i = 1; i <= k; i++) { + let prefix: number[] = new Array(n).fill(0); + prefix[0] = arr[0] % mod; + for (let j = 1; j < n; j++) { + prefix[j] = (prefix[j - 1] + arr[j]) % mod; + } + arr = prefix; + } + return arr[n - 1]; + } +} + + ``` + + + + ```python + class Solution: + def value_after_k_seconds(self, n: int, k: int) -> int: + arr = [1] * n + mod = 10**9 + 7 + + for i in range(1, k + 1): + prefix = [0] * n + prefix[0] = arr[0] % mod + for j in range(1, n): + prefix[j] = (prefix[j - 1] + arr[j]) % mod + arr = prefix + + return arr[n - 1] + ``` + + + + ```java + import java.util.Arrays; + +public class Solution { + public int valueAfterKSeconds(int n, int k) { + int[] arr = new int[n]; + Arrays.fill(arr, 1); + int mod = 1000000007; + + for (int i = 1; i <= k; i++) { + int[] prefix = new int[n]; + prefix[0] = arr[0] % mod; + for (int j = 1; j < n; j++) { + prefix[j] = (prefix[j - 1] + arr[j]) % mod; + } + arr = prefix; + } + return arr[n - 1]; + } +} + + ``` + + + + + ```cpp + class Solution { +public: + int valueAfterKSeconds(int n, int k) { + vector arr(n, 1); + int mod = 1e9 + 7; + for (int i = 1; i <= k; i++) { + vector prefix(n, 0); + prefix[0] = arr[0]%mod; + for (int j = 1; j < n; j++) { + prefix[j] = (prefix[j - 1] + arr[j]) % mod; + } + arr = prefix; + } + return arr[n-1]; + } +}; + ``` + + + +
+
+ +## References + +- **LeetCode Problem**: [Find the N-th Value After K Seconds](https://leetcode.com/problems/find-the-n-th-value-after-k-seconds/) + +- **Solution Link**: [LeetCode Solution](https://leetcode.com/problems/find-the-n-th-value-after-k-seconds/solutions) +