Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6649295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:45:24+00:00 2026-05-26T00:45:24+00:00

OK, Here is my problem: Given an array, such as {9, 4, 3, 2,

  • 0

OK, Here is my problem:


Given an array, such as {9, 4, 3, 2, 5, 4, 3, 2}, its longest monotonous decrease subsequence is {9, 5, 4, 3, 2}, where each element is in the same order as in original array. For simplicity, we assume the elements in subsequence are all different.


I have thought about it for a whole day and can’t get a solution… If you have some good advises, please share with me. Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:45:25+00:00Added an answer on May 26, 2026 at 12:45 am

    This is a dynamic programming problem. The idea is to evaluate every possible subsequence combination and store sub problem results in an array so you don’t need to compute them twice.

    public class LDS {
    
        /*
          For each item in the array, get the longest
          decreasing subsequence to that item.
    
          return the max subsequence to that item, adding
          1 for the item itself.
    
          In both calculate and longestToN, subproblem results  are stored and reused to 
          avoid duplicate computation.
        */
        public int calculate(int []arr) {
            int []table = new int[arr.length];
    
            for (int i = 0; i < arr.length; ++i) {
                table[i] = -1;
            }
    
            int m = 0;
    
            for (int n = 0; n < arr.length; ++n) {
    
                if (table[n] == -1) {//Memoize
                    table[n] = longestToN(n, arr, table);
                }
    
                m = Math.max(m, longestToN(n, arr, table));
            }
            return m + 1;
        }
    
        /*
          Recursively finds the longest decreasing subsequence in array up until arr[n] inclusive.
        */
        private int longestToN(int n, int []arr, int []table) {
            int m = 0;
            for (int i = n; i >= 0; --i) {
                if (arr[i] > arr[n]) {
    
                    if (table[i] == -1) {//Memoize
                        table[i] = longestToN(i, arr, table);
                    }
    
                    m = Math.max(m, 1 + table[i]);
                }
            }
            return m;
        }
    }
    

    To run the code:

        LDS lds = new LDS();
    
        int []arr = {9, 4, 3, 2, 5, 4, 3, 2};
        int ret = lds.calculate(arr);
    
        System.out.println("Longest decreasing subsequence is " + ret);
    

    Running the code on your example input calculates 5. I recommend a book called the algorithm design manual for a better understanding of dynamic programming. The author is a professor and posts lectures from his algorithms online @ http://www.cs.sunysb.edu/~skiena/373/.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the jist of the problem: Given a list of sets, such as: [
i have seen a few days ago such problem there is given two array
Here's my problem. Given that I am saving data in an array: fetch.on('message', function(msg)
This is in reference to the question previously asked The problem here is, each
A fairly basic problem for a change. Given a class such as this: public
here is my problem I have the following array (for example) string[] arr =
Problem: Given a matrix in which each row and each column is sorted, write
I've got a problem here with an MSI deployment that I'm working on (using
Just a small SVN problem here. I setup my own SVN server Setting up
I'm having a strange problem here... I have an ASP.NET 3.5 application that has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.