Technical Interviews - Longest Increasing Subsequence

How to do the Longest Increasing Subsequence code for technical interviews.

πŸ‘︎ 4
πŸ’¬︎
πŸ‘€︎ u/help-me-grow
πŸ“…︎ Dec 06 2021
🚨︎ report
Longest Increasing Subsequence
πŸ‘︎ 19
πŸ’¬︎
πŸ‘€︎ u/ilia_rr
πŸ“…︎ Oct 17 2021
🚨︎ report
Bot explains how to find Longest Increasing Subsequence.

I've built a bot which explains how to find Longest Increasing Subsequence https://bot.chib.me/courses/longest-increasing-subsequence/

If you have any questions or suggestions, please, write a comment.

πŸ‘︎ 22
πŸ’¬︎
πŸ‘€︎ u/web-chib
πŸ“…︎ May 20 2021
🚨︎ report
longest increasing subsequence with patience sort. How do we actually print the LIS?

https://leetcode.com/problems/longest-increasing-subsequence/

I understood how to get the correct length by doing patience sort. But this does not return the correct LIS. Is there a way to print the correct LIS?

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/gamesdf
πŸ“…︎ Aug 16 2021
🚨︎ report
Bot explains how to find Longest Increasing Subsequence. /r/leetcode/comments/nh8e…
πŸ‘︎ 5
πŸ’¬︎
πŸ‘€︎ u/web-chib
πŸ“…︎ May 21 2021
🚨︎ report
Given a list of unsorted integers, find the longest increasing or decreasing subsequence

note: search for longest increasing and decreasing and return the longer, not bitonic

πŸ‘︎ 34
πŸ’¬︎
πŸ‘€︎ u/Bulbasaur2015
πŸ“…︎ Jun 26 2019
🚨︎ report
[LECTURE] Konstantin Matveev - "A surprising journey of longest increasing subsequences."

Hey y'all,

Your friendly local Rutgers Undergrad Math Association here!

Our next professor talk will be by Professor Konstantin Matveev. It'll be called "A surprising journey of longest increasing subsequences." and will be on October 8th at 7:00 PM. It will be hosted on Zoom at the following link: https://rutgers.zoom.us/j/93679418896?pwd=SUM0SXRKZEZBdHllOXZMTWhzajBOZz09

Abstract: I will talk about an eventful story of longest increasing subsequences. It will touch on a combinatorial geometry problem that has led to a 68 years long marriage, on computer experiments run by one of the key participants of the Manhattan project, on representation theory of symmetric groups, on random matrices, and on statistical physics.

See you there!

~the RUMA board

πŸ‘︎ 6
πŸ’¬︎
πŸ‘€︎ u/RutgersRUMA
πŸ“…︎ Oct 03 2020
🚨︎ report
A Visual Guide to Solving the Longest Increasing Subsequence Problem algodaily.com/challenges/…
πŸ‘︎ 11
πŸ’¬︎
πŸ‘€︎ u/algodaily
πŸ“…︎ Jul 18 2020
🚨︎ report
Find longest increasing subsequence in a circular linked list

Someone asked me this question. At that time, my proposed solution was like this :

  1. Create a temporary array set it to zeros

  2. Then for all elements in list, if the current is greater than previous update temporary array's value as follows

    1. dp[i]=max(dp[i],dp[i-1]+1)
    2. The reason behind this is as follows: We assume that all elements in the list up to node i-1 are in increasing order.
    3. Then if a new element i.e node i is greater than previous element, we add it to the subsequence ending at i-1 otherwise we keep it as it is.
    4. And hence,dp or the temporary array will keep track of all elements that are part of the longest increasing subsequence.
  3. To prevent steps 2 from going into an infinite loop due to the presence of cycle, I feel that we should also have another step to detect the point where the cycle ends and starts

Please let me know if my reasoning makes sense. Any other suggestions will be welcome.

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/sgarg2
πŸ“…︎ Nov 15 2019
🚨︎ report
Analyzing Run Time of Longest Increasing Subsequence with the Master Method

