Arranging the number 1 in a 2d matrix

后端 未结 6 1703
没有蜡笔的小新
没有蜡笔的小新 2020-12-29 17:11

Given the number of rows and columns of a 2d matrix

Initially all elements of matrix are 0

Given the number of 1\'s that should be present in each row

6条回答
  •  天涯浪人
    2020-12-29 17:47

    Inspiring from the solution given by RobertBaron I have tried to build a new algorithm.

    rows = [int(x)for x in input().split()]
    cols = [int (ss) for ss in input().split()]
    rows.sort()
    cols.sort(reverse=True)
    for i in range(len(rows)):
        for j in range(len(cols)):
            if(rows[i]!= 0 and cols[j]!=0):
                rows[i] = rows[i] - 1;
                cols[j]  =cols[j]-1;
    print("rows: ",rows)
    print("cols: ",cols)
    #if there is any non zero value, print NO else print yes
    flag = True
    for i in range(len(rows)):
        if(rows[i]!=0):
            flag = False
            break
    
    for j in range(len(cols)):
        if(cols[j]!=0):
            flag = False
    
    if(flag):
        print("YES")
    else:
        print("NO")
    

    here, i have sorted the rows in ascending order and cols in descending order. later decrementing particular row and column if 1 need to be placed! it is working for all the test cases posted here! rest GOD knows

提交回复
热议问题