由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - string scramble 的时间复杂度
相关主题
Leetcode Scramble String简单解法Leetcode problems' difficulty
scramble string 怎么用dp 阿?过两天就要 G家 onsite了
发包子请教大牛:scramble string这题递归的复杂度一道有关String的面试题
scramble的复杂度问个算法题
leetcode的scramble string的test cases是不是有问题?request solutions to 2 questions on leetcode
scramble stringstring interleave
有人面试碰到过scramble string这个题吗?问道careercup 150 题目的复杂度
关于leetcode的Scramble String问题二爷的scramble string能跑的起来么?
相关话题的讨论汇总
话题: string话题: rgeat话题: scramble话题: 时间话题: 经典
进入JobHunting版参与讨论
1 (共1页)
H****s
发帖数: 247
1
用经典DP肯定是O(N^3), 但是用经典递归呢(没有剪枝和caching)?
能想到的是:O(N) = O(N-1) + O(N-2) + O(N-3) + ... + O(1) + N
但怎么解啊?
H****s
发帖数: 247
2
居然没人回答,估计是我的问题太弱了,自己顶,把题目也贴在这里
Given a string s1, we may represent it as a binary tree by partitioning it
to two non-empty substrings recursively.
Below is one possible representation of s1 = "great":
great
/ \
gr eat
/ \ / \
g r e at
/ \
a t
To scramble the string, we may choose any non-leaf node and swap its two
children.
For example, if we choose the node "gr" and swap its two children, it
produces a scrambled string "rgeat".
rgeat
/ \
rg eat
/ \ / \
r g e at
/ \
a t
We say that "rgeat" is a scrambled string of "great".

【在 H****s 的大作中提到】
: 用经典DP肯定是O(N^3), 但是用经典递归呢(没有剪枝和caching)?
: 能想到的是:O(N) = O(N-1) + O(N-2) + O(N-3) + ... + O(1) + N
: 但怎么解啊?

j*****y
发帖数: 1071
3
recursive的 时间代价感觉是
T(n) = 2(T(1) + T(n - 1) + T(2) + T(n - 2) + ... + T(n - 1) + T(1)) =
4(T(1) + T(2) + ... + T(n - 1))
T(n) = O(5^n)

it

【在 H****s 的大作中提到】
: 居然没人回答,估计是我的问题太弱了,自己顶,把题目也贴在这里
: Given a string s1, we may represent it as a binary tree by partitioning it
: to two non-empty substrings recursively.
: Below is one possible representation of s1 = "great":
: great
: / \
: gr eat
: / \ / \
: g r e at
: / \

H****s
发帖数: 247
4
感觉比较靠谱,等我有时间好好算算。

【在 j*****y 的大作中提到】
: recursive的 时间代价感觉是
: T(n) = 2(T(1) + T(n - 1) + T(2) + T(n - 2) + ... + T(n - 1) + T(1)) =
: 4(T(1) + T(2) + ... + T(n - 1))
: T(n) = O(5^n)
:
: it

1 (共1页)
进入JobHunting版参与讨论
相关主题
二爷的scramble string能跑的起来么?leetcode的scramble string的test cases是不是有问题?
google scramble string O(n) 解法scramble string
请教那个scramble string的题目有人面试碰到过scramble string这个题吗?
LeetCode 上的题目 AC Rate。关于leetcode的Scramble String问题
Leetcode Scramble String简单解法Leetcode problems' difficulty
scramble string 怎么用dp 阿?过两天就要 G家 onsite了
发包子请教大牛:scramble string这题递归的复杂度一道有关String的面试题
scramble的复杂度问个算法题
相关话题的讨论汇总
话题: string话题: rgeat话题: scramble话题: 时间话题: 经典