[leetcode]364. Nested List Weight Sum II嵌套列表加权和II
Given a nested list of integers, return the sum of all integers in the list weighted by their depth.
Each element is either an integer, or a list -- whose elements may also be integers or other lists.
Different from the [leetcode]339. Nested List Weight Sum嵌套列表加权和 where weight is increasing from root to leaf, now the weight is defined from bottom up. i.e., the leaf level integers have weight 1, and the root level integers have the largest weight.
Example 1:
Input: [[1,1],2,[1,1]]
Output: 8
Explanation: Four 1's at depth 1, one 2 at depth 2.
Example 2:
Input: [1,[4,[6]]]
Output: 17
Explanation: One 1 at depth 3, one 4 at depth 2, and one 6 at depth 1; 1*3 + 4*2 + 6*1 = 17.
思路
跑的最快的方法是 DFS
1. We can observe that
1x + 2y + 3z = (x+y+z) * (3+1) - (3x+2y+1z)
^ levelSum ^maxDepth ^ Nested List Weight Sum I problem
2. Use DFS recursion, converting this problem to Nested List Weight Sum I, updating levelSum and maxPath at the same time when using DFS
代码
class Solution {
int levelSum = 0;
int maxDepth = 1;
public int depthSumInverse(List<NestedInteger> nestedList) {
int depthSum = dfs(nestedList, 1);
return levelSum * (maxDepth + 1) - depthSum;
}
private int dfs(List<NestedInteger> nestedList, int depth) {
int sum = 0;
for (NestedInteger n : nestedList) {
if (n.isInteger()) {
// same as Nested List Weight Sum I
sum += n.getInteger() * depth;
// at the same time, use DFS to update levelSum and maxDepth
maxDepth = Math.max(depth, maxDepth);
levelSum += n.getInteger();
} else {
// same as Nested List Weight Sum I
sum += dfs(n.getList(), depth + 1);
}
}
return sum;
}
}
思路
最容易想到的方法 DFS
1. use helper function to get maxDepth
2. same as Nested List Weight Sum I, use dfs function to get result. Only concerning that do substraction instead of addition when entering next new level
class Solution {
public int depthSumInverse(List<NestedInteger> nestedList) {
// corner case
if(nestedList == null || nestedList.size() == 0) return 0;
int depth = helper(nestedList);
int sum = dfs(nestedList, depth);
return sum;
}
// helper recursion function to get the maxDepth
public int helper(List<NestedInteger> nestedList) {
int depth = 0;
for (NestedInteger n : nestedList) {
if(n.isInteger()) {
depth = Math.max(depth, 1);
}
else {
depth = Math.max(depth, helper(n.getList()) + 1);
}
}
return depth;
}
// same as Nested List Weight Sum I
public int dfs(List<NestedInteger> nestedList, int depth) {
int result = 0;
for (NestedInteger n : nestedList) {
if (n.isInteger()) {
result += n.getInteger() * depth;
} else {
result += dfs(n.getList(), depth - 1);
}
}
return result;
}
}
思路
BFS(level order traversal)
if we want to get 3x + 2y + 1z, we can use preSum tech like that
levelSum x
preSum x
result x
=======================
levelSum x y
preSum x x+y
result x x + x + y
=======================
levelSum x y z
preSum x x+y x+y+z
result x x + x + y x + x + y + x + y + z
代码
class Solution {
public int depthSumInverse(List<NestedInteger> nestedList) {
// corner case
if(nestedList == null || nestedList.size() == 0) return 0;
// initialize
int preSum = 0;
int result = 0;
// put each item of list into the queue
Queue<NestedInteger> queue = new LinkedList<>(nestedList);
while(!queue.isEmpty()){
//depends on different depth, queue size is changeable
int size = queue.size();
int levelSum = 0;
for(int i = 0; i < size; i++){
NestedInteger n = queue.poll();
if(n.isInteger()){
levelSum += n.getInteger();
}
else{
// depends on different depth, queue size is changeable
queue.addAll(n.getList());
}
}
preSum += levelSum;
result += preSum;
}
return result;
}
}
[leetcode]364. Nested List Weight Sum II嵌套列表加权和II的更多相关文章
- LeetCode 339. Nested List Weight Sum (嵌套列表重和)$
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- [LeetCode] 364. Nested List Weight Sum II 嵌套链表权重和之二
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- LeetCode 364. Nested List Weight Sum II
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum-ii/description/ 题目: Given a nested list ...
- [LeetCode] 364. Nested List Weight Sum II_Medium tag:DFS
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- [leetcode]339. Nested List Weight Sum嵌套列表加权和
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- LeetCode 339. Nested List Weight Sum
原题链接在这里:https://leetcode.com/problems/nested-list-weight-sum/ 题目: Given a nested list of integers, r ...
- 【LeetCode】364. Nested List Weight Sum II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 364. Nested List Weight Sum II 大小反向的括号加权求和
[抄题]: Given a nested list of integers, return the sum of all integers in the list weighted by their ...
- 364. Nested List Weight Sum II
这个题做了一个多小时,好傻逼. 显而易见计算的话必须知道当前层是第几层,因为要乘权重,想要知道是第几层又必须知道最高是几层.. 用了好久是因为想ONE PASS,尝试过遍历的时候构建STACK,通过和 ...
随机推荐
- android ListView 可缩放,支持左右上下手势
public class ZoomListView extends ListView implements SwipeListener { public static enum Action { Le ...
- 第一章 C#入门 (Windows窗体应用程序)(一)
我的第一个窗体应用程序(一) [案例说明] 在文本框中显示一行文字“Hello C#!”,单击[显示]按钮后在文本框中显示文字:单击[清除]按钮后清除文本框中的内容. [案例实现步骤] 1.新建项目 ...
- mySQL 判断表是否存
select `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` where `TABLE_SCHEMA`='数据库名' and `TABLE_NAME`= ...
- HTTP响应过程
完整的一次 HTTP 请求响应过程(一)http://mp.weixin.qq.com/s?__biz=MzUzMTA2NTU2Ng==&mid=2247484648&idx=1&am ...
- ThinkPHP模板的知识
php框架 一.真实项目开发步骤: 多人同时开发项目,协作开发项目.分工合理.效率有提高(代码风格不一样.分工不好) 测试阶段 上线运行 对项目进行维护.修改.升级(单个人维护项目,十分困难,代码风格 ...
- 知识点:spring 完全手册
什么是spring spring是一个开源框架,为简化企业级开发而生,使用spring可以使简单的java bean 实现以前只有EJG才能实现的功能. Spring是一个轻量级的控制反转(IoC)和 ...
- CRM 2016 及 CRM 365 更新地址
CRM2016安装程序下载地址: https://www.microsoft.com/zh-cn/download/details.aspx?id=50372 CRM 365 更新地址: https: ...
- VMware Workstation 11 搭建windows server 2012 之sql server 2012集群常见问题整理
1.windows server 2012内置支持iSCSI发起程序无需额外安装,iSCSI Software Target 可作为“文件和存储服务”角色下的内置功能使用 2.拷贝虚拟机的文件加入域时 ...
- svn Advanced
1.Eclipse last 1.Eclipse 安装subversive插件: 安装连接器: 打开视图"SVN Repositories": 创建“资源库位置”: 配置全局范围文 ...
- SpringBoot 之jsp
Boot 内嵌的tomcat 是不支持jsp 的, jetty 也是. 虽然boot也是有默认配置一个InternalResourceViewResolver ,但是它并不像我们在springmvc ...