A list of puns related to "Longest increasing subsequence"
How to do the Longest Increasing Subsequence code for technical interviews.
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.
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?
note: search for longest increasing and decreasing and return the longer, not bitonic
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
Someone asked me this question. At that time, my proposed solution was like this :
Create a temporary array set it to zeros
Then for all elements in list, if the current is greater than previous update temporary array's value as follows
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.
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!
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--)
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 < length ; t++){
LIS[t] = 0;
}
for (int i = 0; i < 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 < length; j++){
if (LIS[j] > 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 < length - index; k++){
max[k] = 0;
}
int currentElement = array[index];
for (int p = 1; p < (length-index); p++){
int lis = 0;
lis += lisRecursion(index + p, currentElement);
if (currentElement > prevElement){
lis++;
}
max[p] = lis;
}
int temporary = 0;
for (int j = 0; j < length - index; j++){
if (max[j] > temporary){
temporary = maxArray[j];
}
}
delete [] max;
return temporary;
}
This algorithm is giving me slightly incorrect answers and I can't understand why. Thoughts?
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.
# 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
I feel the process is becoming one sided.. These questions should be asked for Software Engineering roles(if at all).
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
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?
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
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.