A. Alyona and copybooks Problem Description: Little girl Alyona is in a shop to buy some copybooks for school. She study four subjects so she wants to have equal number of copybooks for each of the subjects. There are three types of copybook's packs…
How far away [题目链接]How far away [题目类型]dfs+邻接表 &题意: 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起来,接下了有m次询问,每次询问两个房子a,b之间的距离是多少. &题解: 我以为是LCA的题,但dfs居然也能过,感觉是数据有点弱,(时间复杂度是4000x200x10,想了想10组又不能全是200,所以应该勉强可以)但学到了邻接表的数组形式,用数组表示链表,很厉害的样子,感觉和前向星好相似啊 &代码: #include…
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. Example 1: Input: [[1,1],2,[1,1]] Output: 10 Expl…
Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. For example, given a 3-ary tree: We should return its max depth, which is 3. Note: The dept…
Given an integer matrix, find the length of the longest increasing path. From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary (i.e. wrap-around is not allowed). E…
题意转载自https://www.cnblogs.com/blumia/p/poj3279.html 题目属性:DFS 相关题目:poj3276 题目原文:[desc]Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an…
Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is played on an ice game board on which a square mesh is marked. They use only a single stone. The…
https://oj.leetcode.com/problems/path-sum-ii/ Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 return…
原题代号:HDU 4277 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4277 原题描述: USACO ORZ Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5208 Accepted Submission(s): 1725 Problem Description Like ev…
前言 之前一直想不明白dfs的时间复杂度是怎么算的,前几天想了下大概想明白了,现在记录一下. 存图方式都是链式前向星或邻接矩阵.主要通过几道经典题目来阐述dfs时间复杂度的计算方法. $n$是图中结点的个数,$e$是图中边的个数. 深度优先遍历图的每一个结点 时间复杂度为:链式前向星:$O\left( n + e \right)$:邻接矩阵:$O\left( n \right)$ 给定我们一个图(链式前向星存储),通过深度优先遍历的方式把图中每个结点遍历一遍. 首先,图中每个结点最多被遍历一次,…
leetcode 地址: https://leetcode.com/contest/detail/1 (1)-- Lexicographical Numbers Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9]. Please optimize your algorithm to use less t…