最大增幅以保持城市天际线

huangapple go评论79阅读模式
英文:

Max increase to keep city skyline

问题

我正在尝试解决 "Max Increase to Keep City Skyline" LeetCode 问题。

在本地测试算法时,我成功地找到了“天际线”对于“水平”视图的正确值(“水平” = 通过查看矩阵的列来获得的视图)。

然而,对于“垂直”视图(“垂直” = 通过查看矩阵的行来获得的视图),在 verticalView 数组的最后两个值不正确,我不明白为什么。

我添加了 println 来显示变量的值。

我不明白为什么当 verticalView[2] = 8(这发生在 i = 0, j = 2 时),i = 2j = 2verticalView[2] 已经变成了 0

import java.util.Arrays;

class Solution {
    public static int maxIncreaseKeepingSkyline(int[][] grid) {
        int[] verticalView = new int[grid.length];
        int[] horizontalView =  new int[grid[0].length];

        for (int i = 0; i < grid.length; i++) {
            verticalView[i] = 0;
            horizontalView[i] = 0;
            for (int j = 0; j < grid[0].length; j++) {
                boolean b = grid[i][j] > verticalView[j];
                System.out.println("verticalView[" + j + "] = " + verticalView[j]);

                if (grid[i][j] > verticalView[j]) verticalView[j] = grid[i][j];
                if (grid[i][j] > horizontalView[i]) horizontalView[i] = grid[i][j];

                System.out.println("grid[" + i + "][" + j + "] > verticalView[" + j + "] = " + b);
                System.out.println("grid[" + i + "][" + j + "] = " + grid[i][j]);
                System.out.println("verticalView[" + j + "] = " + verticalView[j]);
                System.out.println();
            }
        }
        System.out.println("Vertical view: " + Arrays.toString(verticalView));
        System.out.println("Horizontal view: " + Arrays.toString(horizontalView));

//        int[] verticalView = {9,4,8,7};
//        int[] horizontalView = {8,7,9,3};
        int sum = 0;

        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                while (grid[i][j] < verticalView[j] & grid[i][j] < horizontalView[i]) {
                    grid[i][j]++;
                    sum++;
                }
            }
        }

//        for (int i = 0; i < grid.length; i++) {
//            for (int j = 0; j < grid[0].length; j++) {
//                System.out.print(grid[i][j] + " ");
//            }
//            System.out.println();
//        }
        return sum;
    }

    public static void main(String[] args) {

        int[][] n = {{3, 0, 8, 4}, {2, 4, 5, 7}, {9, 2, 6, 3},{0, 3, 1, 0}};

        System.out.println("Solution: " + maxIncreaseKeepingSkyline(n));
    }
}
英文:

I'm trying to solve "Max Increase to Keep City Skyline" LeetCode problem.

Testing the algorithm locally, I manage to find the "skyline" for the "horizontal" view correctly ("horizontal" = the view you'd see by looking at the matrix's columns).

Though, for the "vertical" view ("vertical" = the view you'd see by looking at the matrix's rows), my last 2 values in the verticalView array aren't the correct ones, and I don't understand why.

I added the println to display the variable's values.

I don't understand why when verticalView[2] = 8 (this happens when i = 0, j = 2), i = 2 and j = 2, verticalView[2] has become 0.

import java.util.Arrays;

class Solution {
    public static int maxIncreaseKeepingSkyline(int[][] grid) {
        int[] verticalView = new int[grid.length];
        int[] horizontalView =  new int[grid[0].length];

        for (int i = 0; i &lt; grid.length; i++) {
            verticalView[i] = 0;
            horizontalView[i] = 0;
            for (int j = 0; j &lt; grid[0].length; j++) {
                boolean b = grid[i][j] &gt; verticalView[j];
                System.out.println(&quot;verticalView[&quot; + j + &quot;] = &quot; + verticalView[j]);

                if (grid[i][j] &gt; verticalView[j] /* b */) verticalView[j] = grid[i][j];
                if (grid[i][j] &gt; horizontalView[i]) horizontalView[i] = grid[i][j];

                System.out.println(&quot;grid[&quot; + i + &quot;][&quot; + j + &quot;] &gt; verticalView[&quot; + j + &quot;] = &quot; + b);
                System.out.println(&quot;grid[&quot; + i + &quot;][&quot; + j + &quot;] = &quot; + grid[i][j]);
                System.out.println(&quot;verticalView[&quot; + j + &quot;] = &quot; + verticalView[j]);
                System.out.println();
            }
        }
        System.out.println(&quot;Vertical view: &quot; + Arrays.toString(verticalView));
        System.out.println(&quot;Horizontal view: &quot; + Arrays.toString(horizontalView));

//        int[] verticalView = {9,4,8,7};
//        int[] horizontalView = {8,7,9,3};
        int sum = 0;

        for (int i = 0; i &lt; grid.length; i++) {
            for (int j = 0; j &lt; grid[0].length; j++) {
                while (grid[i][j] &lt; verticalView[j] &amp; grid[i][j] &lt; horizontalView[i]) {
                    grid[i][j]++;
                    sum++;
                }
            }
        }

//        for (int i = 0; i &lt; grid.length; i++) {
//            for (int j = 0; j &lt; grid[0].length; j++) {
//                System.out.print(grid[i][j] + &quot; &quot;);
//            }
//            System.out.println();
//        }
        return sum;
    }

