由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问一道题
相关主题
求这道题的O(N)解 (转载)请教大家一个问题:Maximum Height (Depth) of a Binary Tree Using PreOrder Traversal
Leetcode triangle 题目clarification[leetcode] Maximum Depth of Binary Tree
请教一个leetcode OJ问题为什么oj.leetcode上面的triangle那道题总是超时
minMSwap 这题能比O(n^2)更快的解法吗大牛们看看这题:Find Peak Element II
问一问这个题。如何做LeetCode
请教一道算法题请教一道L的题
求问一题微软一个面试题
求解一道算法题请教一个C内存泄露问题
相关话题的讨论汇总
话题: int话题: list话题: max话题: loop话题: triangle
进入JobHunting版参与讨论
1 (共1页)
c*u
发帖数: 22
1
三角数求最大和,如
By starting at the top of the triangle and moving to adjacent numbers on the
row below, the maximum total from top to bottom is 27
5
9 6
4 6 8
0 7 1 5
I.e. 5 + 9 + 6 + 7 = 27.
find the maximum total from top to bottom a text file containing a triangle
with 100 rows.
g**G
发帖数: 767
2
maintain another array stores the max sum in current level
For example
5 5
9 6 14 11
4 6 8 18 20 19
0 7 1 5 18 27 21 24
c********p
发帖数: 1969
3
13 哪来的?13那个不对吧。。。

【在 g**G 的大作中提到】
: maintain another array stores the max sum in current level
: For example
: 5 5
: 9 6 14 11
: 4 6 8 18 20 19
: 0 7 1 5 18 27 21 24

r**d
发帖数: 316
4
从下往上算,一个简单的DP
0 7 1 5
11 13 13
22 19
27


the
triangle

【在 c*u 的大作中提到】
: 三角数求最大和,如
: By starting at the top of the triangle and moving to adjacent numbers on the
: row below, the maximum total from top to bottom is 27
: 5
: 9 6
: 4 6 8
: 0 7 1 5
: I.e. 5 + 9 + 6 + 7 = 27.
: find the maximum total from top to bottom a text file containing a triangle
: with 100 rows.

o******e
发帖数: 1001
5
怎么看不懂题目。

the
triangle

【在 c*u 的大作中提到】
: 三角数求最大和,如
: By starting at the top of the triangle and moving to adjacent numbers on the
: row below, the maximum total from top to bottom is 27
: 5
: 9 6
: 4 6 8
: 0 7 1 5
: I.e. 5 + 9 + 6 + 7 = 27.
: find the maximum total from top to bottom a text file containing a triangle
: with 100 rows.

g**G
发帖数: 767
6
en... fixed

【在 c********p 的大作中提到】
: 13 哪来的?13那个不对吧。。。
c********p
发帖数: 1969
7
yeah, it makes sense now.
it's a good way to do it. I used to do it from the butt.

【在 g**G 的大作中提到】
: en... fixed
J****3
发帖数: 427
8
leetcode triangle??
c*u
发帖数: 22
9
这样可以么? list 保存每一轮数值并用变量记下该轮最大值:
从上往下
string filename = "data.txt";
if (File.Exists(filename))
{
string line;
List list = new List();
StreamReader sr = File.OpenText(filename);
int max = 0;
while ((line = sr.ReadLine()) != null)
{
int[] array = line.Trim().Split(' ').Select(n => int.Parse(n)).ToArray<
int>();
int size = array.Count();
if (size ==1)
{
list.Add( array[0]);
max = list[0];
}
else if (size>1)
{
int next=0, val = 0;
for (int i = 0; i < size-1 ; i++)
{
val = list[i];

if (i == 0)
list[i] = list[i] + array[i];
else
{
int mid = list[i] + array[i];
list[i] = (mid > next) ? mid : next;
}
max = (max>list[i])?max:list[i];
next = val + array[i + 1];
if (i == size - 2)
{
list.Add(next);
max = (max > next) ? max : next;
break;
}
}
}
}
Console.WriteLine(max);
sr.Close();
}
data.txt
5
9 6
4 6 8
0 7 1 5

