forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
104 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
|
||
def longest_increasing_subsequence(sequence): | ||
""" | ||
Dynamic Programming Algorithm for | ||
counting the length of longest increasing subsequence | ||
type sequence: List[int] | ||
""" | ||
length = len(sequence) | ||
counts = [1 for _ in range(length)] | ||
for i in range(1, length): | ||
for j in range(0, i): | ||
if sequence[i] > sequence[j]: | ||
counts[i] = max(counts[i], counts[j] + 1) | ||
print(counts) | ||
return max(counts) | ||
|
||
|
||
sequence = [1, 101, 10, 2, 3, 100, 4, 6, 2] | ||
print("sequence: ", sequence) | ||
print("output: ", longest_increasing_subsequence(sequence)) | ||
print("answer: ", 5) | ||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
def license_number(key, K): | ||
res, alnum = [], [] | ||
for char in key: | ||
if char != "-": | ||
alnum.append(char) | ||
for i, char in enumerate(reversed(alnum)): | ||
res.append(char) | ||
if (i+1) % K == 0 and i != len(alnum)-1: | ||
res.append("-") | ||
return "".join(res[::-1]) | ||
|
||
|
||
print(license_number("a-bc-dfd-df", 1), 1) | ||
print(license_number("a-bc-dfd-df", 2), 2) | ||
print(license_number("a-bc-dfd-df", 3), 3) | ||
print(license_number("a-bc-dfd-df", 4), 4) | ||
print(license_number("a-bc-dfd-df", 5), 5) | ||
|
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ def reverse(array): | |
def reverseWords(array): | ||
reverse(array) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
class Node(): | ||
def __init__(self, val = 0): | ||
self.val = val | ||
self.left = None | ||
self.right = None | ||
|
||
def bintree2list(root): | ||
""" | ||
type root: root class | ||
""" | ||
if not root: | ||
return root | ||
root = bintree2list_util(root) | ||
while root.left: | ||
root = root.left | ||
return root | ||
|
||
def bintree2list_util(root): | ||
if not root: | ||
return root | ||
if root.left: | ||
left = bintree2list_util(root.left) | ||
while left.right: | ||
left = left.right | ||
left.right = root | ||
root.left = left | ||
if root.right: | ||
right = bintree2list_util(root.right) | ||
while right.left: | ||
right = right.left | ||
right.left = root | ||
root.right = right | ||
return root | ||
|
||
def print_tree(root): | ||
while root: | ||
print(root.val) | ||
root = root.right | ||
|
||
tree = Node(10) | ||
tree.left = Node(12) | ||
tree.right = Node(15) | ||
tree.left.left = Node(25) | ||
tree.left.left.right = Node(100) | ||
tree.left.right = Node(30) | ||
tree.right.left = Node(36) | ||
|
||
head = bintree2list(tree) | ||
print_tree(head) |
File renamed without changes.