diff --git a/Easy/119. Pascel's Triangle II/README.md b/Easy/119. Pascel's Triangle II/README.md deleted file mode 100644 index 2ef44b2..0000000 --- a/Easy/119. Pascel's Triangle II/README.md +++ /dev/null @@ -1,162 +0,0 @@ -# Pascel's Triangle II - -Given an integer ```rowIndex```, return the rowIndexth(0-indexed) row of the Pascal's triangle. - -In Pascal's triangle, each number is the sum of the two numbers directly above it as shown: - -![gif](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif) - -### Example 1: -``` -Input: rowIndex = 3 -Output: [1,3,3,1] -``` - -### Example 2: -``` -Input: rowIndex = 0 -Output: [1] -``` - -### Example 3: -``` -Input: rowIndex = 1 -Output: [1,1] -``` - -## Constrains: -`0 <= rowIndex <= 33` - -# Explaination: - -This solution here, is Linear in time. - -As we know we can get any element of Pascal's Triangle in O(N) time and constant space complexity.
-for first row first column we have 1C1
-for second row first column we have 2C1
-for second row second column we have 2C2
-..... and so on
-Therefore we can infer, for ith row and jth column we have the number iCj
-
-And calculating this is pretty easy just in N time (factorial basically).
- - -# Solution - -### C Solution: -```c -int* getRow(int r, int* rS){ - int*ans = calloc(r + 1, sizeof(int)); - long temp=1; - ans[0]=1; - for(int i=1,up=r;i<=r;i++,up--){ - temp=temp*up/i; - ans[i]=temp; - } - *rS = r+1; - return ans; -} -``` - -### C++ Solution: -```cpp -class Solution { -public: - vector getRow(int r) { - vectorv(r+1); - long temp=1; - v[0]=v[r]=1; - for(int i=1,up=r,down=1;i getRow(int r) { - List ans = new ArrayList<>(); - ans.add(1); - long temp = 1; - for(int i=1,up=r,down=1;i<=r;i++,up--, down++){ - temp=temp*up/down; - ans.add((int)temp); - } - return ans; - } -} -``` - -### Python Solution: -```py -class Solution(object): - def getRow(self, r): - ans = [1]*(r+1) - up = r - down = 1 - for i in range(1, r): - ans[i] = ans[i-1]*int(up/down) - up = up - 1 - down = down + 1 - return ans -``` - -### Ruby Solution: -```rb -def get_row(r) - return [1] if r==0 - ans = [1] - temp = 1 - for i in 1...r do - temp = temp * (r-i+1)/i - ans << temp - end - ans << 1 - return ans -end -``` - -### C# Solution: -```cs -public class Solution { - public IList GetRow(int r) { - var ans = new int[r+1]; - ans[0]=ans[r]=1; - long temp=1; - for(int i=1,up=r;i -#include - -int* getRow(int r, int* rS) { - int* ans = (int*)calloc(r + 1, sizeof(int)); - long temp = 1; - ans[0] = 1; - for (int i = 1, up = r; i <= r; i++, up--) { - temp = temp * up / i; - ans[i] = (int)temp; - } - *rS = r + 1; - return ans; -} - -int main() { - int rowNumber; scanf("%d",&rowNumber); // You can change this to the desired row number - int size; - int* result = getRow(rowNumber, &size); - - printf("["); - for (int i = 0; i < size; i++) { - printf("%d", result[i]); - if (i < size - 1) { - printf(", "); - } - } - printf("]\n"); - - // Don't forget to free the memory allocated for the result - free(result); - - return 0; -} diff --git a/Easy/119. Pascel's Triangle II/Solution.cpp b/Easy/119. Pascel's Triangle II/Solution.cpp deleted file mode 100644 index 70f7de1..0000000 --- a/Easy/119. Pascel's Triangle II/Solution.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -using namespace std; - -class Solution { -public: - vector getRow(int r) { - vectorv(r+1); - long temp=1; - v[0]=v[r]=1; - for(int i=1,up=r,down=1;i> r; - Solution obb; - vector vec = obb.getRow(r); - - cout << "["; - for(int i = 0; i < vec.size();i++) { - if(i == vec.size() - 1) { - cout << vec[i] << "]\n"; - } else { - cout << vec[i] << ","; - } - } - - return 0; -} diff --git a/Easy/119. Pascel's Triangle II/Solution.cs b/Easy/119. Pascel's Triangle II/Solution.cs deleted file mode 100644 index 6df521f..0000000 --- a/Easy/119. Pascel's Triangle II/Solution.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Collections.Generic; - -public class Solution { - public IList GetRow(int r) { - var ans = new int[r + 1]; - ans[0] = ans[r] = 1; - long temp = 1; - for (int i = 1, up = r; i < r; i++, up--) { - temp = temp * up / i; - ans[i] = (int)temp; - } - return ans; - } -} - -public class Program { - public static void Main() { - Solution solution = new Solution(); - int rowNumber = 5; // You can change this to the desired row number - IList result = solution.GetRow(rowNumber); - Console.WriteLine($"Row {rowNumber} of Pascal's Triangle: {string.Join(", ", result)}"); - } -} diff --git a/Easy/119. Pascel's Triangle II/Solution.java b/Easy/119. Pascel's Triangle II/Solution.java deleted file mode 100644 index dcea91d..0000000 --- a/Easy/119. Pascel's Triangle II/Solution.java +++ /dev/null @@ -1,33 +0,0 @@ -import java.io.BufferedReader; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.BufferedWriter; -import java.io.InputStreamReader; -import java.util.*; - -class Solution { - public List getRow(int r) { - List ans = new ArrayList(); - ans.add(1); - long temp = 1; - for(int i=1,up=r,down=1;i<=r;i++,up--, down++){ - temp=temp*up/down; - ans.add((int)temp); - } - return ans; - } - - public static void main(String args[])throws IOException { - BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); - BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); - - String str = input.readLine(); - int N = Integer.parseInt(str); - - Solution obb = new Solution(); - List li = obb.getRow(N); - - output.write(li+"\n"); - output.flush(); - } -} diff --git a/Easy/119. Pascel's Triangle II/Solution.js b/Easy/119. Pascel's Triangle II/Solution.js deleted file mode 100644 index ba14260..0000000 --- a/Easy/119. Pascel's Triangle II/Solution.js +++ /dev/null @@ -1,7 +0,0 @@ -var getRow = function(r) { - var ans = new Array(r+1) - ans[0]=ans[r]=1 - for(i=1,up=r;igetRow($rowNumber); - echo "[" . implode(', ', $result) . "]\n"; -} - -main(); // Call the main function to execute the code diff --git a/Easy/119. Pascel's Triangle II/Solution.py b/Easy/119. Pascel's Triangle II/Solution.py deleted file mode 100644 index 04bd6ff..0000000 --- a/Easy/119. Pascel's Triangle II/Solution.py +++ /dev/null @@ -1,15 +0,0 @@ -class Solution(object): - def getRow(self, r): - ans = [1]*(r+1) - up = r - down = 1 - for i in range(1, r): - ans[i] = ans[i-1]*int(up/down) - up = up - 1 - down = down + 1 - return ans - -r = int(input()) -object = Solution() -print(object.getRow(r)) - \ No newline at end of file diff --git a/Easy/119. Pascel's Triangle II/Solution.rb b/Easy/119. Pascel's Triangle II/Solution.rb deleted file mode 100644 index dd64510..0000000 --- a/Easy/119. Pascel's Triangle II/Solution.rb +++ /dev/null @@ -1,19 +0,0 @@ -def get_row(r) - return [1] if r == 0 - ans = [1] - temp = 1 - for i in 1...r do - temp = temp * (r - i + 1) / i - ans << temp - end - ans << 1 - return ans -end - -def main - row_number = 5 # You can change this to the desired row number - result = get_row(row_number) - puts "#{result}" -end - -main # Call the main function to execute the code diff --git a/Easy/119. Pascel's Triangle II/Solution.ts b/Easy/119. Pascel's Triangle II/Solution.ts deleted file mode 100644 index 8a349ac..0000000 --- a/Easy/119. Pascel's Triangle II/Solution.ts +++ /dev/null @@ -1,12 +0,0 @@ -function getRow(r: number): number[] { - var ans:number[] = [1] - var temp:number = 1 - for(var i:number=1;i<=r;i++){ - temp = temp*(r-i+1)/i - ans.push(temp) - } - return ans -}; - -let r = 5 -console.log(getRow(r)) \ No newline at end of file