Daily Leetcode Challenge | Day 29 | Maximum Number of Moves in a Grid

Поділитися
Вставка
  • Опубліковано 14 лис 2024

КОМЕНТАРІ • 3

  • @darshankumar5546
    @darshankumar5546  17 днів тому

    # Approach 2- OPTIMIZED
    class Solution:
    def maxMoves(self, grid: List[List[int]]) -> int:
    col=len(grid[0])
    row=len(grid)
    dp=[0 for j in range(row)]
    #print(dp)
    columnn=col-2
    ans=0
    while(columnn>-1):
    dp1=[0 for j in range(row)]
    for r in range(row):
    i,j=r,columnn
    maxx=0
    x,y=i-1,j+1
    if((x=0) and (y=0) and grid[x][y]>grid[i][j]):
    maxx=max(maxx,dp[x]+1)
    x,y=i,j+1
    if((x=0) and (y=0) and grid[x][y]>grid[i][j]):
    maxx=max(maxx,dp[x]+1)
    x,y=i+1,j+1
    if((x=0) and (y=0) and grid[x][y]>grid[i][j]):
    maxx=max(maxx,dp[x]+1)
    if(maxx>0):
    dp1[i]=maxx
    if(j==0):
    ans=max(ans,dp1[i])
    columnn-=1
    dp=dp1

    #print(dp)
    return ans

  • @darshankumar5546
    @darshankumar5546  17 днів тому

    # Approach 1
    class Solution:
    def maxMoves(self, grid: List[List[int]]) -> int:
    col=len(grid[0])
    row=len(grid)
    dp=[[0 for i in range(col)] for j in range(row)]
    #print(dp)
    columnn=col-2
    ans=0
    while(columnn>-1):
    for r in range(row):
    i,j=r,columnn
    maxx=0
    x,y=i-1,j+1
    if((x=0) and (y=0) and grid[x][y]>grid[i][j]):
    maxx=max(maxx,dp[x][y]+1)
    x,y=i,j+1
    if((x=0) and (y=0) and grid[x][y]>grid[i][j]):
    maxx=max(maxx,dp[x][y]+1)
    x,y=i+1,j+1
    if((x=0) and (y=0) and grid[x][y]>grid[i][j]):
    maxx=max(maxx,dp[x][y]+1)
    if(maxx>0):
    dp[i][j]=maxx
    if(j==0):
    ans=max(ans,dp[i][j])

    columnn-=1

    #print(dp)
    return ans