ARTS 2019 03 17 (22)

68 篇文章 0 订阅
49 篇文章 0 订阅

ARTS
Algorithm:每周至少做一个leetcode的算法题;
Review:阅读并点评至少一篇英文技术文章;
Tip/Tech:学习至少一个技术技巧;
Share:分享一篇有观点和思考的技术文章;

Algorithm

120. 三角形最小路径和

https://leetcode-cn.com/problems/triangle/
这道题也是在动态回归当中非常经典的问题,属于必做的题之一的。
这题其实放在动态回归的一多题目中属于简单的题目:
(1)老套路,用一个二维数组来记录当前的行的最小值,然后最后一步进行检查二维数组的最后一个行的最小的元素
Talk is cheap, show the code:

public int minimumTotal(List<List<Integer>> triangle) {
        int size = triangle.size();
        int maxSize = triangle.get(size - 1).size();
        int[][] array = new int[triangle.size()][maxSize];
        for (int i = 0; i < size; ++i) {
            List<Integer> tempList = triangle.get(i);
            if (i == 0) {
                array[0][0] = tempList.get(0);
                continue;
            }
            for (int j = 0, tempLen = tempList.size(); j < tempLen; ++j) {
                if (j == 0) {
                    array[i][j] =  array[i - 1][j] + tempList.get(0);
                } else if (j == tempLen - 1) {
                    array[i][j] =  array[i - 1][j - 1] + tempList.get(j);
                } else {
                    array[i][j] = Math.min(array[i - 1][j] + tempList.get(j), array[i - 1][j - 1] + tempList.get(j));
                }
            }
        }
        int res = Integer.MAX_VALUE;
        for (int i = 0; i < maxSize; ++i) {
            if (res > array[size - 1][i]) {
                res = array[size - 1][i];
            }
        }
        return res;
    }

上面代码没啥错,就是太冗余了,我们来看一段比较好的代码的解法

 /**
     * 解法1 二维数组进行求解
     * @param triangle
     * @return
     */
    public int betterMinimumTotal(List<List<Integer>> triangle) {
        if (triangle == null || triangle.size() == 0){
            return 0;
        }
        // 加1可以不用初始化最后一层
        int[][] dp = new int[triangle.size()+1][triangle.size()+1];

        for (int i = triangle.size()-1; i>=0; i--){
            List<Integer> curTr = triangle.get(i);
            for(int j = 0 ; j< curTr.size(); j++){
                dp[i][j] = Math.min(dp[i+1][j], dp[i+1][j+1]) + curTr.get(j);
            }
        }
        return dp[0][0];
    }

这上面的也是用二维数组的空间大小来做的,但是却完成的非常好,代码不冗余,而且用了不一样的思路,从尾部到头部,要比一般的动态规划的代码写的好多了。
但是题目中有提到要用一维数组来进行解答会很加分的,所以还有一段一维数组的解法。

 /**
     * 解法2 一维数组进行求解
     * @param triangle
     * @return
     */
    public int better2MinimumTotal(List<List<Integer>> triangle) {
        if (triangle == null || triangle.size() == 0) {
            return 0;
        }
        // 只需要记录每一层的最小值即可
        int[] dp = new int[triangle.size()+1];
        for (int i = triangle.size() - 1; i >= 0; i--) {
            List<Integer> curTr = triangle.get(i);
            for (int j = 0; j < curTr.size(); j++) {
                //这里的dp[j] 使用的时候默认是上一层的,赋值之后变成当前层
                dp[j] = Math.min(dp[j],dp[j+1]) + curTr.get(j);
            }
        }
        return dp[0];
    }

这里的思路其实很巧妙的,如果我们选择从第一行开始遍历,那么记录每一行的最下值会变的没有意义,但是如果你选择从最后一行开始取值,那么就有意义了。这个你要自己手写一遍才能明白其中的奥妙,我原来选择用一维数组来进行解答就发发现了这样的问题,但是现在在看到这个代码就觉得很神奇了。从尾到头,然后每个区选择他的后一个数组相加取其最小值。思路很厉害。

Review

https://www.techiedelight.com/find-length-longest-path-matrix-consecutive-characters/
这题吧,超级复杂,我真的有点。。。没怎么弄懂,就是在一堆的数组里面寻找最大的路径的长度。
具体的代码长这样:

#include <iostream>
using namespace std;

// Size of given matrix is M x N
#define M 5
#define N 5

// Below arrays details all 8 possible movements
int row[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int col[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

// check whether cell (x, y) is valid or not
bool isValid(int x, int y)
{
	return (x >= 0 && x < M && y >= 0 && y < N);
}

// Find length of longest path in the matrix mat[][] with consecutive characters
// The path should continue from the previous character
// Here (i, j) denotes the coordinates of the current cell
int findMaxLength(char mat[][N], int x, int y, char previous)
{
	// base case: return length 0 if current cell (x, y) is invalid or
	// current character is not consecutive to the previous character
	if (!isValid(x, y) || previous + 1 != mat[x][y])
		return 0;

	// stores the length of longest path
	int max_length = 0;

	// recurse for all 8 adjacent cells from current cell
	for (int k = 0; k < 8; k++)
	{
		// visit position (x + row[k], y + col[k]) and find maximum length from that path
		int len = findMaxLength(mat, x + row[k], y + col[k], mat[x][y]);

		// update the length of longest path if required
		max_length = max(max_length, 1 + len);
	}

	return max_length;
}

// Find length of longest path in the matrix with consecutive characters
int findMaxLength(char mat[][N], char ch)
{
	// stores the length of longest path
	int max_length = 0;

	// traverse the matrix
	for (int x = 0; x < M; x++)
	{
		for (int y = 0; y < N; y++)
		{
			// start from the current cell if its value matches with the given character
			if (mat[x][y] == ch)
			{
				// recurse for all 8 adjacent cells from current cell
				for (int k = 0; k < 8; k++)
				{
					// visit position (x + row[k], y + col[k]) and find maximum length from that path
					int len = findMaxLength(mat, x + row[k], y + col[k], ch);

					// update the length of longest path if required
					max_length = max(max_length, 1 + len);
				}
			}
		}
	}

	return max_length;
}

int main()
{
	// input matrix
	char mat[][N] =
	{
		{ 'D', 'E', 'H', 'X', 'B' },
		{ 'A', 'O', 'G', 'P', 'E' },
		{ 'D', 'D', 'C', 'F', 'D' },
		{ 'E', 'B', 'E', 'A', 'S' },
		{ 'C', 'D', 'Y', 'E', 'N' }
	};

	// starting character
	char ch = 'C';

	cout << "The length of longest path with consecutive characters "
		 << "starting from character " << ch << " is "
		 << findMaxLength(mat, ch) << endl;

	return 0;
}

这种题目一般没有动手写一遍的话,一般很难弄明白

Tip/Tech

双端队列。

Share

https://www.project-syndicate.org/commentary/global-economic-prospects-bleak-in-2019-by-kaushik-basu-2019-01

The Sorry State of the World Economy(糟糕的世界经济情况)

这篇文章中所描写的世界的经济情况很不好,各国的都不好,中国经过了长达20年左右的发展,也进入了一种高收入但是低增长的情况,印度的的经济虽然发展不错,但是就业的机会却没有增加多少,印尼的政府问题导致了他们不能像咱们一样把制造业发展的得心应手。
其实世界的经济想要快速发展其实现在看起来很难了,一旦人工智能发展起来,那么制造业也不会那么容易的发展,一切都将进入一种不可知的情况当中。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值