Hi,

I'm looking at a brute force implementation of the Longest Increasing Subsequence problem.

Here's the code in Java

public class Solution {

    public int lengthOfLIS(int[] nums) {
        return lengthofLIS(nums, Integer.MIN_VALUE, 0);
    }

    public int lengthofLIS(int[] nums, int prev, int curpos) {
        if (curpos == nums.length) {
            return 0;
        }
        int taken = 0;
        if (nums[curpos] > prev) {
            taken = 1 + lengthofLIS(nums, nums[curpos], curpos + 1);
        }
        int nottaken = lengthofLIS(nums, prev, curpos + 1);
        return Math.max(taken, nottaken);
    }
}

I'm trying to use the Master Method to analyze the runtime, but I'm having a bit of trouble?

The runtime for this is 2^n , so does that mean it's not possible to use the Master Method to analyze this?

It seems like the rate of subproblem proliferation is 2 (a), but I'm a bit stuck after that.

I would really appreciate any help.

Thanks!

πŸ‘︎ 9
πŸ’¬︎
πŸ‘€︎ u/EducationalHound
πŸ“…︎ Mar 04 2020
🚨︎ report
Longest Increasing Subsequence with K exceptions

I want to find the LIS but with maximum K exceptions. I managed to write it with at least one exception. Could somebody give a hint?

Here i my code:

>#include <cstdio>
>
>#include <vector>
>
>#include <iostream>
>
>#include <algorithm>
>
>#include <stack>
>
>
>
>using namespace std;
>
>
>
>vector<long int> tail;
>
>vector<long int> bob_increasing(1,1);
>
>vector<long int> bob_decreasing;
>
>vector<long int> v;
>
>
>
>
>
>int binary1(vector<long int>& v, long int l, long int r, long int key)
>
>{
>
>while (r - l > 1) {
>
>long int m = l + (r - l) / 2;
>
>if (v[m] >= key)
>
>r = m;
>
>else
>
>l = m;
>
>}
>
>
>
>return r;
>
>}
>
>
>
>int LIS(vector<long int>& v)
>
>{
>
>if (v.size() == 0)
>
>return 0;
>
>
>
>vector<long int> tail(v.size(), 0);
>
>int length = 1; // always points empty slot in tail
>
>
>
>tail[0] = v[0];
>
>for (int i = 1; i < v.size(); i++) {
>
>
>
>if (v[i] < tail[0])
>
>tail[0] = v[i];
>
>
>
>else if (v[i] > tail[length - 1])
>
>tail[length++] = v[i];
>
>
>
>else
>
>tail[binary1(tail, -1, length - 1, v[i])] = v[i];
>
>
>
>bob_increasing.push_back(length);
>
>}
>
>
>
>}
>
>int binary2(vector<long int>& v, long int l, long int r, long int key)
>
>{
>
>while (r - l > 1) {
>
>long int m = l + (r - l) / 2;
>
>if (v[m] <= key)
>
>r = m;
>
>else
>
>l = m;
>
>}
>
>
>
>return r;
>
>}
>
>
>
>
>
>int LDS(vector<long int>& v)
>
>{
>
>if (v.size() == 0)
>
>return 0;
>
>
>
>vector<long int> tail(v.size(), 0);
>
>
>
>int length = 1; // always points empty slot in tail
>
>
>
>tail[0] = v[v.size()-1];
>
>for (int i = v.size()-1; i >=0; i--)

... keep reading on reddit ➑

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/Bob_206
πŸ“…︎ Dec 27 2019
🚨︎ report
Recursively finding the longest increasing subsequence

https://en.wikipedia.org/wiki/Longest_increasing_subsequence is the problem.

I have two functions that allow me to do this. The variable length is a class variable that holds the length of the array. The array I conveniently called array holds the sequence of numbers.

This is the code:

int LISmain(){
    
    int *LIS = new int[length];
    for (int t = 0; t &lt; length ; t++){
        LIS[t] = 0;
    }

    for (int i = 0; i &lt; length; i++){
        int i_max = 1;
        if (i == 0){
            i_max = LISrecursion(i, -1);
        }
        else{
            i_max = LISrecursion(i,array[i-1]);
        }
        LIS[i] = i_max;
    }
    int temp = 0;
    for (int j = 0; j &lt; length; j++){
        if (LIS[j] &gt; temp){
            temp = LIS[j];
        }
    }

    delete [] LIS;

    return temp;
}

int lisRecursion(int index, int prevElement){

    int *max = new int[length - index];
    for (int k = 0; k &lt; length - index; k++){
        max[k] = 0;
    }
    int currentElement = array[index];

    for (int p = 1; p &lt; (length-index); p++){
        int lis = 0;
        lis += lisRecursion(index + p, currentElement);
        if (currentElement &gt; prevElement){
            lis++;
        }
        max[p] = lis;
    }

    int temporary = 0;
    for (int j = 0; j &lt; length - index; j++){
        if (max[j] &gt; temporary){
            temporary = maxArray[j];
        }
    }
    delete [] max;

    return temporary;
}

This algorithm is giving me slightly incorrect answers and I can't understand why. Thoughts?

πŸ‘︎ 15
πŸ’¬︎
πŸ‘€︎ u/lambo4bkfast
πŸ“…︎ Sep 04 2017
🚨︎ report
Code for "Longest increasing subsequence" is on Rosetta Code as a new task. rosettacode.org/wiki/Long…
πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/Paddy3118
πŸ“…︎ Aug 17 2013
🚨︎ report
Calculating the longest increasing subsequence where two attributes are considered for each item.

I have an assignment and want to know whether my way of going about it is the correct way of doing it.

The assignment is to calculate the longest increasing subsequence of people where each person has 2 attributes (lets call their attributes "Hand size" and "Foot size"). The sequence to find is the sequences where hand size increases and foot size decreases (note: the two attribute values are random for each person, no correlations exist between them).

We need an efficient way of calculating the longest set of people where hand increased and foot decreases. Therefore, as the set of people is in a more or less random order too, I thought it would be best to order by one attribute first using a good sorting algorithm (something nlogn). That way, as the one attribute has now been taken care of, I only have to focus on the second attribute now and whether I can find sequences of these (this would be done using the standard nlogn method for finding these - I don't know how to do that yet, I am still researching).

Overall the time complexity would be O(2nlogn) (one for sort, one for search) or simply O(nlogn).

I wanted to check that is is correct before I continued and wasted my time.

πŸ‘︎ 6
πŸ’¬︎
πŸ“…︎ Apr 21 2015
🚨︎ report
Longest Increasing Subsequence (LIS) length in less than 10 lines techiedelight.com/longest…
πŸ‘︎ 8
πŸ’¬︎
πŸ‘€︎ u/techiedelight
πŸ“…︎ Jan 01 2017
🚨︎ report
Longest Common and Longest Increasing Subsequence Algorithm Implementations in C sitterle.co/post/6/Longes…
πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/Andrey_Karpov_N
πŸ“…︎ Nov 23 2012
🚨︎ report
Patience sort and the Longest increasing subsequence wordaligned.org/articles/…
πŸ‘︎ 9
πŸ’¬︎
πŸ‘€︎ u/gthank
πŸ“…︎ Mar 26 2009
🚨︎ report
Longest Common and Longest Increasing Subsequence Algorithm Implementations in C sitterle.co/post/6/Longes…
πŸ‘︎ 5
πŸ’¬︎
πŸ‘€︎ u/einherjer
πŸ“…︎ Nov 21 2012
🚨︎ report
I can't think in python as I did in c unfortunately, in 2022 they gave us a longest_match(sequence, subsequence) but it return 0 I can't figure out why.

# TODO: Read database file into a variable

li = []

database = sys.argv[1]

with open(database,"r") as data:

dataReader = csv.DictReader(data)

for samble in dataReader:

# data.append(sample)

li.append(samble)

# TODO: Read DNA sequence file into a variable

string = sys.argv[2]

with open(string) as line:

text = line.readlines()

# TODO: Find longest match of each STR in DNA sequence

listo = []

for i in samble:

if i != "name":

listo.append(longest_match(text ,i))

print(listo)

// the output is [0, 0, 0]

print(samble)

// the output is {'name': 'Charlie', 'AGATC': '3', 'AATG': '2', 'TATC': '5'}

print(text)

// the output is ['AAGGTAAGTTTAGAATATAAAAGGTGAGTTAAATAATAGAAGG\n']

print(i)

# TODO: Check database for matching profiles

// the output is TATC

return

πŸ‘︎ 2
πŸ’¬︎
πŸ“…︎ Jan 11 2022
🚨︎ report
How will solving a problem to find longest subsequence (maintaining order) between 2 strings prove that I am a good data engineer ?

I feel the process is becoming one sided.. These questions should be asked for Software Engineering roles(if at all).

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/ezio20
πŸ“…︎ Mar 12 2021
🚨︎ report
Algorithmic puzzle: Continuous Increasing Subsequences bor0.wordpress.com/2021/0…
πŸ‘︎ 3
πŸ’¬︎
πŸ‘€︎ u/buritomath
πŸ“…︎ Apr 09 2021
🚨︎ report
Question about DP (longest common substring vs. longest common subsequence)

I'm still confused on the intuitive difference between "longest common substring' (finding the longest contiguous substring between two strings) and "longest common subsequence" (finding longest non-contiguous substring between two strings).

Going off of "Grokking Algorithms", suppose we're comparing two strings:

"FOR" and "FOT". 

If we're building a DP table and we're on the 'square' that represents the 'R' and 'T' (the last chars)...longest common subsequence table would have us find the max of the left and top cell (FOR vs. FO or FO vs. FOT).

But when looking for contiguous substrings, we don't do this check. We just put in '0'.

What? Why don't we check the best answer for FOR vs. FO and FO vs. FOT? Why just put in '0'?

After the longest common substring DP table is finished, you traverse it again to find the max value. My best guess is the values don't need to be 'carried over' like it does in longest common subsequence since you're going to do a 'max' check eventually...but that still doesn't fit.

Longest common substring: https://youtu.be/BysNXJHzCEs

Longest common subsequence: https://youtu.be/NnD96abizww

πŸ‘︎ 2
πŸ’¬︎
πŸ‘€︎ u/tranderman
πŸ“…︎ Jan 01 2021
🚨︎ report
A Faster Algorithm for Finding the Longest Common Subsequence

Both CS Dojo and GeeksforGeeks suggest a bottom-up O(nm) approach to finding the longest common subsequence between two strings of lengths n and m. Yet, my Python implementation of said approach still times out on the more difficult test cases in the corresponding HackerRank problem. Could there be an even faster algorithm for finding the LCS?

πŸ‘︎ 5
πŸ’¬︎
πŸ‘€︎ u/shakozzz
πŸ“…︎ Jan 21 2021
🚨︎ report
How do you prove that for any array of integers of lenght n, either the greatest increasing subsequence or the greatest decreasing subsequence has a lenght of over sqrt(n) ?

They are distinct as well. EDIT: I found a really well exaplained solution online guys. I'm posting it here for anyone else who might encounter this problem: https://medium.com/cantors-paradise/a-theorem-of-erdos-and-szekeres-261627b0ffb7

πŸ‘︎ 24
πŸ’¬︎
πŸ‘€︎ u/Acidic_Jew2
πŸ“…︎ Sep 07 2020
🚨︎ report

Please note that this site uses cookies to personalise content and adverts, to provide social media features, and to analyse web traffic. Click here for more information.