10 examples of 'longest increasing subsequence python' in Python

Every line of 'longest increasing subsequence python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
50def longest_increasing_subsequence(a, verbose=False):
51
52 s = a[:]
53 s.sort()
54
55 (cost, alignment) = lcs(a, s)
56
57 if verbose:
58 print
59 print "input: ", a
60 print "sorted: ", s
61
62 print 'cost:', cost
63 print
64 for (a,b) in alignment:
65 print ' %16s => %s' % (a,b)
66 print
67
68 return [a for (a,b) in alignment if a is not None and b is not None]
10def longestCommonSubsequence(self, A, B):
11 """
12 let f(i, j) represents the LCS END WITH A[i], B[j]
13 f(i, j) = f(i-1, j-1)+1, if A[i] == A[j]
14 f(i, j) = max{f(i-1, j), f(i, j-1)}, otherwise
15
16 :param A: str
17 :param B: str
18 :return: The length of longest common subsequence of A and B.
19 """
20 m = len(A)
21 n = len(B)
22 f = [[0 for _ in xrange(n+1)] for _ in xrange(m+1)]
23
24 if m == 0 or n == 0:
25 return 0
26
27 for i in xrange(1, m+1):
28 for j in xrange(1, n+1):
29 if A[i-1] == B[j-1]:
30 f[i][j] = f[i-1][j-1]+1
31 else:
32 f[i][j] = max(f[i][j-1], f[i-1][j])
33
34 return f[-1][-1]
2def longestPalindromeSubseq(self, s: str) -> int:
3 n = len(s)
4 dp = [[0 for _ in range(n)] for _ in range(n)]
5 for i in range(n - 1, -1, -1):
6 dp[i][i] = 1
7 for j in range(i + 1, n):
8 if s[i] == s[j]:
9 dp[i][j] = dp[i + 1][j - 1] + 2
10 else:
11 dp[i][j] = max(dp[i + 1][j], dp[i][j - 1])
12 return dp[0][n - 1]
6def longestConsecutive(self, nums):
7 """
8 :type nums: List[int]
9 :rtype: int
10 """
11 visited = set()
12 for num in nums:
13 visited.add(num)
14
15 max_len = 0
16 for num in nums:
17 if num - 1 not in visited:
18 curr = 0
19 item = num
20 while item in visited:
21 curr += 1
22 item += 1
23 max_len = max(max_len, curr)
24 return max_len
37def longestCommonPrefix(self, array):
38 if array is None or len(array) == 0:
39 return ''
40
41 if len(array) == 1:
42 return array[0]
43
44 half = len(array) / 2
45 longest_left = self.longestCommonPrefix(array[:half])
46 longest_right = self.longestCommonPrefix(array[half:])
47
48 min_length = min(len(longest_left), len(longest_right))
49 for index in xrange(min_length):
50 if longest_left[index] != longest_right[index]:
51 return longest_left[:index]
52
53 return longest_left[:min_length]
47def longestConsecutive(self, nums):
48 """
49 :type nums: list[int]
50 :rtype: int
51 """
52 ans = 0
53
54 if not nums:
55 return ans
56
57 nums.sort()
58
59 size = 1
60
61 for i in range(1, len(nums)):
62 if nums[i] == nums[i - 1]:
63 continue
64
65 if nums[i] == nums[i - 1] + 1:
66 size += 1
67 else:
68 size = 1
69
70 if size > ans:
71 ans = size
72
73 return ans if ans > 0 else size
2def longestCommonPrefix(self, strs):
3 """
4 :type strs: List[str]
5 :rtype: str
6 """
7 if len(strs) == 0:
8 return ""
9 i = 0
10 j = 0
11 end = 0
12 while j < len(strs) and i < len(strs[j]):
13 if j == 0:
14 char = strs[j][i]
15 else:
16 if strs[j][i] != char:
17 break
18
19 if j == len(strs) - 1:
20 i += 1
21 j = 0
22 end += 1
23 else:
24 j += 1
25
26 return strs[j][:end]
8def longestCommonPrefix(self, strs):
9 if not strs: return ""
10 l = min(map(len, strs))
11 i = 0
12 while i < l:
13 char = strs[0][i]
14 for s in strs:
15 if s[i] != char:
16 return strs[0][:i]
17
18 i += 1
19
20 return strs[0][:i]
2def lengthOfLongestSubstring(self, s):
3 result = 0
4 left = 0
5 last = {}
6 for i in range(len(s)):
7 if s[i] in last and left <= last[s[i]]:
8 left = last[s[i]] + 1
9 last[s[i]] = i
10 result = max(result, i - left + 1)
11 return result
57def lengthOfLongestSubstring(self, s):
58 # https://leetcode.com/articles/longest-substring-without-repeating-characters/
59 charMap = {}
60 for i in range(256):
61 charMap[i] = -1
62 ls = len(s)
63 i = max_len = 0
64 for j in range(ls):
65 # Note that when charMap[ord(s[j])] >= i, it means that there are
66 # duplicate character in current i,j. So we need to update i.
67 if charMap[ord(s[j])] >= i:
68 i = charMap[ord(s[j])] + 1
69 charMap[ord(s[j])] = j
70 max_len = max(max_len, j - i + 1)
71 return max_len

Related snippets