From 000305a7d3008b0d293f29653679214f6ddfd9f3 Mon Sep 17 00:00:00 2001 From: WHAHA-HA Date: Thu, 10 Sep 2020 08:51:48 +0530 Subject: [PATCH] Added solution 295 --- Problems/295.txt | 14 +++++++++++++ Solutions/295.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 Problems/295.txt create mode 100644 Solutions/295.py diff --git a/Problems/295.txt b/Problems/295.txt new file mode 100644 index 0000000..79b08dd --- /dev/null +++ b/Problems/295.txt @@ -0,0 +1,14 @@ +PROBLEM 295: + +Pascal's triangle is a triangular array of integers constructed with the following formula: + +The first row consists of the number 1. For each subsequent row, each element is the sum of the numbers directly above it, on either side. For example, here are the first few rows: + + 1 + 1 1 + 1 2 1 + 1 3 3 1 +1 4 6 4 1 +Given an input k, return the kth row of Pascal's triangle. + +Bonus: Can you do this using only O(k) space? \ No newline at end of file diff --git a/Solutions/295.py b/Solutions/295.py new file mode 100644 index 0000000..5e25b01 --- /dev/null +++ b/Solutions/295.py @@ -0,0 +1,51 @@ +""" +Problem: + +Pascal's triangle is a triangular array of integers constructed with the following +formula: + +The first row consists of the number 1. For each subsequent row, each element is the +sum of the numbers directly above it, on either side. For example, here are the first +few rows: + + 1 + 1 1 + 1 2 1 + 1 3 3 1 +1 4 6 4 1 +Given an input k, return the kth row of Pascal's triangle. + +Bonus: Can you do this using only O(k) space? +""" + +from typing import List + + +def get_pascal(k: int) -> List[int]: + row = [1 for _ in range(k)] + curr = 1 + for _ in range(k): + # generating the value for each level + last = 0 + for i in range(curr - 1): + last, temp = row[i], last + row[i] += temp + curr += 1 + return row + + +if __name__ == "__main__": + print(get_pascal(1)) + print(get_pascal(2)) + print(get_pascal(3)) + print(get_pascal(4)) + print(get_pascal(5)) + print(get_pascal(6)) + + +""" +SPECS: + +TIME COMPLEXITY: O(k ^ 2) +SPACE COMPLEXITY: O(k) +"""