- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
任何人都可以帮助我理解 http://www.topcoder.com/stat?c=problem_statement&pm=1259&rd=4493 中提到的问题的解决方案背后的核心逻辑吗?
之字形序列是交替增加和减少的序列。所以,1 3 2 是之字形,但 1 2 3 不是。一个或两个元素的任何序列都是之字形。我们需要找到给定序列中最长的之字形子序列。子序列意味着元素不必是连续的,就像在最长递增子序列问题中那样。因此,1 3 5 4 2 可能有 1 5 4 作为锯齿形子序列。我们对最长的感兴趣。
我知道这是一个动态规划问题,它与How to determine the longest increasing subsequence using dynamic programming? 非常相似。 .
我认为任何解决方案都需要一个外循环来遍历不同长度的序列,而内循环则必须遍历所有序列。
我们将在另一个数组中存储以索引 i 结尾的最长之字形序列,例如索引 i 处的 dpStore。因此,中间结果会被存储起来,以后可以重复使用。这部分对于所有动态规划问题都是通用的。稍后我们找到全局最大值并将其返回。
我的解决方案肯定是错误的,粘贴在这里以展示我到目前为止的内容。我想知道我哪里出错了。
private int isZigzag(int[] arr)
{
int max=0;
int maxLength=-100;
int[] dpStore = new int[arr.length];
dpStore[0]=1;
if(arr.length==1)
{
return 1;
}
else if(arr.length==2)
{
return 2;
}
else
{
for(int i=3; i<arr.length;i++)
{
maxLength=-100;
for(int j=1;j<i && j+1<=arr.length; j++)
{
if(( arr[j]>arr[j-1] && arr[j]>arr[j+1])
||(arr[j]<arr[j-1] && arr[j]<arr[j+1]))
{
maxLength = Math.max(dpStore[j]+1, maxLength);
}
}
dpStore[i]=maxLength;
}
}
max=-1000;
for(int i=0;i<arr.length;i++)
{
max=Math.max(dpStore[i],max);
}
return max;
}
最佳答案
这就是您链接到的问题所说的内容:
A sequence of numbers is called a zig-zag sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a zig-zag sequence.
For example, 1,7,4,9,2,5 is a zig-zag sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, 1,4,7,2,5 and 1,7,4,5,5 are not zig-zag sequences, the first because its first two differences are positive and the second because its last difference is zero.
Given a sequence of integers, sequence, return the length of the longest subsequence of sequence that is a zig-zag sequence. A subsequence is obtained by deleting some number of elements (possibly zero) from the original sequence, leaving the remaining elements in their original order.
这与您在帖子中描述的完全不同。下面解决实际的topcoder问题。
dp[i, 0] = maximum length subsequence ending at i such that the difference between the
last two elements is positive
dp[i, 1] = same, but difference between the last two is negative
for i = 0 to n do
dp[i, 0] = dp[i, 1] = 1
for j = 0 to to i - 1 do
if a[i] - a[j] > 0
dp[i, 0] = max(dp[j, 1] + 1, dp[i, 0])
else if a[i] - a[j] < 0
dp[i, 1] = max(dp[j, 0] + 1, dp[i, 1])
例子:
i = 0 1 2 3 4 5 6 7 8 9
a = 1 17 5 10 13 15 10 5 16 8
dp[i, 0] = 1 2 2 4 4 4 4 2 6 6
dp[i, 1] = 1 1 3 3 3 3 5 5 3 7
^ ^ ^ ^
| | | -- gives us the sequence {1, 17, 5, 10}
| | -- dp[2, 1] = dp[1, 0] + 1 because 5 - 17 < 0.
| ---- dp[1, 0] = max(dp[0, 1] + 1, 1) = 2 because 17 - 1 > 0
1 element
nothing to do
the subsequence giving 7 is 1, 17, 5, 10, 5, 16, 8, hope I didn't make any careless
mistakes in computing the other values)
然后取两个 dp
数组的最大值。
关于algorithm - 动态规划 : Find longest subsequence that is zig zag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6914969/
dic = {'a':4, 'b':5, 'cd':5 } 我正在寻找: 最高值(首先搜索最高值 => b, cd) 最长的键(然后搜索最长的键 => 'cd') 我使用以下代码: max_val =
dic = {'a':4, 'b':5, 'cd':5 } 我正在寻找: 最高值(首先搜索最高值 => b, cd) 最长的键(然后搜索最长的键 => 'cd') 我使用以下代码: max_val =
题目地址:https://leetcode.com/problems/longest-palindrome/open in new window Difficulty: Easy 题目描
题目地址:https://leetcode-cn.com/problems/longest-happy-string/ 题目描述 如果字符串中不含有任何 'aaa','bbb' 或 'ccc' 这
本文关键词:prefix, 公共前缀,题解,leetcode, 力扣,Python, C++, Java 题目地址:https://leetcode.com/problems/longest-com
最长回文子串,题解,leetcode, 力扣,python, C++, java 题目地址:https://leetcode.com/problems/longest-palindromic-sub
题目地址:https://leetcode.com/problems/longest-increasing-subsequence/description/ 题目描述 Given an unsor
题目地址:https://leetcode.com/problems/longest-palindromic-subsequence/description/ 题目描述 Given a strin
题目地址:https://leetcode.com/problems/longest-word-in-dictionary/description/open in new window 题目描述
题目地址:https://leetcode.com/problems/longest-mountain-in-array/description/ 题目描述 Let's call any (con
路由器 (IPv4) Destination Interface 0.0.0.0/0 m0 172.58.128.0/17 m1 1
我想找到给定目录中子目录中最长的路径,因为我遇到了这个错误: The specified path, file name, or both are too long. The fully qualif
我正在尝试创建一个Java程序,该程序读取键盘输入的数字字符串, 并给出最长的升序子字符串。 以下是我的代码: import java.util.Scanner; public class Ascen
我正在尝试编写一个正则表达式来识别单行文本,下划线 ( _ ) 被识别为行继续符。例如,“foo_\nbar”应被视为单行,因为“foo”以下划线结尾。我在尝试: $txt = "foo_\nbar"
我可能在这里做了一些非常愚蠢的事情,但我已经达到了 double 可以达到的极限,并且在我的编译器上(我在 mac 上使用最新的 xcode)long double 似乎也好不到哪里去。 我在别处读到
我已经阅读了 LCS 问题的解决方案。但是现在有一个最长相似子序列问题:序列 C 是两个序列 A、B 的相似子序列当且仅当 C 是 A 的子序列并且我们最多可以替换 C 中的 K 个元素使得 C 是
我将复习在寻找两个等长字符串的最长公共(public)子序列的上下文中讨论动态规划的笔记。有问题的算法输出长度(不是子字符串)。 所以我有两个字符串,比如说: S = ABAZDC,T = BACBA
题目是解决 Sedgewick Wayne 的 Python 书中的以下问题: 给定一个整数数组,编写一个程序,找出最长的连续等值序列的长度和位置,其中该序列前后元素的值较小。 我试过这个问题,遇到了
我们可以用DP(动态规划)找到两个字符串的LCS(最长公共(public)子序列)。通过跟踪 DP 表,我们可以获得 LCS。但是,如果存在不止一个濒海战斗舰,我们如何获得所有的濒海战斗舰呢? 例子:
过去两个小时我一直试图理解这个算法,但似乎无法理解。有人可以用通俗易懂的方式解释一下吗? function lis_length(a) n := a.length q := new A
我是一名优秀的程序员,十分优秀!