当前位置:首页|资讯

GFG 175 Longest Increasing Path in Tree

作者:您是打尖儿还是住店呢发布时间:2024-10-08

You are given a tree of n nodes and n-1 undirected edges . You are also given an array values ,

where values[i] represent the value on i node . You have to  nd the length of the longest strictly

increasing path in the tree.(1-based indexing is used)


Example 1:


Input:

n = 3

edges = {{1,2}, {1,3}}

values = {4, 3, 5}

Output:

3

Explanation:


as we can see in the tree , the longest strictly increasing path is 2 -> 1 -> 3 as 

their values(3, 4, 5) are in strictly increasing order;

----

我直接dfs做,结果超时了,因为是对每个点都进行dfs,然后也没有保存结果,所以就超时了,参考别的ac的代码,就是新建一个数组,dp,保存每个点数据,然后取最大值即可。

python的话,直接加个cache就行。

学到了这个dp,以后得试试。



Copyright © 2024 aigcdaily.cn  北京智识时代科技有限公司  版权所有  京ICP备2023006237号-1