    public static void main(String[] args) {

        int[][] n = {{3, 0, 8, 4}, {2, 4, 5, 7}, {9, 2, 6, 3},{0, 3, 1, 0}};

        System.out.println(&quot;Solution: &quot; + maxIncreaseKeepingSkyline(n));
    }
}

答案1

得分: 2

以下是翻译好的代码部分:

代码中的错误在于

    verticalView[i] = 0;
    horizontalView[i] = 0;

在Java中数组的默认值是零

即使值被更新为非零的值你也将它们更改为零

问题的简化解决方案

```java
import java.util.Arrays;

class Solution {
    public static int maxIncreaseKeepingSkyline(int[][] grid) {
        int[] verticalView = new int[grid.length];
        int[] horizontalView =  new int[grid[0].length];
        
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] > verticalView[j]) verticalView[j] = grid[i][j];
                if (grid[i][j] > horizontalView[i]) horizontalView[i] = grid[i][j];
            }
            
        }
        
        int sum = 0;

        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                while (grid[i][j] < verticalView[j] & grid[i][j] < horizontalView[i]) {
                    grid[i][j]++;
                    sum++;
                }
            }
        }

        return sum;
    }

    public static void main(String[] args) {

        int[][] n = {{3, 0, 8, 4}, {2, 4, 5, 7}, {9, 2, 6, 3},{0, 3, 1, 0}};

        System.out.println("Solution: " + maxIncreaseKeepingSkyline(n));
    }
}
英文:

The mistake in your code is in:

verticalView[i] = 0;
horizontalView[i] = 0;

The default values of the array are zero in java.

You are changing values to zero even if they are updated to something other than zero.

Simplified Solution to the Problem:

import java.util.Arrays;
class Solution {
public static int maxIncreaseKeepingSkyline(int[][] grid) {
int[] verticalView = new int[grid.length];
int[] horizontalView =  new int[grid[0].length];
for (int i = 0; i &lt; grid.length; i++) {
for (int j = 0; j &lt; grid[0].length; j++) {
if (grid[i][j] &gt; verticalView[j]) verticalView[j] = grid[i][j];
if (grid[i][j] &gt; horizontalView[i]) horizontalView[i] = grid[i][j];
}
}
int sum = 0;
for (int i = 0; i &lt; grid.length; i++) {
for (int j = 0; j &lt; grid[0].length; j++) {
while (grid[i][j] &lt; verticalView[j] &amp; grid[i][j] &lt; horizontalView[i]) {
grid[i][j]++;
sum++;
}
}
}
return sum;
}
public static void main(String[] args) {
int[][] n = {{3, 0, 8, 4}, {2, 4, 5, 7}, {9, 2, 6, 3},{0, 3, 1, 0}};
System.out.println(&quot;Solution: &quot; + maxIncreaseKeepingSkyline(n));
}
}

答案2

得分: 1

看起来不错!

我们还可以使用 Math.max() 来简化一下,虽然不是什么大问题:

public class Solution {
    public static final int maxIncreaseKeepingSkyline(
        final int[][] grid
    ) {
        int[] rows = new int[grid.length];
        int[] cols = new int[grid.length];

        for (int row = 0; row < grid.length; row++) {
            for (int col = 0; col < grid.length; col++) {
                rows[row] = Math.max(rows[row], grid[row][col]);
                cols[col] = Math.max(cols[col], grid[row][col]);
            }
        }

        int maxIncrease = 0;

        for (int row = 0; row < grid.length; row++) {
            for (int col = 0; col < grid.length; col++) {
                maxIncrease += Math.min(rows[row], cols[col]) - grid[row][col];
            }
        }

        return maxIncrease;
    }
}
英文:

Looks good!

We can also use Math.max() for just simplifying it a bit, nothing major though:

public class Solution {
public static final int maxIncreaseKeepingSkyline(
final int[][] grid
) {
int[] rows = new int[grid.length];
int[] cols = new int[grid.length];
for (int row = 0; row &lt; grid.length; row++) {
for (int col = 0; col &lt; grid.length; col++) {
rows[row] = Math.max(rows[row], grid[row][col]);
cols[col] = Math.max(cols[col], grid[row][col]);
}
}
int maxIncrease = 0;
for (int row = 0; row &lt; grid.length; row++) {
for (int col = 0; col &lt; grid.length; col++) {
maxIncrease += Math.min(rows[row], cols[col]) - grid[row][col];
}
}
return maxIncrease;
}
}

huangapple
  • 本文由 发表于 2020年10月19日 16:36:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64423831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定