Pet*_*ter 5 algorithm matrix mathematical-optimization data-structures
Given an N x M matrix having only positive integer values, we have to nullify the matrix
i.e make all entries 0.
We are given two operations
1) multiply each element of any one column at a time by 2.
2) Subtract 1 from all elements of any one row at a time
Find the minimum number of operations required to nullify the matrix.
Run Code Online (Sandbox Code Playgroud)
我想做一些与LCM有关的事情,但无法达成解决方案
我们首先求解 1 行,然后将其扩展到所有行。让我们随机举一个例子:
6 11 5 13
Run Code Online (Sandbox Code Playgroud)
目标是使所有元素都为 1。首先,我们将 5(最小元素)设为 1。为此,我们需要减去 4(即减去 1 四次)。结果数组是:
2 7 1 9
Run Code Online (Sandbox Code Playgroud)
现在我们将 1 乘以 2,并将所有行元素减去 1:
1 6 1 8
Run Code Online (Sandbox Code Playgroud)
接下来,我们将 2 个 1 乘以 2,并将所有行元素减去 1:
1 5 1 7
Run Code Online (Sandbox Code Playgroud)
继续以这种方式,我们得到1 1 1 1。现在我们减去 1 得到0 0 0 0。
接下来,我们进入其他行并执行与上面相同的操作。我们上面取消的行全部为零,因此在操作其他行时乘以 2 不会更改已经取消的行。
找到最小操作数的问题还取决于我们选择的行顺序。我认为首先选择最大值为最小值的行(在其他行中)。我需要验证这一点。
| 归档时间: |
|
| 查看次数: |
568 次 |
| 最近记录: |