예제 #1
0
        public int Solution(int[][] grid, bool showDp = false)
        {
            if (grid == null)
            {
                return(0);
            }
            int len = grid.Length;

            int[][] dp = new int[len][];

            for (int i = 0; i < len; i++)
            {
                dp[i] = new int[len];
            }

            for (int i = len - 1; i >= 0; i--)
            {
                if (i + 1 == len)
                {
                    dp[len - 1][i] = grid[len - 1][i];
                }
                else
                {
                    dp[len - 1][i] = Math.Max(grid[len - 1][i], dp[len - 1][i + 1]);
                }
            }

            for (int i = len - 2; i >= 0; i--)
            {
                for (int j = 0; j < len; j++)
                {
                    dp[i][j] = Math.Max(grid[i][j], dp[i + 1][j]);
                }

                for (int j = 1; j < len; j++)
                {
                    if (j > 0 && dp[i][j] >= grid[i][j - 1] && dp[i][j] > dp[i][j - 1])
                    {
                        dp[i][j] = Math.Max(dp[i][j - 1], grid[i][j]);
                    }
                }

                for (int j = len - 2; j >= 0; j--)
                {
                    if (j + 1 < len && dp[i][j] >= grid[i][j + 1] && dp[i][j] > dp[i][j + 1])
                    {
                        dp[i][j] = Math.Max(dp[i][j + 1], grid[i][j]);
                    }
                }
            }

            if (showDp)
            {
                Console.WriteLine(ShowList.GetStr(dp));
            }

            return(dp[0][0]);
        }
예제 #2
0
        /**
         *
         * Runtime: 104 ms, faster than 100.00% of C# online submissions for Swim in Rising Water.
         * Memory Usage: 23.4 MB, less than 100.00% of C# online submissions for Swim in Rising Water.
         *
         * nice~~ 有所付出,总会有所收获 yeah~
         *
         */
        public int Solution2(int[][] grid, bool showTest = false)
        {
            if (grid == null)
            {
                return(0);
            }
            int len = grid.Length;

            int[][] dp = new int[len][];

            for (int i = 0; i < len; i++)
            {
                dp[i] = new int[len];
            }

            // 确定最后一行为基准
            // because : 最后一行的值是可以通过当前值和后一个路径值确定的 dp[i][j] = Math.Max(grid[i][j], dp[i][j+1]);
            for (int i = len - 1; i >= 0; i--)
            {
                if (i + 1 == len)
                {
                    dp[len - 1][i] = grid[len - 1][i];
                }
                else
                {
                    dp[len - 1][i] = Math.Max(grid[len - 1][i], dp[len - 1][i + 1]);
                }
            }

            // 从倒数第二行往上遍历
            for (int i = len - 2; i >= 0; i--)
            {
                // ps 此处之所以使用两个循环拆分是由于改变周边值时由于遍历顺序导致周边值尚未更新从而无法影响,从而导致影响不起作用...

                // 确定猜测值 : 当前值 or 下方路径值
                // ps 之所以为猜测值是由于路径还可以选择左右方
                for (int j = 0; j < len; j++)
                {
                    dp[i][j] = Math.Max(grid[i][j], dp[i + 1][j]);
                }

                // 从当前点出发,试图改变周边路径值
                for (int j = 0; j < len; j++)
                {
                    ChangeOther(i, j, grid, dp);
                }
            }

            if (showTest)
            {
                Console.WriteLine(ShowList.GetStr(grid));
                Console.WriteLine("------------------------ dp:");
                Console.WriteLine(ShowList.GetStr(dp));
            }

            return(dp[0][0]);
        }
예제 #3
0
        public int Try(int[][] grid)
        {
            int sum = 0;

            for (int i = 0; i < grid.Length; i++)
            {
                for (int j = 0; j < grid[i].Length; j++)
                {
                    grid[i][j] = Helper(grid, i, j);
                    sum       += grid[i][j];
                }
            }

            Console.WriteLine(ShowList.GetStr(grid));

            return(sum);
        }
예제 #4
0
        /**
         * Runtime: 88 ms, faster than 100.00% of C# online submissions for Max Increase to Keep City Skyline.
         * Memory Usage: 24.8 MB, less than 12.50% of C# online submissions for Max Increase to Keep City Skyline.
         */
        public int Solution(int[][] grid)
        {
            int len = grid.Length, colLen = grid[0].Length;

            int[] vertical = new int[colLen], across = new int[len];

            for (int i = 0; i < len; i++)
            {
                var max = 0;
                for (int j = 0; j < colLen; j++)
                {
                    max = Math.Max(max, grid[i][j]);
                }

                across[i] = max;
            }

            for (int i = 0; i < colLen; i++)
            {
                var max = 0;
                for (int j = 0; j < len; j++)
                {
                    max = Math.Max(max, grid[j][i]);
                }

                vertical[i] = max;
            }

            int sum = 0;

            for (int i = 0; i < len; i++)
            {
                for (int j = 0; j < colLen; j++)
                {
                    sum       -= grid[i][j];
                    grid[i][j] = Math.Min(vertical[j], across[i]);
                    sum       += grid[i][j];
                }
            }

            Console.WriteLine(ShowList.GetStr(grid));

            return(sum);
        }