【在 g**G 的大作中提到】
: maintain another array stores the max sum in current level
: For example
: 5 5
: 9 6 14 11
: 4 6 8 18 20 19
: 0 7 1 5 18 27 21 24

b******7
发帖数: 92
10
简单dp
sum(i, j) 自底向上到i层j元素的最大和,目标sum(0, 0)
sum(i, j) = A[i,j] + max{sum(i+1, j), sum(i+1, j+1)}
边界sum(n-1, j) = A[n-1, j]

the
triangle

【在 c*u 的大作中提到】
: 三角数求最大和,如
: By starting at the top of the triangle and moving to adjacent numbers on the
: row below, the maximum total from top to bottom is 27
: 5
: 9 6
: 4 6 8
: 0 7 1 5
: I.e. 5 + 9 + 6 + 7 = 27.
: find the maximum total from top to bottom a text file containing a triangle
: with 100 rows.

c*u
发帖数: 22
11
string filename = "data.txt";
if (File.Exists(filename))
{
int depth = 100;
int[,] arr = new int[depth,depth];
StreamReader sr = File.OpenText(filename);
int loop = 0;
while ((line = sr.ReadLine()) != null)
{
int[] array = line.Trim().Split(' ').Select(n => int.Parse(n)).
ToArray();
for(int i=0;i arr[loop, i] = array[i];

Console.WriteLine(line);
loop++;
}
sr.Close();
//从最底下两层开始
for (loop = depth - 2; loop >= 0; loop --)
for (int j = 0; j <= loop; j++)
arr[loop, j] += Math.Max(arr[loop + 1, j], arr[loop + 1, j + 1]);
Console.WriteLine("The maximum path sum is: " + arr[0, 0]);
}

【在 c*u 的大作中提到】
: 这样可以么? list 保存每一轮数值并用变量记下该轮最大值:
: 从上往下
: string filename = "data.txt";
: if (File.Exists(filename))
: {
: string line;
: List list = new List();
: StreamReader sr = File.OpenText(filename);
: int max = 0;
: while ((line = sr.ReadLine()) != null)

p*i
发帖数: 411
12
bottom-up
vector> num; // assume this is the input
const int n = num.size();
if (n == 0) return 0;
for (int i = n-2; i >= 0 --i) {
for (int j = 0; j <= i; ++j) {
num[i][j] += max(num[i+1][j], num[i+1][j+1]);
}
}
return num[0][0];

the
triangle

【在 c*u 的大作中提到】
: 三角数求最大和,如
: By starting at the top of the triangle and moving to adjacent numbers on the
: row below, the maximum total from top to bottom is 27
: 5
: 9 6
: 4 6 8
: 0 7 1 5
: I.e. 5 + 9 + 6 + 7 = 27.
: find the maximum total from top to bottom a text file containing a triangle
: with 100 rows.

k*******t
发帖数: 144
13
同感

【在 J****3 的大作中提到】
: leetcode triangle??
1 (共1页)
进入JobHunting版参与讨论
相关主题
请教一个C内存泄露问题问一问这个题。
一道面试题请教一道算法题
问个题: 找read-only array中duplicate的数求问一题
请教一道题求解一道算法题
求这道题的O(N)解 (转载)请教大家一个问题:Maximum Height (Depth) of a Binary Tree Using PreOrder Traversal
Leetcode triangle 题目clarification[leetcode] Maximum Depth of Binary Tree
请教一个leetcode OJ问题为什么oj.leetcode上面的triangle那道题总是超时
minMSwap 这题能比O(n^2)更快的解法吗大牛们看看这题:Find Peak Element II
相关话题的讨论汇总
话题: int话题: list话题: max话题: loop话题: triangle