当前位置: 首页 > 知识库问答 >
问题:

求数组中最长的邻接子序列

刘曾琪
2023-03-14

我的任务是编写一个程序,在给定的数组中找到最长的递增连续子序列,并打印该子序列的长度和它自己的子序列。表示数组为:

int[] arr = {3, 6, 5, 1, 9, 3, 2, 3, 4, 5, 1}

最长的连续递增子序列为2,3,4,5,长度为4。因此此方法的输出为

4
2, 3, 4, 5
public class LongestSubsequence {
  public static void main(String[] args) {

    // Test arrays
    int[] arrC = {9, 5, 2, 3, 4, 5};
    int[] arrA = {1, 2, 3, 4, 5, 7};
    int[] arrB = {7, 6, 5, 4, 1, 2};
    int[] arr = {3, 6, 5, 1, 9, 3, 2, 3, 4, 5, 1};

    longestForward(arr);

  }

  // input of the int array, returns nothing.
  public static void longestForward(int[] arr) {
    // variables for Length of longest subsequence found and for the length of the current sequence
    int subSeqLength = 1;
    int longest = 1;
    boolean longestSub = false;
    int indexStart = 0;
    int indexEnd = 0;

    for (int i = 0; i < arr.length-1; i++) {
      //Increases subsequence length variable 
      if (arr[i] < arr[i+1]) {
        subSeqLength++;
      }
      // Sets the current subsequence to the longest variable if it is the longest one found at the time.
      else if (subSeqLength > longest) {
        longest = subSeqLength;
        longestSub = true;
      }
      // if the current sequence being analyzed is the longest one, keeps track of where it starts and ends
      else if (longestSub = true) {
        arr[i] = indexStart;
        arr[i+1] = indexEnd;
      }
      // sets the subsequence length back to one if it is no longer increasing         
      else subSeqLength = 1;
    }

    System.out.println(subSeqLength);
    System.out.println(indexStart);
    System.out.print(indexEnd);
  }
}

我推断,要打印子序列,我需要跟踪最长序列的开始和结束时间,然后让程序在这些elements上打印。但是我的代码似乎没有正确运行。没有给出错误,它只是运行,但不返回任何东西。

非常感谢任何帮助。谢了!

共有1个答案

邹博明
2023-03-14

这里我用注释修正了你的算法:

public static void longestForward(int[] arr)
{
    int subSeqLength = 1;
    int longest = 1;
    int indexStart = 0;
    int indexEnd = 0;

    for (int i = 0; i < arr.length - 1; i++)
    {
        if (arr[i] == arr[i + 1] - 1)//We need to check if the current is equal to the next
        {
            subSeqLength++;//if it is we increment
            if (subSeqLength > longest)//we assign the longest and new bounds
            {
                longest = subSeqLength;
                indexStart = i + 2 - subSeqLength;//make sure the index start is correct
                indexEnd = i + 2;
            }

        } 
        else
            subSeqLength = 1;//else re-initiate the straight length
    }


    for (int i = indexStart; i < indexEnd; i++)//print the sequence
        System.out.println(arr[i] + ", ");        
}
 类似资料:
  • 最大乘积子数组给定一个数组包含正整数和负整数,求最大乘积的子数组。例子: 但不能破题找到子数组。

  • 、和是子序列中的三个连续元素。 例如,如果输入数组为,则最长凸子序列应为:或。 在“最长递增子序列”(LIS)问题中,我尝试用同样的动态规划思想来解决这个问题。但是由于子序列中的每个元素都依赖于前面的两个元素,所以O(n^2)解似乎是不可能的。谢谢你的帮助。

  • 我正试图找到最小和邻接子数组的起始和结束索引。我试了很多次,但还是找不到。为此我使用C++。 求最小和邻接子数组的代码:

  • 这里,在这段代码中,它打印序列的最大子序列的长度,该序列先增加后减少,或者反之亦然。 例如: 输入:1,11,2,10,4,5,2,1 如增-减-增或减-增-减 示例: 投入:7 16 1 6 20 17 7 18 25 1 25 21 11 5 29 11 3 3 26 19

  • 例如:如果数组是[9,8,7,6,5,4,3,1,2,2],它应该返回46(长度为7的子数组[9,8,7,6,5,4,3]和长度为2的子数组[2,2]之和)。不能组合[9,8,7,6,5,4,3]和[1,2,2],因为这将产生长度为10的非素数的连续子数组(幂等性)。 有谁能解释一下如何使用DP来解决这类问题吗?多谢了。

  • 求其和可被K整除的最长子数组。在O(n)中可能吗?如果不是,它能比n^2更好地完成吗?

  • 您将获得一个包含n个元素的数组:<代码>d[0],d[1]。。。,d【n-1】。计算所有相邻子数组的最大差值之和。 形式上:S=sum{max{d[l,..., r]}-min{d[l,..., r}},Δ0 输入: 输出: 解释: l=0;r=1;数组:[1,3]和=最大值([1,3])-最小值([1,3])=3-1=2 l=0;r=2;数组:[1,3,2]和=最大值([1,3,2])-最小值(

  • 在长度为N的数组中求和最大的邻接子数组。 输入格式: 输出格式: 返回一个整数,表示相邻子数组的最大可能和。 制约因素: 输入2:A=[-2,1,-3,4,-1,2,1,-5,4] 产出2:6 说明2:子数组[4,-1,2,1]的最大可能和为6。 你能告诉我为什么下面的代码不起作用,代码中的错误是什么吗: