【发布时间】:2026-02-21 11:00:02
【问题描述】:
给定一个 n*n 阶矩阵。我需要找到最大排序子矩阵的长度(以increasing order 的row-wise 和column-wise 方式排序)。
我做了以下代码,由于我的逻辑中的一些错误,它没有给出正确的输出。
我的伪代码:
arr=[map(int,raw_input().split()) for j in range(n)] #given list
a=[[0 for j in range(n)]for i in range(n)] #empty list
a[0][0]=1
for i in range(n):
for j in range(1,n):
if i==0 and arr[i][j-1]<=arr[i][j]: #compare list element with the
right element
a[i][j-1]=a[i][j-1]+1
if i>0 and arr[i][j-1]>=arr[i-1][j-1]: #compare list element with the
top for i>0
a[i][j-1]=a[i-1][j-1]+1
if i>0 and arr[i][j-1]<=arr[i][j]: #compare the same element with the
right element for i>0
a[i][j-1]=a[i][j-1]+1
print maximum number present in a
给定数组:
2 5 3 8 3
1 4 6 8 4
3 6 7 9 5
1 3 6 4 2
2 6 4 3 1
expected output=8(最大排序子矩阵的长度)
1 4 6 8
3 6 7 9
是按行和按列排序的子矩阵
我的方法应该是什么?
【问题讨论】:
-
排序矩阵中是否计算了相同的元素?例如,以下矩阵的输出应该是什么 - [ [ 2 2 4], [2 2 5], [3 2 1] ] 在 2 或 4 之间?
-
输出将是 [[2 2 4],[ 2 2 5]] 和 length=6 因为 3 2 1 不是按行排序的。
标签: python c++ algorithm dynamic-programming