摘要:最长连续递增递减子序列,设置正向计数器,后一位增加则计数器加,否则置。反向计数器亦然。每一次比较后将较大值存入。
Problem 最长连续递增/递减子序列
Give an integer array,find the longest increasing continuous subsequence in this array.
An increasing continuous subsequence:
Can be from right to left or from left to right.
Indices of the integers in the subsequence should be continuous.
For [5, 4, 2, 1, 3], the LICS is [5, 4, 2, 1], return 4.
For [5, 1, 2, 3, 4], the LICS is [1, 2, 3, 4], return 4.
设置正向计数器,后一位增加则计数器加1,否则置1。反向计数器亦然。
每一次比较后将较大值存入max。
O(1) space, O(n) time
public class Solution { public int longestIncreasingContinuousSubsequence(int[] A) { if (A == null || A.length == 0) return 0; int n = A.length; int count = 1, countn = 1, max = 1; int i = 1; while (i != n) { if (A[i] >= A[i-1]) { count++; countn = 1; max = Math.max(max, count); } else { countn++; count = 1; max = Math.max(max, countn); } i++; } return max; } }
DP using two dp arrays, O(n) space
public class Solution { public int longestIncreasingContinuousSubsequence(int[] A) { if (A == null || A.length == 0) return 0; int n = A.length; if (n == 1) return 1; int[] dp = new int[n]; int[] pd = new int[n]; int maxdp = 0, maxpd = 0; dp[0] = 1; for (int i = 1; i < n; i++) { dp[i] = A[i] >= A[i-1] ? dp[i-1]+1 : 1; maxdp = Math.max(maxdp, dp[i]); } pd[n-1] = 1; for (int i = n-2; i >= 0; i--) { pd[i] = A[i] >= A[i+1] ? pd[i+1]+1 : 1; maxpd = Math.max(maxpd, pd[i]); } return Math.max(maxdp, maxpd); } }
DP using one dp arrays, O(n) space
public class Solution { public int longestIncreasingContinuousSubsequence(int[] A) { if (A == null || A.length == 0) return 0; int n = A.length; if (n == 1) return 1; int[] dp = new int[n]; int maxdp = 0, maxpd = 0; dp[0] = 1; for (int i = 1; i < n; i++) { dp[i] = A[i] >= A[i-1] ? dp[i-1]+1 : 1; maxdp = Math.max(maxdp, dp[i]); } for (int i = 1; i < n; i++) { dp[i] = A[i] <= A[i-1] ? dp[i-1]+1 : 1; maxpd = Math.max(maxpd, dp[i]); } return Math.max(maxdp, maxpd); } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/65419.html
Problem Given a sequence of integers, find the longest increasing subsequence (LIS). You code should return the length of the LIS. Clarification Whats the definition of longest increasing subsequence?...
摘要:样例给出,这个是,返回给出,这个是,返回挑战要求时间复杂度为或者说明最长上升子序列的定义最长上升子序列问题是在一个无序的给定序列中找到一个尽可能长的由低到高排列的子序列,这种子序列不一定是连续的或者唯一的。 Longest Increasing Subsequence 本文最新版本位于 https://yanjia.me/zh/2018/11/... 给定一个整数序列,找到最长上升子序...
摘要:题目要求思路和代码这里采用广度优先算法加上缓存的方式来实现。我们可以看到,以一个节点作为开始构成的最长路径长度是确定的。因此我们可以充分利用之前得到的结论来减少重复遍历的次数。 题目要求 Given an integer matrix, find the length of the longest increasing path. From each cell, you can ei...
Problem 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 ou...
摘要:解题思路求不必连续的最长升序字符串长度,采用动态规划,利用状态表示以字符结尾的最长子串的长度,那么状态转移方程就是且必须小于另外还需维护一个最大长度。 Longest Increasing SubsequenceGiven an unsorted array of integers, find the length of longest increasing subsequence. ...
阅读 1516·2023-04-25 18:56
阅读 1460·2021-09-29 09:34
阅读 1692·2021-09-22 15:51
阅读 3465·2021-09-14 18:03
阅读 1140·2021-07-23 17:54
阅读 1996·2019-08-29 18:38
阅读 2885·2019-08-29 12:38
阅读 594·2019-08-26 13:41