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 7785077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:11:53+00:00 2026-06-01T20:11:53+00:00

I have a list of numbers like so (randomly generated, numbers sorted within each

  • 0

I have a list of numbers like so (randomly generated, numbers sorted within each sub-group. The groups are disjoint, meaning you won’t find a given number in more than one group):

L=[[19,18,14,9,4],[15,12,11,10,6,5],[8],[16,13,3,2],[17,7,1]]

I am trying to count the number of ways I can create a decreasing-triplet.

A decreasing-triplet is a triplet where we scan the list from left to right and pluck out element 1 from a group, then element 2 from another group, then element 3 from another group where the end result should be in decreasing order naturally.

For instance, (19,11,7) is a valid decreasing triplet because these numbers come from different sub-lists and are in decreasing, natural order (19 comes before 11 which comes before 7 in the master list).

To clarify with a counter-example: (15, 9, 8) would not be a decreasing triplet because the 9 is coming from an earlier sublist than 15.

I am trying to count the number of decreasing triplets using dynamic programming or memoization of some sort. It is easy enough to set up a loop structure like so:

for i in xrange(0,len(L)-2):
    for j in xrange(i+1, len(L)-1):
        for k in xrange(j+1, len(L)):
            for item1 in L[i]:
                for item2 in L[j]:
                    if item1>item2:
                        for item3 in L[k]:
                            if item2>item3:
                                count+=1

But it does not scale very well for larger lists. I feel like there should be some way to count triplets by going through the list once. For instance, if I know one number is larger than another (or if I know how many numbers it’s larger than), I feel like I should be able to re-use that information later.

For instance, I know 16 can come before 7 or 1 in a valid triplet. That’s 2 “pairs.” Therefore, if I am looking to create a triplet as I go backward in the list, and I look at, say, 19, I should be able to say “It’s bigger than 16, therefore you can create two triplets from this because we know 16 is larger than 2 numbers.” and so on.

I am just thinking out loud but would appreciate some insight.

  • 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-06-01T20:11:53+00:00Added an answer on June 1, 2026 at 8:11 pm

    Use an index i between 0 and n instead of nested loops.
    Keep track of the last element of the current triplet.
    And use a memo to make it efficient.

    L=[[19,18,14,9,4],[15,12,11,10,6,5],[8],[16,13,3,2],[17,7,1]]
    
    n=len(L)
    
    memo = {}
    def f(i,j,last):
      if (i,j,last) in memo:
        return memo[(i,j,last)]
      if j==3:
        return 1
      if i==n:
        return 0
      res=0
      # take one from L[i]
      for x in L[i]:
        if last > x:
          res+=f(i+1,j+1,x)
      # don't take any element from L[i]
      res += f(i+1,j,last)
      memo[(i,j,last)] = res
      return res
    
    BIG = 10**9
    print f(0,0,BIG)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a list of numbers like this one: 10,9,8,8,9,7,6,5,4,6,7,8,11,10,12,14,16,20,30,29,28,29,27,25,20,18,15,10,8,5,4,1 I want to
I have a list of numbers which looks like this: 1.234D+1 or 1.234D-02 .
I have a List with numbers, and I'd like to find the position of
I have a file that contain list of numbers that looks like this: 10^-92
So I have a list of numbers in python in a list like this
I have a list of numbers, say data = [45,34,33,20,16,13,12,3] I'd like to compute
I have a list with numeric strings, like so: numbers = ['1', '5', '10',
I have a list of numbers which total 540000. I would like to sort
I have a list of numbers that i would like to send out onto
My spreadsheet is set up like this: I have a list of job numbers

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.