Skip to content

Commit 1d5c6bc

Browse files
committed
update
1 parent bb61156 commit 1d5c6bc

File tree

309 files changed

+13793
-230
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

309 files changed

+13793
-230
lines changed

.DS_Store

6 KB
Binary file not shown.

CLRS/.DS_Store

2 KB
Binary file not shown.

CLRS/LCS.py

+35-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,35 @@
1-
def findLCS(A, n, B, m):
2-
# write code here
3-
c = [([0] * (m + 1)) for _ in xrange(n + 1)]
4-
for i in xrange(n):
5-
for j in xrange(m):
6-
if A[i] == B[j]:
7-
c[i + 1][j + 1] = c[i][j] + 1
8-
elif c[i][j + 1] > c[i + 1][j]:
9-
c[i + 1][j + 1] = c[i][j + 1]
10-
else:
11-
c[i + 1][j + 1] = c[i + 1][j]
12-
return c[n][m]
13-
14-
15-
print findLCS("1A2C3D4B56",10,"B1D23CA45B6A",12)
1+
# coding=utf-8
2+
3+
# 最大公共子串,CLRS思路
4+
5+
# def findLCS(A, n, B, m):
6+
# # write code here
7+
# c = [([0] * (m + 1)) for _ in xrange(n + 1)]
8+
# for i in xrange(n):
9+
# for j in xrange(m):
10+
# if A[i] == B[j]:
11+
# c[i + 1][j + 1] = c[i][j] + 1
12+
# elif c[i][j + 1] > c[i + 1][j]:
13+
# c[i + 1][j + 1] = c[i][j + 1]
14+
# else:
15+
# c[i + 1][j + 1] = c[i + 1][j]
16+
# return c[n][m]
17+
#
18+
#
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
# print findLCS("1A2C3D4B56",10,"B1D23CA45B6A",12)
35+
print findLCS("1A2C3D4B56","B1D23CA45B6A")

CLRS/LIS.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def LIS(self, nums):
2+
"""
3+
:type nums: List[int]
4+
:rtype: int
5+
"""
6+
n = len(nums)
7+
res = [0] * n
8+
for s in xrange(n):
9+
q = 0
10+
for k in xrange(s):
11+
if nums[k] < nums[s]:
12+
q = max(q, res[k])
13+
res[s] += (q + 1)
14+
return max(res) if n <> 0 else 0

CLRS/RB-STD.txt

