题目地址:

https://leetcode.com/problems/longest-path-with-different-adjacent-characters/

给定一棵 n n n个节点的有根树,每个节点的编号 0 ∼ n − 1 0\sim n-1 0∼n−1,并且每个节点 i i i对应一个字符 s [ i ] s[i] s[i]。问树中最长的相邻节点字符不同的路径长度。

枚举路径最高节点,递归求所有当前节点儿子出发向下的最长路径长度,求出不同于当前节点字符的儿子的最长路径前两名,那么也就求出了最高节点为当前节点的最长路径长度。枚举完所有最高点之后即得答案。代码如下:

import java.util.ArrayList;
import java.util.List;public class Solution {int res;public int longestPath(int[] parent, String s) {int n = parent.length;List<Integer>[] g = new ArrayList[n];for (int i = 0; i < n; i++) {g[i] = new ArrayList<>();}for (int i = 1; i < n; i++) {g[parent[i]].add(i);}dfs(0, g, s);return res;}int dfs(int u, List<Integer>[] g, String s) {if (g[u].isEmpty()) {res = Math.max(res, 1);return 1;}int max1 = 0, max2 = 0;for (int ne : g[u]) {int len = dfs(ne, g, s);if (s.charAt(ne) != s.charAt(u)) {if (len > max1) {max2 = max1;max1 = len;} else if (len > max2) {max2 = len;}}}res = Math.max(res, max1 + 1 + max2);return 1 + max1;}
}

时空复杂度 O ( n ) O(n) O(n)。

【Leetcode】2246. Longest Path With Different Adjacent Characters相关推荐

  1. python棋盘最短路径_【leetcode】64. Minimum Path Sum 棋盘最短路径

    1. 题目 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right ...

  2. 【LeetCode】3. Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  3. 【LeetCode】15.Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 从字符数组的第一项往后挨个 ...

  4. 【Leetcode】1062. Longest Repeating Substring

    题目地址: https://leetcode.com/problems/longest-repeating-substring/ 给定一个长nnn字符串sss,求其最长重复子串的长度. 思路是二分 + ...

  5. 【leetcode】521. Longest Uncommon Subsequence I

    题目如下: 解题思路:本题有点意思.首先如果输入的两个字符串都为空,那么结果是-1:如果两个字符串长度不一样,那么结果是较长的字符串的长度,因为较长的字符串肯定是自身的子序列,但一定不是较短的字符串的 ...

  6. 【LeetCode】1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA)每日一题

    [LeetCode]1631. Path With Minimum Effort 最小体力消耗路径(Medium)(JAVA) 题目描述: You are a hiker preparing for ...

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  8. 【LeetCode】【HOT】39. 组合总和(回溯)

    [LeetCode][HOT]39. 组合总和 文章目录 [LeetCode][HOT]39. 组合总和 package hot;import java.util.ArrayList; import ...

  9. 【LeetCode】剑指 Offer 34. 二叉树中和为某一值的路径

    [LeetCode]剑指 Offer 34. 二叉树中和为某一值的路径 文章目录 [LeetCode]剑指 Offer 34. 二叉树中和为某一值的路径 package offer;import ja ...

最新文章

  1. SyntaxError: Missing parentheses in call to ‘print‘. Did you mean print(i, colour[i])?
  2. 数组的有关属性和方法
  3. js折线图设置y轴刻度_手绘风格的 JS 图表库:Chart.xkcd
  4. 企业邮箱自建战略优势大盘点
  5. 2013年35真棒恭贺新禧 - 壁纸
  6. linux内核能否扩展,Linux内核用到的GCC扩展
  7. 宝典9.2——海量数据的基本处理方法
  8. 媒体查询使用方法@media
  9. mysql+自动还原备份_Mysql 自动备份与恢复
  10. android object数组赋值_Java对象数组定义与用法详解
  11. oc51--循环retain
  12. 贪婪算法在求解最小生成树中的应用(JAVA)--Kruskal算法
  13. Windows下Git的下载与安装
  14. 程序员跳槽的7个常见理由,最后一个有人能看懂吗!
  15. Android 显示原理简介
  16. xxl_job springboot改造
  17. 评分卡模型开发(七)--基于逻辑回归的标准评分卡实现
  18. mac下cordova的ios-deploy安装问题
  19. php如何发送短信,PHP发送短信功能
  20. #(二)、古人怎么解决波的表达

热门文章

  1. Torvalds:SteamOS将“真正帮助”Linux桌面系统实现逆袭
  2. PyTorch 深度学习实践 第3讲
  3. 神经网络模型参数辨识,神经网络模型应用实例
  4. 论文学习——一种基于关键点的SAX改进算法
  5. css中hover属性的使用技巧
  6. [浪子学编程][读书笔记]-道法自然之用例分析
  7. linux畸形文件夹,Linux Kernel畸形ULE报文处理远程拒绝服务漏洞
  8. 网络服务与安全之openSSL制作CA证书
  9. Python爬虫爬取猫眼电影风语咒影评评论信息
  10. h5移动端,类似qq空间、朋友圈不规则图片显示适配功能