We can do the follow up using only 1 array by updating the array backward row=[1] for i in range(rowIndex): row.append(1) for j in range(i,0,-1): row[j]+=row[j-1] return row
This solution works, but it doesn't cover the mathematical meaning of Pascal's triangle and therefore can be simplified into a time and space O(rowIndex) function with a calculation of binomial coefficients.
@@pbenikovszky1 there is a easier way to calculate binomial coefficients without calculating factorials. Here is js version: function nCr(n, r) { if(r === 0 || n === r) return 1; if(r > n/2) r = n-r; let res = 1; for(let i = 1; i
My approach to this problem was to try to figure out a function such that: f(n, i) = ans[i] ; Meaning there would be a function which when given the rowIndex would be able to calculate the value of i th element in the row. Does a function like this exist? Is it even possible? This to me would seem much more efficient. Here are some examples of this functions: f(5,5) = 1 f(4,2) = 6 f(5,2) = 10 f(5,3) = 10 f(n, 0) = 1 f(n, n) = 1 .... etc. Update: This is the binomial coefficient function
Easiest to come up with class Solution: def getRow(self, rowIndex: int) -> List[int]: res = [[1]] for i in range(rowIndex): tmp = [0] + res[-1] + [0] row = [] for j in range(len(res[-1])+1): row.append(tmp[j]+tmp[j+1]) res.append(row) return res[rowIndex]
for math nerds: ---------------------------------------------------------------- TC: O(rowIndex) SC: O(rowIndex) ---------------------------------------------------------------- var getRow = function(rowIndex) { if(rowIndex === 0) return [1]; const row = new Array(rowIndex+1); for(let i = 0; i n/2) r = n-r; let res = 1; for(let i = 1; i
We can do the follow up using only 1 array by updating the array backward
row=[1]
for i in range(rowIndex):
row.append(1)
for j in range(i,0,-1):
row[j]+=row[j-1]
return row
fkin genious bro
A Q&A video and two problems .. you are on a roll. Thanks for making these videos !
This solution works, but it doesn't cover the mathematical meaning of Pascal's triangle and therefore can be simplified into a time and space O(rowIndex) function with a calculation of binomial coefficients.
sure, and the cpu will easily handle rowindex 1000 :D
@@pbenikovszky1 there is a easier way to calculate binomial coefficients without calculating factorials. Here is js version:
function nCr(n, r) {
if(r === 0 || n === r) return 1;
if(r > n/2) r = n-r;
let res = 1;
for(let i = 1; i
Just did this few hours back 😃
My approach to this problem was to try to figure out a function such that:
f(n, i) = ans[i] ;
Meaning there would be a function which when given the rowIndex would be able to calculate the value of i th element in the row. Does a function like this exist? Is it even possible? This to me would seem much more efficient.
Here are some examples of this functions:
f(5,5) = 1
f(4,2) = 6
f(5,2) = 10
f(5,3) = 10
f(n, 0) = 1
f(n, n) = 1 .... etc.
Update: This is the binomial coefficient function
C++
class Solution {
public:
vector getRow(int rowIndex) {
vectorprev(rowIndex+1);
for(int r=0;r
Awesome solution
Discovered this channel recently and am learning a lot! Thank you! Beginner question: why do you put your sol in a class?
'cause leetcode is passing all the inputs to this class in the background. Though some sites just use a function.
hi @NeetCodeIO code isn't working for current format of the question Leetcode 119. Code returns [1] always. Can you please check? Thank you
my friend you have to figure it out on your own sometimes
Easiest to come up with
class Solution:
def getRow(self, rowIndex: int) -> List[int]:
res = [[1]]
for i in range(rowIndex):
tmp = [0] + res[-1] + [0]
row = []
for j in range(len(res[-1])+1):
row.append(tmp[j]+tmp[j+1])
res.append(row)
return res[rowIndex]
Please Bro
Make video for
2905. Find Indices With Index and Value Difference II
There’s a good quality one here: th-cam.com/video/IL1PB8jnfhE/w-d-xo.htmlfeature=shared (found this from the top rated post under the solutions tab)
for math nerds:
----------------------------------------------------------------
TC: O(rowIndex)
SC: O(rowIndex)
----------------------------------------------------------------
var getRow = function(rowIndex) {
if(rowIndex === 0) return [1];
const row = new Array(rowIndex+1);
for(let i = 0; i n/2) r = n-r;
let res = 1;
for(let i = 1; i