-113
Original file line numberDiff line numberDiff line change
@@ -1,113 +0,0 @@
1-
LF-RT(T, x)
2-
y = x.right
3-
x.right = y.left
4-
if y.left != T.nil
5-
y.left.parent = x
6-
y.parent = x.parent
7-
if x.parent == T.nil
8-
T.root = y
9-
elif x.parent.right = x
10-
x.parent.right = y
11-
else
12-
x.parent.left = y
13-
y.left = x
14-
x.parent = y
15-
16-
RB-TRANSPLANT(T, u, v)
17-
if u.parent == T.nil
18-
T.root = v
19-
elif u.parent.right == u
20-
u.parent.right = v
21-
else
22-
u.parent.left = v
23-
v.parent = u.parent
24-
25-
RB-INSERT(T, z)
26-
x = T.root
27-
y = T.nil
28-
while x != T.nil
29-
y = x
30-
if x.val < z.val
31-
x = x.right
32-
else
33-
x = x.left
34-
z.parent = y.parent
35-
if y == T.nil
36-
T.root = z
37-
elif y.parent.right == y
38-
y.parent.right = z
39-
else
40-
y.parent.left = z
41-
z.left = T.nil
42-
z.right = T.nil
43-
z.color = RED
44-
RB-INSERT-FIX(T, z)
45-
46-
RB-INSERT-FIX(T, z)
47-
while z.parent.color == RED
48-
if z.parent = z.parent.parent.left
49-
y = z.parent.parent.right
50-
if y.color == RED
51-
y.color = BLACK
52-
z.parent.color = BLACK
53-
z.parent.parent.color = RED
54-
z = z.parent.parent
55-
elif z = z.parent.right
56-
z = z.parent
57-
LF-RT(T, z)
58-
z.parent.color = BLACK
59-
z.parent.parent.color = RED
60-
RT-RT(T, z.parent.parent)
61-
else (change left and right)
62-
T.root.color = BLACK
63-
64-
RB-DELETE(T, z)
65-
y = z
66-
y-ori-color = z.color
67-
if z.right == T.nil
68-
x = z.right
69-
TRANSPLANT(T, z, z.right)
70-
elif z.left == T.nil
71-
x = z.left
72-
TRANSPLANT(T, z, z.left)
73-
else
74-
y = TREE-MIN(T, z.right)
75-
y-ori-color = y.color
76-
x = y.right
77-
if y.parent == z
78-
x.parent = y
79-
else
80-
TRANSPLANT(T, y, y.right)
81-
y.right = z.right
82-
y.right.parent = y
83-
TRANSPLANT(T, z, y)
84-
y.left = z.left
85-
y.left.parent = y
86-
y.color = z.color
87-
if y-ori-color == BLACK
88-
RB-DELETE-FIX(T, x)
89-
90-
RB-DELETE-FIX(T, x)
91-
while T.root != x and x.color == BLACK
92-
if x == x.parent.left
93-
w = x.parent.right
94-
if w.color == RED
95-
w.color = BLACK
96-
x.parent.color = RED
97-
LF.RT(x.parent)
98-
w = x.parent.right
99-
if w.left.color == BLACK and w.right.color == BLACK
100-
w.color = RED
101-
x = x.parent
102-
elif w.right.color == BLACK
103-
w.color = RED
104-
w.left.color = BLACK
105-
RT-RT(T, w)
106-
w = x.parent.right
107-
w.color = x.parent.color
108-
w.right.color = BLACK
109-
x.parent.color = BLACK
110-
LF-RT(T, x.parent)
111-
T.root = x
112-
else(change left and right)
113-
x.color = BLACK

CLRS/scratch.java

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
// Java Program to show segment tree operations like construction,
2+
// query and update
3+
class SegmentTree
4+
{
5+
int st[]; // The array that stores segment tree nodes
6+
7+
/* Constructor to construct segment tree from given array. This
8+
constructor allocates memory for segment tree and calls
9+
constructSTUtil() to fill the allocated memory */
10+
SegmentTree(int arr[], int n)
11+
{
12+
// Allocate memory for segment tree
13+
//Height of segment tree
14+
int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));
15+
16+
//Maximum size of segment tree
17+
int max_size = 2 * (int) Math.pow(2, x) - 1;
18+
19+
st = new int[max_size]; // Memory allocation
20+
21+
constructSTUtil(arr, 0, n - 1, 0);
22+
}
23+
24+
// A utility function to get the middle index from corner indexes.
25+
int getMid(int s, int e) {
26+
return s + (e - s) / 2;
27+
}
28+
29+
/* A recursive function to get the sum of values in given range
30+
of the array. The following are parameters for this function.
31+
32+
st --> Pointer to segment tree
33+
si --> Index of current node in the segment tree. Initially
34+
0 is passed as root is always at index 0
35+
ss & se --> Starting and ending indexes of the segment represented
36+
by current node, i.e., st[si]
37+
qs & qe --> Starting and ending indexes of query range */
38+
int getSumUtil(int ss, int se, int qs, int qe, int si)
39+
{
40+
// If segment of this node is a part of given range, then return
41+
// the sum of the segment
42+
if (qs <= ss && qe >= se)
43+
return st[si];
44+
45+
// If segment of this node is outside the given range
46+
if (se < qs || ss > qe)
47+
return 0;
48+
49+
// If a part of this segment overlaps with the given range
50+
int mid = getMid(ss, se);
51+
return getSumUtil(ss, mid, qs, qe, 2 * si + 1) +
52+
getSumUtil(mid + 1, se, qs, qe, 2 * si + 2);
53+
}
54+
55+
/* A recursive function to update the nodes which have the given
56+
index in their range. The following are parameters
57+
st, si, ss and se are same as getSumUtil()
58+
i --> index of the element to be updated. This index is in
59+
input array.
60+
diff --> Value to be added to all nodes which have i in range */
61+
void updateValueUtil(int ss, int se, int i, int diff, int si)
62+
{
63+
// Base Case: If the input index lies outside the range of
64+
// this segment
65+
if (i < ss || i > se)
66+
return;
67+
68+
// If the input index is in range of this node, then update the
69+
// value of the node and its children
70+
st[si] = st[si] + diff;
71+
if (se != ss) {
72+
int mid = getMid(ss, se);
73+
updateValueUtil(ss, mid, i, diff, 2 * si + 1);
74+
updateValueUtil(mid + 1, se, i, diff, 2 * si + 2);
75+
}
76+
}
77+
78+
// The function to update a value in input array and segment tree.
79+
// It uses updateValueUtil() to update the value in segment tree
80+
void updateValue(int arr[], int n, int i, int new_val)
81+
{
82+
// Check for erroneous input index
83+
if (i < 0 || i > n - 1) {
84+
System.out.println("Invalid Input");
85+
return;
86+
}
87+
88+
// Get the difference between new value and old value
89+
int diff = new_val - arr[i];
90+
91+
// Update the value in array
92+
arr[i] = new_val;
93+
94+
// Update the values of nodes in segment tree
95+
updateValueUtil(0, n - 1, i, diff, 0);
96+
}
97+
98+
// Return sum of elements in range from index qs (quey start) to
99+
// qe (query end). It mainly uses getSumUtil()
100+
int getSum(int n, int qs, int qe)
101+
{
102+
// Check for erroneous input values
103+
if (qs < 0 || qe > n - 1 || qs > qe) {
104+
System.out.println("Invalid Input");
105+
return -1;
106+
}
107+
return getSumUtil(0, n - 1, qs, qe, 0);
108+
}
109+
110+
// A recursive function that constructs Segment Tree for array[ss..se].
111+
// si is index of current node in segment tree st
112+
int constructSTUtil(int arr[], int ss, int se, int si)
113+
{
114+
// If there is one element in array, store it in current node of
115+
// segment tree and return
116+
if (ss == se) {
117+
st[si] = arr[ss];
118+
return arr[ss];
119+
}
120+
121+
// If there are more than one elements, then recur for left and
122+
// right subtrees and store the sum of values in this node
123+
int mid = getMid(ss, se);
124+
st[si] = constructSTUtil(arr, ss, mid, si * 2 + 1) +
125+
constructSTUtil(arr, mid + 1, se, si * 2 + 2);
126+
return st[si];
127+
}
128+
129+
// Driver program to test above functions
130+
public static void main(String args[])
131+
{
132+
int arr[] = {1, 3, 5, 7, 9, 11};
133+
int n = arr.length;
134+
SegmentTree tree = new SegmentTree(arr, n);
135+
136+
// Build segment tree from given array
137+
138+
// Print sum of values in array from index 1 to 3
139+
System.out.println("Sum of values in given range = " +
140+
tree.getSum(n, 1, 3));
141+
142+
// Update: set arr[1] = 10 and update corresponding segment
143+
// tree nodes
144+
tree.updateValue(arr, n, 1, 10);
145+
146+
// Find sum after the value is updated
147+
System.out.println("Updated sum of values in given range = " +
148+
tree.getSum(n, 1, 3));
149+
}
150+
}
151+
//This code is contributed by Ankur Narain Verma

CLRS/scratch.py

Whitespace-only changes.

0 commit comments

Comments
 (0)