Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

1048 的DP 感觉有点问题。 #158

Open
@xiaoming-lu

Description

我们是打算 sort 一下 然后按倒序排列
比如
bdca, bda, bca, ba, b, a

并且有一个poss 来纪录 每一种LENGTH 最初始的index,
POSS[1] = 4
POSS[2] = 3
POSS[3] = 1
POSS[4] = 0

然后我们从后面Index 开始

dp := make([]int, len(words))
for i := len(words) - 1; i >= 0; i-- {
	dp[i] = 1
	for j := poss[len(words[i])+1]; j < len(words) && len(words[j]) == len(words[i])+1; j++ {
		if isPredecessor(words[j], words[i]) {
			dp[i] = max(dp[i], 1+dp[j])
		}
	}
	res = max(res, dp[i])
}
return res

word[I] 是小的
找word[J](多出一个字母的) 来判断 I 是否是 J 的pre
我觉的 我们应该是要UPDATE dp[j] = max ( dp[i] + 1, dp[j])
这样 我们可以不断地往前推。 而且最开始要加一个condition
if(dp[i] == 0 )
dp[i] = 1;
防止之前找到过PRE的被重新重置到1.

我不清楚 是不是我没太了解你的思路 但感觉你这个代码应该过不了吧。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /