Você está na página 1de 18

Problem & page numbers may differ slightly across versions.

Use the Table of Contents available here http://bit.ly/14BWO13 to cross reference.


Version v1.0.0 Chapter 5 Reviewer THL Location 56 Description Last line of paragraph 4: Nodes D, E, H, M, and P are the leaves of the tree => Nodes D, E, H, M, N, and P are the leaves of the tree. Last line of paragraph 5: A complete binary tree on n nodes has height ceil(log n) => A complete binary tree on n nodes has height ceil(lg n). 2.7 Line 2: 3x100+1x100+4x1=>3x100+1x10+4x1 Problem 7.12, Line 3: Your algorithms time => Your algorithm's time Para. 2 Line3: heapsort is in-place but not; => heapsort is in-place but not stable; Para. 2, Line7: returns 1 => returns -1 Para. 1, Line 5: NP-cmoplete => NP-hard Solution 17.4, Line last 2: 1 1/2 => 1(1/2) Solution 17.10: The function use {0, 1, ..., n-1}. But in the solution, Xi corresponds to {1, 2, ..., n} Category Discussions Fixed typo Fixed typo typo typo typo Fixed Fixed Fixed

v1.0.0 v1.0.0 v1.0.0 v1.0.0 v1.0.0 v1.0.0 v1.0.0 v1.0.0 v1.0.0 v1.0.0 v1.0.0 v1.0.0 v1.0.0 v1.0.0 v1.0.0

6 2 7 10 10 14 17 17 18 2 3 4 5 5 5

THL CYH CYH CYH CYH CYH CYH CYH CYH CYH CYH CYH CYH CYH CYH

56 33 65 81 81 123 406 411 439 35 179 203 53 53 220

v1.0.0

CYH

222

v1.0.0

CYH

222

v1.0.0 v1.0.0 v1.0.0 v1.0.0 v1.0.0 v1.0.0 v1.0.0 v1.0.0 v1.0.0

5 6 6 6 6 7 7 7 11

CYH CYH CYH CYH CYH CYH CYH CYH CYH

222 56 56 58 234 244 246 246 92

v1.0.0

11

CYH

311

v1.0.0 v1.0.0 v1.0.3 v1.0.3

9 13 1 2

CYH CYH DBH DBH

77 359 15 169

v1.0.3

DBH

181

v1.0.3

DBH

172

Suggestion Modified as this suggestion Suggestion Modified as this suggestion Suggestion Modified as this suggestion Made this change in several places. One Suggestion hiccup is that the X_i is now the i+1-th RV in the sequence, but I've worked with that. Solution 18.23: Line last 6: from s By => from s. By Fixed typo 2.10 Line 2: a mod b = 0. => b mod a = 0. Fixed typo Solution 3.3: P.179, Para. 2: I think brute-force algorithm Fixed by changing from O(n^5) to O(n^4) for(1) should be O(n^4) rather than O(n^5) because of Question and removing the trailing part. only 4 variables, i.e., i0, j0, i1 and j1. Solution 4.4, Line last 2: return either of a1 or a2. => Fixed typo return either a1 or a2. 5.8, Para. 2, last line: level(n) = 0 => level(root) = 0 Fixed typo 5.8, Para. 2, last line: for each descendant => for each Fixed typo child Solution 5.11, Para.4, Line 3: we have not done any Modified as this suggestion dequeues so far, => we have not done any dequeues so Suggestion far or all elements in B have been popped, Solution 5.13, Para. 4, Line 3: A[i] < A[j], A[i] will never Stated that we will delete elements where i be a maximum element => A[i] <= A[j], A[j] will be the < j and A[i] <= A[j] maximum element (In Line 3, we see remove all i-th Suggestion element from tail of deque with A[i] < A[j]. In line 5, we see remove till we reach i' such that A[j] < A[i']. We don't see what happen when A[i] == A[j]) Solution 5.13, Para. 4, Line 5: removed from head of the Fixed queue => removed from tail of the deque (In Para.5 Line typo last 2: we could see "delete starting from the tail of the deque.) Solution 5.13, Para. 5, Line 6: removing elements in the Fixed typo head till => removing elements in the tail till Line last 8: 2^h - 1 nodes, of which 2^(h-1) are leaves Fixed typo => 2^(h+1) -1 nodes, of which 2^h are leaves Line last 8 ~ 7: A complete binary tree on n nodes has Fixed height Ceil(logn) => A complete binary tree on n nodes typo has height floor(logn) Problem 6.4, Line 4: isLock(), isLock() => isLock(), lock() Fixed typo Solution 6.12, Code Line 15: // found a and b in the Modified as this suggestion Suggestion subtrees => // found a and b in different subtrees. Solution 7.8, Line 1: There two => There exists two Fixed typo Solution 7.10, Line 2: S[j] = => S[j] >= Fixed typo Solution 7.10, Proof: Confused to see Y[a, b] and Y[b, a] Fixed interchangeably. In the first paragraph, Y[b, a] means Suggestion server a processes job b. In second paragraph, Y[a, b] means server b process job a. Problem 11.15: In second line, <l,r,c> => <l, r, c, h> Fixed typo Solution 11.16: In the first paragraph, solution 11.4 is Yes, it should be the modification of 11.5 applied to find the first node whose value is equal to L. instead of 11.5 itself. Then, solution 11.5 is applied to find the first node Question whose value is greater than L. According to problem definition of 11.5, if solution 11.4 returns NULL, then so does 11.5. Problem 9.5: Solve Problem 9.5 => Solve Problem 9.4 Fixed typo In the first line, BFS and DFS also have an O(V^3) time Mention that it will be more efficient in complexity for dense graphs - the all pairs shortest path Suggestion practice. algorithm is not more efficient. second from last line: "one-to-one ONTO fashion", done typo "onto" probably should be removed. Solution 2.5 code line 6: I think this line can also be Agree, we have modifiy the code and text suggestion written as: int tar = log2(x&(~(x-1))); about that also. In the code of solution 3.5, I think this line should be Fixed. added between line 18 and line 19: "i += max_len - 1;" bug because if is_skippable is false, i should be moved forward to where new search should start. Solution 2.9 second line: "Each encoded number starts Fixed. with as many 0s...". According to the problem bug description, the number of 0s should be the number of bits in the number minus 1.

Problem & page numbers may differ slightly across versions. Use the Table of Contents available here http://bit.ly/14BWO13 to cross reference.
Version v1.0.3 Chapter 3 Reviewer DBH Location 176 Description Last line on page 176: top: stored in subarray A[larger:|A| - 1] "larger" should be "larger+1". Solution 3.7, last sentence in first paragraph: (If i_k = i_k+1, R_k is empty.) I think R_k should be R_k+1. Solution 3.9 frist sentence in 5th paragraph: "One way to DO perform...", "do" is redundant. Solution 3.11: second line on that page: l must exist since p[k] > p[k+1]... ">" should be changed into "<". second paragraph, second from last sentence: "a variable to track many elements..." probably should be: "a variable to track HOW many elements". Solution 3.12: first part of the proof, in the equation that shows contradiction, should l_0 - 1 be changed into l_0? Because we are assuming l_0 * i % n = (r * i) % n. Solution 3.12, 4th paragraph: "Let THE g ...", THE is redundant Solution 3.14: there is a bug in the first algorithm in java. If the input matrix is N*N and N is a odd number, the element that is in the very center won't be printed out. Variant 2 of 3.14: I don't get it. Suppose the number of elements in input sequence is a square number, then A should always exist. Is that right? Variant 3 of 3.14, second line, "starting from (0, 0) followed by (1,0)'", is (1,0) supposed to (0, 1)? Both algorithms in solution 3.15 use a processed[][] array to keep track of positions that have been visited. But since we are modifying the input matrix, if we keep track of the original color of input position, then change the current position's color before processing next positions, we can remove processed[][] and improve space complexity. Solution 4.2: the implementation code uses one pass to check whther all nodes have identical values and count the total number of nodes, then uses 2nd pass to identify the start of input linked list. These two passes can be merged into one pass to improve the effiency. Solution 4.6, the two "while" loops in code lines 29-36 can be replaced by two "if" sentences: if (F) add_node_last<T>(sorted_head, sorted_tail, F); if (L) add_node_last<T>(sorted_head, sorted_tail, L); Category Discussions Fixed. bug Fixed. bug typo typo Fixed. typo Fixed. bug typo bug Clarified. comments typo Fixed. Yes, it can greatly reduce the space complexity. comments Fixed. Fixed. Remove "do" from the sentence. Fixed.

v1.0.3 v1.0.3 v1.0.3

3 3 3

DBH DBH DBH

182 184 186

v1.0.3

DBH

187

v1.0.3 v1.0.3 v1.0.3

3 3 3

DBH DBH DBH

188 188 190

v1.0.3 v1.0.3

3 3

DBH DBH

191 191

v1.0.3

DBH

Fixed. suggestion

v1.0.3

DBH

202

It does not seem right because F or L may have multiple nodes left. There is an already updated code at our soultion download page, and you can double-check on that. suggestion DBH: No, it doesn't matter how many nodes are left in F or L. Because once we have connected the first node left in F or L, all the other nodes after it will be connected too, since they were connected originally. You are right, fixed. Fixed. bug

v1.0.3

DBH

205

v1.1

DBH

v1.0.3

DBH

222

Solution 5.2, online C++ code has a bug. RPN is equivalent to post-order traversal of expression tree. So lines 20 and 22 should be swapped. The top element is y, the next top element is x. In test cases, "2, -10, /" should return 0, "-10, 2, /" should return -5. The java code on the book v1.0.3 has same bug. Solution 5.13, 4th paragraph line 2: "i < j and A[j] <= A[i] ...", I think it should be A[j] >=A[i]. In the next line: "such that A[i] < A[j] ...", I think it should be A[i] <= A[j]. Solution 5.13: I am confused because the latest online code seems to be the solution to Variant 2.

updated solution fixes this problem typo?

v1.0.3

DBH

v1.0.3

DBH

223

you are correct, v-1.1 solves the variant since that's more interesting, given our new solution to the preceding queue with max problem. specifically, in v-1.0 we used a reduction of queue with max to stack with question max and queue from two stacks, later realized it can be done directly using the idea of the deque/efficient frontier. to make the maximum of the sliding window more interesting we added the delta time differences to it. Solution 6.1 2nd paragraph: Strictly speaking, when the I have changed the get_height() function to parameter of the function height() is not NULL, it actually return -1 for NULL node and use -2 to returns either -1 (if subtree is unbalanced), or the represent the unbalanced situation. It subtree's height + 1 (if the subtree is balanced), clarification should fits the defintion more althoguh a because according to the definition of "height" on null node is kind of undefined behavior textbook, a binary tree that has only one node has according to the definition. height 0, not 1.

Problem & page numbers may differ slightly across versions. Use the Table of Contents available here http://bit.ly/14BWO13 to cross reference.
Version v1.0.3 Chapter 6 Reviewer DBH Location 229 Description Solution 6.7: In both programs, line #5 can be removed, and return nullptr at the end. Solution 7.3: I don't really follow the logic in proof. It says "Now by hypothesis, it must be that B[j']-A[i] > m' or C[k']-A[i] >m'...". In my opinion, it should be |B[j']-A[i]| > m' or |C[k']-A[i]| >m'. When we start to consider the absolute values, the arguments that follow are not valid any more. Solution 7.3: In the paragraph after proof, it says the time complexity is O(nlogk), but in the code, to get the maximum difference of k elements, the function distance() checks all pairs of k elements, which is O (k^2), so the time complexity becomes O(n logk + k^2)). In my opinion, to get the max difference, we can scan k elements once and find the max and min, then the time complexity will be O(n logk + k) = O(n log k). Solution 7.8: I think code lines #35-41 can be simply replaced by: vector<T> res(A.begin(), A.begin() + k); because std::nth_element() already puts the first n element at the beginning of the vector. Solution 7.9: the code is buggy. Consider the input sequence { 3, 4, 1}. For 3 and 4, the outputs are right, but when 1 is inserted, it will be put in heap H, then returned as the median, which is incorrect. For any input element, we need to check whether it is between the top elements in two heaps, or less than top element of L, or larger than top element in H, then decide which heap it should be put in, instead of always putting it in H first. 7.10: The description before problem is confusing. The 2nd line says "of work that takes w CPU cycles.", but w is not used at all. On 3rd line, "exist present between jobs", "present" seems to be a redundant word. 4th line says "server j runs S[j] CPU cycles in a unit time". It took me a while to figure out exactly what this problem is about. To make it consistent, I think "that takes w CPU cycles" on line 2 should be removed, then 4th line changed to "server j runs S[j] jobs in a unit time". Solution 7.10: 1st paragraph, line 1: "Let X be an nxm Boolean 2D array", according to the problem description and sigma formula on the next line, "nxm" should be "mxn", since we have m tasks and n servers. Second program that shows overflow of U - L: when we have char A[N]; then char* B = (A - 1500000000); Since A points to A[0], isn't B already out of boundary? In this line: int result = binsearch(B + 1500000000, key, L, U); Is B supposed to be A? Solution 8.2 first paragraph. I think the sentence on third line should be changed to: "If k is not found, binary search will end up pointing to either the FIRST value after k in the array, ..., or the LAST VALUE BEFORE K, in which case the next element, if exists, is the VALUE THAT WE ARE LOOKING FOR." Solution 8.4: Second line in first paragraph: "lookup k - x in the hash table", change "lookup" into "look up". Solution 8.4 last sentence (before variant problem): If the array is sorted on elements (and not absolute values), we can simply use one head pointer and one tail pointer to find the pair (which is mentioned earlier), the time complexity is O(n) and space complexity is O (1). Why bother binary search? 8.2: Line 1: "Sometime we want ...", change "Sometime" to "Sometimes". Problem 8.18 line 2: "Return -1 (IF) no such index exists". Solution 8.8, 3rd paragraph, line 2: "..., so we can update l to x..." I think x should be x+1. Solution 8.9, 3rd paragragh, line 3: "Since x < 1.0 => x^2 < x," It probably makes more sense if change to: "If x < 1.0, since x^2 < x," Solution 8.16, code lines 8-11 can be replaced by oneliner: int differ_bit = miss_XOR_dup & (~(miss_XOR_dup 1)); suggestion Category Discussions Fixed. Fixed question

v1.0.3

DBH

239

Fixed.

v1.0.3

DBH

239

bug

Fixed, in fact, I replace line #35~42 as "return {A.begin(), A.begin() + k};" comments

v1.0.3

DBH

245

Fixed.

v1.0.3

DBH

246

bug

Reworded as per suggestion

v1.1.3

DBH

66

suggestion

agreed, fixed typo line 3 should be "char *B = (A + 1500000000); and line 11 should be int result = binsearch(B, key, L, U);

v1.1.3

DBH

254

v1.1.3

DBH

question

Fixed.

v1.1.3

DBH

259

suggestion

v1.1.3

DBH

262

typo

Fixed. Fixed

v1.1.3

DBH

262

comments

v1.1.3 v1.1.3 v1.1.3

8 8 8

DBH DBH DBH

69

typo typo typo?

Fixed. Fixed. Fixed.

265

Fixed. suggestion Fixed. suggestion

v1.1.3

DBH

266

v1.1.3

DBH

273

Problem & page numbers may differ slightly across versions. Use the Table of Contents available here http://bit.ly/14BWO13 to cross reference.
Version Chapter Reviewer Location Description Category Discussions Problem 9.2: I don't understand why we need 13 bits to Fixed represent 13 different possibilities in each square of chessboard. Isn't log13 (base of 2) already good bug enough? If it is log13, the total number of bits will be 64*log13 (instead of 64*13 = 832). There are a few places that need to be modified. Solution 9.12, 2nd paragraph, line 6, the book should Changed to a single log file, so this issue use another variable name to denote the number of log has gone away suggestion files instead of using "k", because in the original problem, k is the number of most visited pages. Solution 9.16, the first paragraph says "We maintain Agree, fixed. three hashtables", but in the code, L and D are vectors instead of hashtables. Although L and D actually act as suggestion hashmaps, the book may want to explain a little bit more. (I was confused at first.) Solution 9.16 code line 19: This line should be changed Agree, fixed. into: i - L[it->second - 1] + D[it->second - 1]; bug because D[x] maintains the latest distance (but not necessarily the shortest) that covers words Q[0:x]. Lines 2-3 of Hoare's 1962 quotation: "The methods Fixed. In fact, it is "The method compares". compares...", maybe it is "compare" instead of typo Nice catch! "compares"? Figure 10.4: Since there are six rays in the figure, the Fixed. typo text should say six cameras (line 2) and six rays (line 3). Solution 10.1, line 2 of paragraph 1: "... since it has a Fixed run time ..." probably should be changed to: "since it has suggestion an average run time", because in the worst case, qsort takes O(n^2) time. Variant 10.7.1: I know counting sort can achieve O(n), You can't, that's what I was trying to get to. but when ties are to be broken by names, how can we I've reworded it a little bit to make it more question possiblly get O(n)? obvious that you should think about the impossibility Solution 10.13, lines 2-3: "Assume this is the case", I agreed, have fixed think it should be: "Assume this is NOT the case.", because when it is the bug case, we can apply the previous solution directly to solve the current problem. First sentence in 3rd paragraph: "Key lookup, insertion Fixed. and deletion takes time...". I think "takes" should be typo "take", since we are talking about three operations. 11.12 first paragraph, last sentence: "A binary tree ... the agreed, was clearly badly written tree". It probably should be changed into: suggestion "If a binary tree satisfies the BST property, it will be easier to construct the tree." Solution 11.2, to make the code simpler, the last line Fixed. can be replaced by: suggestion return n->paret; Solution 11.3, line 9 of 1st paragraph: "We set the Fixed parent of d to d'; the left child of the parent of d' is set to the right child of d'...". According to the code, the first part should be: suggestion "We set the parent of d' to the parent of d." The second part is not true when d is the parent of d', so the text should be tweaked a little bit.

v1.1.3

DBH

v1.1.3

DBH

285

v1.1.3

DBH

288

v1.1.3

DBH

289

v1.1.3 v1.1.3

10 10

DBH DBH

81 86

v1.1.3

10

DBH

291

v1.1.3

10

DBH

296

v1.1.3

10

DBH

301

v1.1.3

11

DBH

87

v1.1.3

11

DBH

92

v1.1.3

11

DBH

306

v1.1.3

11

DBH

307

Problem & page numbers may differ slightly across versions. Use the Table of Contents available here http://bit.ly/14BWO13 to cross reference.
Version Chapter Reviewer Location Description Problem 10.12: You are given a set of n tasks modeled as closed intervals [ai, bi]. A set S of visit times covers the tasks intersection of [ai, ni] and S is non-empty. Calculate the minimum carnality of S. Given the following inputs: vector<Interval<int>> I; I.push_back(Interval<int>(1, 4)); I.push_back(Interval<int>(2, 8)); I.push_back(Interval<int>(3, 6)); I.push_back(Interval<int>(3, 5)); I.push_back(Interval<int>(7, 10)); I.push_back(Interval<int>(9, 11)); find_min_visit(I); The output is incorrect: 4,5,6,10, 11 The correct output is: 4, 10 The problem in the given solution is how we decide which elements in L need to be deleted. The suggested solution calculates l_end as upper_bound(*R.begin()) which in this case is wrong. v1.0.3 10 RG The correct solution, in my opinion, is the following: vector<int> find_min_visit(const vector<Interval>& I) { set<Interval, LeftCompare> L; set<Interval, RightCompare> R; for(int i=0; i<I.size(); i++) { L.insert(I[i]); R.insert(I[i]); } vector<int> S; while(L.size() && R.size()) { int b = R.begin()->right; S.push_back(b); //set<Interval, LeftCompare>::iterator l_end = L. upper_bound(*R.begin()); set<Interval, LeftCompare>::iterator iter = L.begin(); while(iter != /*l_end*/ L.end() && iter->left <= b) { R.erase(*iter); L.erase(iter++); } } return S; } Solution 11.4, the second program (iterative solution) seems awkward because of the second while loop. Why don't we use the similar approach in solution 11.5? The code will be like this: template <typename T>shared_ptr<BinarySearchTree<T> > search(shared_ptr<BinarySearchTree<T> > r, const T &k) { shared_ptr<BinarySearchTree<T> > target = 0; while (r) { if (r->data < k) r = r->right; else if (r->data > k) r = r->left; else { target = r; r = r->left; } } return target; } Solution 11.6: The last line of code can be simplified to: return search_min_first_BST(r->right, k); Solution 11.10: The two while loops on lines 22-24 and 26-28 can be changed into "if". At the end of merge_BSTs() function, we should call another function to convert the merged list back into a BST instead of returning the merged list. bug Category Discussions Fixed

Fixed.

v1.1.3

11

DBH

310

suggestion

v1.1.3

11

DBH

312

Fixed. In fact, I simplify the last two lines suggestion with return search_min_first_BST(r->left, k) || search_min_first_BST(r->right, k); Fixed. suggestion

v1.1.3

11

DBH

315

Problem & page numbers may differ slightly across versions. Use the Table of Contents available here http://bit.ly/14BWO13 to cross reference.
Version Chapter Reviewer Location Description Solution 11.12: The time complexity in this solution seems to be O(nlgn), because T(n) = 2*T(n/2) + O(n). But O(n) solution is possible. See my online code: Category Discussions I like this idea, which is identical the solution of transform a sorted list to BST. Some text is added and code is updated. suggestion

v1.1.3

11

DBH

317

v1.1.3 v1.1.3 v1.1.3

11 11 11

DBH DBH DBH

323 323

v1.1.3 v1.1.3

11 12

DBH DBH

325 329

v1.1.3

DBH

15

v1.1.3

12

DBH

105

v1.1.3

12

DBH

108

https://docs.google.com/file/d/0BNRWCYRFz6uUE82blktbVp4bk0/edit?usp=sharing Variant 11.16.1: "... when MORE multiple segments ...", Fixed. typo MORE seems to be redundant. The second "Variant 11.16.3" should be 11.16.4. Fixed. typo Problem 11.18 description line 4: "lookup(s) ..., or -1 IS s Good catch. Fixed. typo is ...", the first "IS" should be "if". Solution 11.18: Since client names do not have to be in Fixed. sorted order, why not define credits like this: suggestion unordered_map<string, int> credits; Then lookup() will be O(1) instead of O(logn). Solution 12.2, line 5: "For any (i, j) pair, IF L[i] > R[j], ...". Fixed. typo The greedy approach in the problem of 2n cities on a Good catch, have updated. (meant to say line is not exactly right. For example, suppose we have city before, not white city) white cities at { 0, 5, 7}, and black cities { 2, 3, 6}, first connect 0 and 2; for 5, if we connect it to 6, which is the bug closest one, then 7 will have to be connected with 3, which is not optimal. The correct approach is, after sorting two input arrays W[ ] and B[ ], always connect W [i] with B[i]. Problem 12.18: first line: "an even number (of) coins ..." Fixed typo Probelm 12.23, line 3: k_m-2 shoule be k_m-1 THL: I think you are right. One thing bothers me now is that we mentioned "to the server with the lowestID i such that c ki,or to server m1 if no such i exists.", I think k_{m-1} should be equal to h_{n-1} in this situation. Besides, the indexing in typo solution part is broken for sure since we used the index starts from 1 instead of 0 at there. AA: no, it's the last line of the problem statement that's broken, should be to k_m2. Fixed

v1.1.3

12

DBH

109

v1.1.3

12

DBH

350

Problem 12.27, line 5: "not know (at least) three other suggestion ..." Solution 12.15: I do not understand what the two variant Here is the new wording problems mean. For example, in #1, what is s'? In #2, what does "the lead" mean? \variant{Suppose the final score is given in the form $(s,s')$, i.e., Team~1 scored $s$ points and Team~2 scored $s'$ points. How would you compute the number of distinct scoring sequences which result in this score? For example, if the final score is $(6,3)$ then Team~1 scores $3$, Team $2$ scores $3$, Team $1$ scores $3$ is one scoring sequence which results in this score.} question \variant{Suppose the final score is $(s,s')$. How would you compute the maximum number of times the team that lead could have changed? For example, if $s=10$ and $s'=6$, the lead could have changed $4$ times: Team~1 scores $2$, then Team~2 scores $3$ (lead change), then Team~1 scores $2$ (lead change), then Team~2 scores $3$ (lead change), then Team~1 scores $3$ (lead change) followed by $3$.} AA: i see the problem with our solution now, but rather than fix it, i think we should use your solution. your solution is a little less "streaming" but definitely better time complexity. error DBH: Thanks. I was about to rephrase the words to make myself understood. THL: Part of your algorithm is similar with "Smallest subarray covering set" where using two pointers.

Solution 12.7, 2nd paragraph talks about the "efficient frontier" of candidate starting indexes, but both the formula on line 3 and the code uses the idea of "efficient frontier" of candidate ending indexes. The text should be modifies to make them consistent. v1.1.3 12 DBH 337 This problem can actually be solved in O(n) time by maintaining a stack of starting or ending points. See my code: https://docs.google.com/file/d/0BNRWCYRFz6uT0gyQTZWNmk5dEk/edit?usp=sharing

Problem & page numbers may differ slightly across versions. Use the Table of Contents available here http://bit.ly/14BWO13 to cross reference.
Version Chapter Reviewer Location Description Solution 12.24: code lines 22-23 can be replaced by: tree[right].cap; because when left < tree.size(), since both left and tree. size() are both odd numbers, and right = left + 1, right will be less than tree.size() too. Category Discussions THL: Awesome! It is really a good catch. I think one other reason that right node is always valid here is because we are processing on a complete binary tree. suggestion

v1.1.3

12

DBH

360

v1.1.3

12

DBH

365

v1.1.3

17

DBH

424

v1.1.3

17

DBH

424

DBH: The reason why tree.size() is always odd is because the tournament tree is a complete binary tree. Solution 12.28: 2nd paragragh says that the idea of AA: have reworded to make it clearer that greedily searching for non-diverse vertex v and flipping the algo is the flipping vertices one and its color won't work. But isn't this idea actually equivalent that we focus on edges just to prove its clarification to coloring edges and flipping the color? Coloring the convergence edge only makes it easy to prove that greedy approach is correct, but coloring vertexes will also work, right? Solution 17.8: the formula of Pr(r, n) should be: Good catch. Fixed. Pr(r, n) = Pr(r-1, n - 1) Pn + Pr(r, n - 1) (1 - Pn) since r is the number of wins and n is total number of error elections. To make the formula consistent with DP table in the code, it will be better to write it as: Pr(n, r) = Pr(n - 1, r - 1) Pn + Pr(n - 1, r ) (1 - Pn) Solution 17.8: the code has some bugs. Right after line THL: I don't agree that we should add "if (a 7, we should add: == 0) return 0.0;" because a will decrease by one in both cases, and because the if (a == 0) return 0.0; condition "if (a < b)", it guarantees that no out of index range situation will happen (i. If not, when a is 0, b may be negative and out of index e., b never be negative). Instead, I think we range. should add "if (r < 0) return 0.0" to prevent the out of index range error. And on lines 10 and 11, prob[a] should be changed into bug prob[a-1], because prob[a-1] is the probability that I agree the second one. The fix is in, Republicans will win a-th election. besides, I refactor the code by using r and n instead b and a. The new code is in https://docs.google. com/file/d/0B5znOthff2HSWlFT0hUODlyXzQ/edit?usp=sharing DBH: I think you are right. Thanks. THL: We temporarily remvoe this problem since we cannot find the correct definition of this.

v1.1.3

17

Matthieu

142

v1.1.3 v1.1.3 v1.1.3

15 15 14

Matthieu Matthieu Matthieu

405 398 124

Problem 17.22: I am not sure how the Jacobi symbol is well defined with those 4 properties. In particular, I don't see how it can take the value 0. In the solution, the third if states that if m is zero then the number is 0, I don't see why from the definition... Problem 15.12: In the solution, I don't get the T(u) = max (for 0<=i<=n-1) T(vi) = i ?? Solution 15.3: There is a delay argument in the solution... not sure where that comes from? Problem 14.11: Can the problem define what CNF-SAT means? it is possible to guess but not really obvious.... Solution 8.13: In the first few sentences, I believe in fact we reorder in a way that all elements strictly greater than x are before p

question

typo question question

AA: typo, should have been A(vi)+i +1, thanks for catching it. AA: have added a comment explaining that it's just simulating some actual computation AA: fixed, removed the acronym THL: Good catch, I think you are saying that according to the code, it seems we reorder the elements strictly greater than.

v1.1.3

Matthieu

269

bug

v1.1.3

Matthieu

252

v1.1.3

Matthieu

62

v1.1.3

Matthieu

38

v1.1.3

16

Matthieu

131

Solution 7.8: When mentioning there are 2 algorithms to AA: done find the media in an array in O(n), maybe it's worth suggestion referring to Solution of problem 8.13, since that's one of them... Problem 6.14: Maybe explain what a Trie is in the THL: I see what you mean. Probably we introduction. Also, I believe Trie is more efficient when can change the problem description to that the trie does not change, but we will test various we will have lots of queries incoming, and patterns. The example is more the opposite (different the set D is already given. Good point! sets to match against one pattern). Problem 16.3 I suggestion guess is much better to explain the trie... Another example could be: given a set like {"cat", "dog", "car", "cut", "be"} that does not change, find the shortest unique patterns for "cab", "brag", "cub" Problem 3.7: It was difficult to understand the question. AA: Agreed, have added more text and an question Especially what does "weakest" mean here? example Problem 16.1: I actually had that question during an AA: good point, have added it interview before. I think it's worth mentioning that a lot of this can easily be run in parallel... (cut the original suggestion picture in smaller parts and each can be independently searched for a match in the database)

Problem & page numbers may differ slightly across versions. Use the Table of Contents available here http://bit.ly/14BWO13 to cross reference.
Version Chapter Reviewer Location Description Problem 3.5: I suggested another solution for the case mod 10 on the quora forum. Should still run in O(n) and work even if the table is smaller than k Sorry, it was posted as a comment to a comment (the original post was titles Problem 3.4)... so it's not easy to find. I put the code in another sheet of this doc I found one way that would be O(n) too I believe and can find discontinuous sets. It basically builds a table of the modulo 10 values that can be reached from the previous values in the table. Not the prettiest code in any way (it's in C too) Solution 8.10: I believe the table that's given for the worst case scenarios is misaligned. the (x-1) should be directly to the left of the (x+1), not on the row below (or the matrix would not be square) Solution 8.18: I don't think the last sentence of the solution to problem 8.18 is correct. If all elements are 0, a lot of it depends on k too (which could also be negative)... I am not sure the best case makes much sense anyway, but it would be if abs(a[0]-k) > n then we know there is not such value in the table... no? Category Discussions THL: I think there is a misunderstanding that we are asking for subset sum mod n, where n is exactly the size of array. MJ: I see... did not make the connection between the size of the array and the suggestion modulo. Got it! AA: have called out the connection explicitly

v1.1.3

Matthieu

37

v1.1.3

Matthieu

267

bug

THL: you are right, x-1 should be on the left of the x+1.

THL: I think what you mentioned is more like a boundary case which is contained in the "our algorithm inspects 1/k-th of the array". Because if n < k, n / k < 1 which means we don't need to do anything to check any elements except A[0]. question MJ:Sorry, still not convinced... if k=-2, what does it mean "1/k-th of the array" ? what if k=0? THL: After I checked it, it should be something like 1/abs(A[i] - k)-th array instead of 1/k-th array. (AA: agreed) THL: Awesome! Good catch. Fixed. Added a throw about overflow error. bug AA: fixed typo

v1.1.3

Matthieu

274

v1.1.3 v1.1.3

14 14

Matthieu Matthieu

392 394

v1.1.3

18

Matthieu

148

Solution 14.10: it should say the divide and conquer would look to build x^k and x^(n-k) Solution 14.12: the text mentions we should test for overflow (in case the sequence keeps increasing), but the code does not have that test Problem 18.15: The introduction to the problem says the relation is anti-reflexive (a does not know a), but the problem itself says F[a][a] may be true or false. That's the variant 18.15.1, in this case, it should always be false Solution 13.9: Tested the latest online source code. I think line #53 has to be changed from: min_heap.emplace(s); to: min_heap.emplace(s, 0, 0); (I don't have book version 1.2, so I don't know the exact page number.)

typo

v1.2

13

DBH

typo

THL: We have a new implementation which will use set instead of priority_queue for Dijkstra. New code shall be online soon. In the previous implementation, it is fine (although it is bad use since it will call the default value of the second and third arguments, which is 0). THL: the updated code is at https://docs. google.com/file/d/0B5znOthff2HMVZ4WnV3em4yTkU/edit?usp=sharing AA: thanks for pointing out the issues with spurious notify and the indentation. the notifys on Lines 9 and 39 are not needed have removed them too

Solution 15.5: A couple of suggestions for the code. Line 34 should be in a loop to always check for the condition while(RW.readCount==0) RW.LR.wait(); v1.1.3 15 Matthieu 400 (It's documented in Effective Java and other places, the documentation for wait() explains there can be some "spurious wakeup" Line 25 and following are not indented properly (?) Line 9, 39: why do a notify there? The only place it is waiting is for the readCount to come to 0 Solution 12.20: comment on line #76: NW rectangle => SE comment on line #81: SE rectangle => NW v1.1.3 12 DBH 355 It is probably a good idea to point out that we are assuming that the matrix "grows" from bottom up by rows, and from left to right by columns. Many readers (including me) always assume a matrix grows from top down by rows. 2nd paragraph, line 3: "... may contain of a single vertex". "of" probably should be removed. typo suggestion

AA: added a line about our convention for the matrix growing up by rows

v1.1.3

13

DBH

111

typo

Fixed.

Problem & page numbers may differ slightly across versions. Use the Table of Contents available here http://bit.ly/14BWO13 to cross reference.
Version Chapter Reviewer Location Description Solution 13.11, 1st paragraph, line 3: min(S(a, b), S(a, x) + d(x, y) + S(y, b)) should be changed into: min(S(a, b), S(a, x) + d(x, y) + S(y, b), S(a, y) + d(y, x) + S(x, b) Of course here d(x, y) and d(y, x) are equal. Code line 15: "G[i][j] < G[i][k] + G[k][j]) {" Here '<' should be changed into '>'. Code lines 45 and 49: change '<' to '>' Actually the body in the for loop should be: ...... // Check the path of a => p.x => p.y => b if (G[a][p.x] != numeric_limits<int>::max() && G[p.y][b] != numeric_limits<int>::max() && min_dist_a_b > G[a][p.x] + p.distance + G[p.y][b]) { min_dist_a_b = G[a][p. x] + p.distance + G[p.y][b]; best_proposal = p; } // Check the path of a => p.y => p.x => b if (G[a][p.y] != numeric_limits<int>::max() && G[p.x][b] != numeric_limits<int>::max() && min_dist_a_b > G[a][p.y] + p.distance + G[p.x][b]) { min_dist_a_b = G[a][p.y] + p.distance + G[p.x][b]; best_proposal = p; } ...... Solution 14.5: At the end of solution, the books says: "Furthermore, if d* > , then we can say nothing about the relationship between c* and c_opt." Did you "d* < "? Because according to the first sentence in this paragraph, d* > will yield a lower bound. Solution 14.7, the last paragraph says: "Note that the initial selection of a warehouse is immaterial for the argument to work but heuristically, it is better to choose a central city as a starting point." v1.1.3 14 DBH 387 According to the algorithm, the initial selection of the warehouse is always located in the city for which the cost is minimized, so its selection should be unique. It will never be a city on borders, right? question Category Discussions Good catch! Fixed.

v1.1.3

13

DBH

379

error

Good catch! Fixed.

v1.1.3

13

DBH

error

agreed, fixed typo?

v1.1.3

14

DBH

385

AA: what i am trying to say is that we pick the central most city in the algorithm, but we could have picked any one, including a border city. we end up with O(n^2) complexity this way, maybe we should just pick a random city, since that drives complexity to O(nk)... DBH: Thank you. I got it. AA: fixed AA: good catch, fixed typo?

v1.1.3

14

DBH

388

Solution 14.8, on code line #32, comment says: // start a new row I think it should be "start a new column", because the Sudoku is solved by columns.

Problem & page numbers may differ slightly across versions. Use the Table of Contents available here http://bit.ly/14BWO13 to cross reference.
Version Chapter Reviewer Location Description Category Discussions Solution 14.10: I am not entirely clear how the solution DBH: I believe the solution can be greatly works. When I run the code, it produces the right optimized by using BFS. See my code: solution, but seems quite slow and crashes when run https://docs.google.com/file/d/0Blarger numbers (like x^149 for example). NRWCYRFz6udExIQkwtUVlBRlE/edit? When I try this problem, I am saving all the possible usp=sharing "paths" with the same shortest length to reach one exponent and do that from the bottom up. The source For X^149, my program can generate the code is there: answer in 0.3 second. https://github. com/MatthieuLJ/EPI_java/blob/master/src/com/matthieu/epi/ComputeXPowerN.java I mentioned this optimization when reviewing the manuscript, but was not confirmed by authors. THL: I just remember that. We cannot confirm this BFS method works since we thought this problem is a NP-hard problem which should not exist polynominal-time solution here. Probably we can use DBH's program and the one we have right now to make a test on that. DBH: I don' think the BFS solution conflicts with the NP-hard argument, because in BFS approach, the number of vertexes in the graph seems to be O(n), so the time complexity of BFS is pseudo-polynomial. Matthieu: DBH, in the code you shared, I don't see how it works, the variable path does not get updated at all in the function BFS, there must be something missing...? v1.1.3 14 Matthieu 392 question THL: One thing I am checking DBH's code right now is that it only combines the last (i. e., curr.back()) with any of curr. Basically, it only uses one loop which cannot simulate the effect of getting the square of some previous elements. I am not sure about skipping those can work or not. THL: My intuition tells me that BFS-style program should work, and I am using the original one and DBH's to run test from 2 to 199 to verify the result. There is only one small bug of DBH's program is that it will never end when n = 1 Matthieu: DBH, let me run your code first and go through it again, if I have more question, I'll email you (and cc THL) DBH: Yeah, that is a bug... Thanks for pointing it out. About the square of any previous element, it should have been included in BFS. For example, suppose we have { 1, 2, 3 }, square of 2 is covered by { 1, 2, 4 }, which is another node on the same level as { 1, 2, 3 }. THL: I just implemented a BFS-version program in https://docs.google. com/file/d/0B5znOthff2HQTkxOGt1QTBEems/edit?usp=sharing AA: have reworded solution text Solution 14.12: on code line #9, "test_i != 1" seems to done comments be redundant, because i starts from 2. Solution 14.13: The first sentence in the last paragraph agreed, fixed seems redundant. Because at the end of the paragraph comments "We can develop heuristics ...", the ending sentence is almost identical. Solution 15.10: the end of line 4 on this page, Fixed. comments "(keep) track of ..." The end of line 2: "... project or a ...", "or" probably Fixed. typo should be "for"? Solution 16.4, line #7 says n* m^2 hash table lookups.It Fixed. should be n^2 * m^2, because for a word that has n characters, we can pick up any two characters and change them to any other char in the alphabet. The total typo? number of choices of selecting any two chars is n(n-1)/2, and each char can be changed to (m-1) other chars. So overall it should be n*(n-1) * (m-1)^2 / 2.

v1.1.3

14

DBH

394

v1.1.3

14

DBH

395

v1.1.3 v1.1.3

15 16

DBH DBH

403 131

v1.1.3

16

DBH

409

Problem & page numbers may differ slightly across versions. Use the Table of Contents available here http://bit.ly/14BWO13 to cross reference.
Version Chapter Reviewer Location Description Solution 17.5: I don't get the last sentence on this page. Suppose we have t[0] = 3, t[1] = 5, and p[0] = 0.4, p[1] = 0.6, and the value we get from the random number generator is 0.45, then since 0.45 > p[0], the t value we return should be t[1], right? In my opinion, the last sentence on this page should be changed to: "we return the LEAST t[i] such that Fx(t[i]) >= r." Solution 17.14, second from last paragraph, it is probably better to change the last sentence into: "Hence the final probability is 1 - (...) = 1 - 0.807 = 0.193." because we are supposed to get the probability of making a triangle. Solution 17.15, 3rd paragraph, line 4: I think m/(2m) * (m+1) / (2m) should be changed to: m/(2m) * m/(2m-1), because when n = 2m, first half has m choices to put the second best, and the second half has m choices for the best. Variant 17.20.4, line 1: "a pocked" probably should be changed into "one of the pockets". 17.23, 3rd paragraph, it will be better to change "-26" into "(-26)", so is "-20" on the next line. 2nd paragraph, line 2, "... is needed an aha moment" probably should be changed into: "... is needed IS an aha moment". Solution 18.5, I think comments in code line 1 should be changed to: // Return 0 means choosing even numbers, and return 1 means choosing odd numbers Solution 18.9: the last sentence in the 3rd paragraph probably should be changed into: "(If they live in the same building, that building will be the optimum building.)" Category Discussions AA: DBH is right, have squashed this bug

v1.1.3

17

DBH

421

question

Done suggestion

v1.1.3

17

DBH

429

Agreed, have fixed typo?

v1.1.3

17

DBH

430

v1.1.3 v1.1.3 v1.1.3

17 17 18

DBH DBH DBH

435 143 144

typo suggestion typo

Agreed, have fixed Agreed, have fixed Agreed, have fixed

v1.1.3

18

DBH

440

typo

THL: fixed as //Return 0 means choosing F (even numbered coins), and return 1 means choosing S (odd numbered coins) Agreed, made change

v1.1.3

18

DBH

442

suggestion

v1.0.0(?)

12

PL

v1.1.3

18

DBH

449

In the introduction part of DP, we need to explain what AA: have added text just before the code to return if entries are negative (in particular an empty suggestion for this. subarray is fine) Solution 18.15: It seems to me that this solution has THL: You are right. I thought this before nothing to do with whether the relationship is reflexive or but did not remove that since I forgot that. not. Can we use the same solution to solve variant 18.15.1? AA: have added text to solution, this one is question done! THL: We comment out the variant and remove the constraint of reflexive. Made suggested change

v1.1.3

18

DBH

450

v1.1.3

18

DBH

451

v1.1.3

18

DBH

453

Solution 18.18: 1st paragraph, line #4: "he likes best suggestion (AND) to whom ..." Solution 18.18: In the paragraph "It converges to a state Fixed. ...", line 3: "... must (HAVE) made ..." suggestion Line #4: the last word "everyone" probably should be changed to "every professor". Solution 18.20: in the last paragraph before variant, line Fixed. #7 "cycle that result on ...", "on" probably should be suggestion changed to "from". Figure 21.16: Maybe my book is not printed very well, THL: In (a), the edge from e to d is but in (a), I didn't see the thin edge from e to d. And the missing, and it should not have the edge edge from e to c is not supposed to be there. from e to c. In (b), the thin edge from f to g' is missing. However, I don't think we In (b), I don't see the dotted line between f and g'. should have edge from f to g in (c). In (c), I don't see the edge from f to g. DBH: In (c), there should be a thin edge from f to g, right? bug THL: These is a thin line from f to g on my book. I admit that may be too thin and the color is not strong enough such that it may not be visible easily. We probably will change it with dot lines instead. DBH: I see. Thanks. THL: We use color to diffentiate it now.

v1.1.3

18

DBH

455

Problem & page numbers may differ slightly across versions. Use the Table of Contents available here http://bit.ly/14BWO13 to cross reference.
Version Chapter Reviewer Location Description Variant 18.21.1: Can you elaborate a little more? I have no idea what this problem is talking about. Category Discussions AA: i have reworded, see the new text: A set $S$ of positive integers is said to be {\em division free} if there do not exist distinct elements $x$ and $y$ of $S$ such that $x$ is divisible by $y$. For example $S_1 = \{ 2, 5, 8, 21, 23, 27\}$ is division free, but $S_1 \cup \{ 3\}$ is not, since $21$ is divisible by $3$. How would you compute a maximum cardinality subset of question $\{1,2,\ldots,n\}$ that is division free? For example, if $n=10$ then $T_1 = \{2,3,5,7 \}$ is a division free subset, but it is not maximum cardinality, e.g., $T_2 = \ {4,6,7,9,10\}$ is also division free and has a larger cardinality, as is $T_3 = \ {10,9,8,7,6\}$. (Both $T_2$ and $T_3$ are maximum cardinality division free subsets of $\{1,2,\ldots,10\}$.) fixed typo THL: change the code, still needs to change the description of text about substring. AA: have made change

v1.1.3

18

DBH

455

v1.1.3

18

DBH

459

Solution 18.26, 1st paragraph, last line: "is 1 an index i...", I think "an" should be "on". Question 6.23 This is the model question that I expect to see from the book. I like the problem introduction, problem solution and the testing code! // Case (2) : ends with '$' if (r.front() == '$' && r.size() == 1) { return s.empty(); } Can be replaced by: // Case (2) : ends with '$' if (r == "$") { return s.empty(); }

v1.2.1

DL

206

The usage of for (int i = 0; i <= s.size(); ++i) { should always be replaced by for (string::size_type i = 0; i <= s.size(); ++i) Please refer to http://stackoverflow. com/questions/1181079/stringsize-type-instead-of-int

suggestion

I think it is good that we illustrate what k-th suffix of s means. Here is my understanding but it is not straightforward from the book: r^0 == r r^1 == r.substr(1) etc. "Assume the numbers in L and F appear in sorted order within the lists." 1.2.1 7 DL It is better to indicate that both L and F are either in ascending or descending order. when !r_node, throw exception is a better solution // Find the middle points of L 216 For a list with odd length, it is the middle point. For a list with even length, it is the right-middle point. The code works fine. Just need to clarify the comments. missing 1.2.1 8 DL 227 #include <stdexcept> in normalized_path_names.cpp bug suggestion

fixed: mention all are sorted in ascending order

1.2.1

DL

suggestion THL: Agree, fixed. THL: Agree, added inline comments. suggestion

1.2.1

DL

THL: Fixed.

Problem & page numbers may differ slightly across versions. Use the Table of Contents available here http://bit.ly/14BWO13 to cross reference.
Version Chapter Reviewer Location Description if (s.empty() || s.back() == "..") { s.emplace_back(token); } else { if (s.back() == "/") { throw invalid_argument("Path error"); } s.pop_back(); } Q1> When s is empty, why we should push ".." in? Q2> Why /.. is invalid? ubuntu@ubuntu-VirtualBox:/$ cd / ubuntu@ubuntu-VirtualBox:/$ cd /.. ubuntu@ubuntu-VirtualBox:/$ cd /../ The above commands run fine under linux. Solution 8.9 Q1> The code doesn't match the description of the solution. To me, it looks like we have to print the level but it is not the case. 1.2.1 8 DL 228 Q2> "We use a count variable" Where in the code do you use the count variable? bug Category Discussions THL: A1> it is going up one directory from the current directory. Like "../Document". A2> The path is still reachable since UNIX implements the fallback mechanism to prevent error. However, the pathname itself is invalid if we just consider it. AA: mention at end of problem that unix will keep you in root even if you .. in it question

1.2.1

DL

227

THL: Good catch! The fixes will be only printing keys level by level, and the code should print proper newline symbols after each level.

Problem 9.4 Q1> When the field BinaryTree<T>::numChildrenLocks will exceed one? It seems to me that the field can be either 1 or 0. 1.2.1 9 DL 238 Q2> Based on my understanding, the whole tree can only allow at most one node to be locked at any time. If that is the case, then we neither have to add the locked field for each node nor use integer field numChildrenLocks. question

Added clarification that that siblings can be simultaneously locked - ancestors are not locked, just precluded from being locked

Problem 9.6 why we need to check: while(k && r) and why not while (r) instead. ... find_kth_node_binary_tree( ... ) { if (k <=0) throw ...; while(r) { ... } throw ...; }

THL: Good point, I just change "while (k && r)" to "while (r)" since k should never reaches 0. The new code is at https://docs. google.com/file/d/0B5znOthff2HdlJfSnp3cW5mX2s/edit?usp=sharing

1.2.1

DL

240

question

Problem & page numbers may differ slightly across versions. Use the Table of Contents available here http://bit.ly/14BWO13 to cross reference.
Version Chapter Reviewer Location Problem 9.13: I don't quite get the idea from this question. The author's intention is to exchange space for speed, but I cannot see the result from the code. Q1> there is no definition for d_l and I assume d_l is the LCA of a and b. Q2> Let's assume that d_b >= d_a. For problem 9.12, here is the computation complexity (d_b - d_a) + 2*(d_a - d_l) = d_b + d_a - 2*d_l 1.2.1 9 DL 246 For problem 9.13, again here is the computation complexity 2*(d_b - d_l) = 2*d_b - 2*d_l To me, the complexity of 9.13 is more expensive than that of 9.12 2*d_b - 2*d_l >= d_b + d_a - 2*d_l (because d_b >= d_a) Also, this is not the exact number of steps as indicated by the book. This is the upper bound b/c node a can hit the root first. So what is the point of trading space for time? Line 11, public priority_queue<...> Based on my knowledge, this is not a good practice because Stack is NOT a priority_queue. A better solution is to use either private inheritance or composite method. Problem 10.6, Line 12, Heap operation is more expensive than two if comparison statements! It may be a better option to keep the heap with size of k and manually compare the new elment with that of the top. if x <= top => export x otherwise pop the heap and push x. THL: Use the composition method to rewrite the code. The code is at https: //docs.google.com/file/d/0B5znOthff2Hsuggestion eUE3T0dEWGVPZUk/edit?usp=sharing question Description Category Discussions AA A1> i have added notation explanation for d_1 AA A2> your logic is true *if* we know d_a and d_b, but we don't, and hence need O (h) time to compute it incidentally, i dont think it's possible to keep a height field and maintain it through inserts and delete without adding to the time complexity of adds and deletes

1.2.1

10

DL

251

THL: That is a good point. However, I would like keep what we had right now since it makes code look simpler. I will revisit it later and think more about it. Feel free to discuss this here, and I really appreciate your opinion. AA: i dont get the point, it doesnt seem to buy anything from an asympototic (worstcase) complexity perspective, and even in suggestion practice i doubt if it speeds things up? THL: It will not improve the worst-case time complexity but an operation on heap shall be more expensive than an if-else statement. DL's logic here is try to exchange the if-else statement with the heap operations.

1.2.1

10

DL

254

Problem 10.6, Line 18, 1.2.1 10 DL 254 min_heap.size() should always be replaced by ! min_heap.empty() "Let S be the elements strictly less than p. .. if |S| <k-1, at least k - |S| duplicates of p are present" Why "at least k - |S| duplicates of p are present"? Why we can guarantee that the elements following the position of k-1 are with values of p? 1.2.1 10 DL 255 question suggestion

THL: Good catch! I almost forgot that I read it in Effective STL.

A1> otherwise p could not be the k-th element A2> good point. efficient selection algos reorder the array so that the desired element is put in sorted place (index k-1 in this case), and all elements before are smaller than or equal to it, all element after are greater than or equal to it. (i dont this we required this in the text but the code certainly makes use of it) have added some text about how the selection algo reorders the array so the case analysis is not needed THL: Good catch! Fixed. typo

Problem 10.9 "we add it to H" 1.2.1 10 DL 256 should be "we add it to S"

Problem & page numbers may differ slightly across versions. Use the Table of Contents available here http://bit.ly/14BWO13 to cross reference.
Version 1.2.1 Chapter 10 Reviewer DL Location 257 Description a+b*sqrt(2) = c + d*sqrt(2) iff a = b and c = d Why? why not a == b and b == d? Problem 11.6 "Then we do a conventional binary search for k in the range of [2^(p-1) + 1, 2^p -2]". 1.2.1 11 DL 264 I don't get the idea here. Based on my understanding, the range should be [2^(p-1), 2^p -2] instead. bug Category Discussions THL: I think it is a typo here. Keen observation. question THL: Good catch! Fixed.

Problem 11.7 Question> http://www.cplusplus. com/reference/numeric/partial_sum/?kw=partial_sum y0 = x0 y1 = x0 + x1 y2 = x0 + x1 + x2 y3 = x0 + x1 + x2 + x3 y4 = x0 + x1 + x2 + x3 + x4 ... ... ... The formula for prefix sum Z_k is wrong because Z_k stores the sum of elements from A[0] to A[k].

THL: z_k is not equal to prefix_sum[k] in the code. We use z_k to help us illustrate the idea. That is the reason you see some -1 in the code because the nature of partial_sum(). If there is any better implementation, please feel free to propose it :) question DL: The code works quite well. The difference between the solution illustration and the real implementation causes unnecessary confusion here. THL: That makes sense. I will try to rewrite the definition part.

1.2.1

11

DL

265

Problem 11.12 Code Line 4. 1.2.1 11 DL 270 if (A.size() <= 1) { return {A.front(), A.front() }; } // when A.size() <= 0, we should throw instead. Problem 11.14, What is the reason we select the array of size 2k-1 rather than others (i.e. 2k) bug

THL: We change the problem description that the input is non-empty array.

AA: good point we could use less (e.g., 1.5k) and still achieve O(n) time, but the run time would be higher (since we only discard k/2 elements for 3k/2 effort) if we used 4k storage, we could discard 3k elements for 4k effort. with 2k elements, we remove k elements with 2k effort. clearly more storage leads to faster run times (in the extreme we read all n and do a single selection), so the tradeoff is wrt how much storage we want. i'll put in a line about this tradeoff AA: it's setting all bits to 0 except for the least significant bit that's 1. DL: I saw the results but I don't know why we need to do that based on the description in the solution. THL: We are using the differ_bit to reduce the problem to a subset of the original problem, i.e., Find missing (or duplicate) problem in a set of numbers whose AND with differ_bit is not 0. DL: Frankly, I still don't get it. AA: we are trying to find a bit in which m and t differ. the xor of all numbers in Z_n and A is m^t. any bit that's set to 1 in m^t is a bit in which m and t differ - the computation we are using is a one liner that gives us such a bit

1.2.1

11

DL

272

question

Line 8 What is the purpose of the following line? int differ_bit = miss_XOR_dup & (~(miss_XOR_dup - 1))

1.2.1

11

DL

274

question

Problem & page numbers may differ slightly across versions. Use the Table of Contents available here http://bit.ly/14BWO13 to cross reference.
Version Chapter Reviewer Location Problem 12.14. <<< The code in Book >>> // Keep moving l until it reaches end or count_Q has less |Q| items while (l < static_cast<int>(A.size()) && count_Q.size() == Q.size()) { if (dict.find(A[l]) != dict.end()) { auto it = count_Q.find(A[l]); if (--(it->second) == 0) { count_Q.erase(it); if ((res.first == -1 && res.second == -1) || r - 1 - l < res.second - res.first) { res = {l, r - 1}; } } } ++l; } Description Category Discussions THL: A1> Good catch! It can be simplified by this. A2> This will be hit the very first time when we enter the if (--(it->second) == 0).

1.2.1

12

DL

287

question

Q1> <<< Why not the following code >>> while (l < r && count_Q.size() == Q.size()) { //[DL] change from "l < static_cast<int>(A.size())" to "l < r" .... } Q2> When can the following condition be hit? if (--(it->second) == 0) { count_Q.erase(it); if ((res.first == -1 && res.second == -1) || // <<<<<< When this can be hit? ...) { ... } Problem 13.8 1> The return value doesn't match the question 1.2.1 13 DL 298 2> the last line is not valid " return it - A.cbegin()" since it may be invalidated at the time 3> missing ) in the last paragraph(i.e.(Solution ... Why we compute the multiplier with the following line: int max_multiplier = n / floor(sqrt(norm(p))) + 1; I made a testing case as follows: Given n is 2, In one of the iteration, x is (3+3i) and x*p is -6i. The following line doesn't do anything because the candidates has no element of x*p. 1.2.1 14 DL 327 question question

THL: A1> We will clear the problem more detailedly. A2> Please go to the shared directory to check the update solution, eliminate_duplicate.cpp. A3> Good catch! Fixed.

AA: the point you seem to be making is that the upper_bound max_multiplier is too high in some cases. when we looked more carefully at the code, we realized it's actually off by a factor of \sqrt 2, i.e., it's too low! as a result there are some nonprimes we do not sieve out. e.g., if n = 29, 29 + 28i is not a gaussian prime ((it's 6 +17i)* (2-i) but our program reports it as a prime. the intent in our original program was to be conservative by adding 1 and taking the floor, as you pointed out, that leads to checking more possibilities than needed, however, it is a little-oh(n) addded term and is insignificant as n increaes. however, your insight was good, that there was no explanation of the bound. we will add it (basically want to ignore multiplers x st |xp| > |n+ni| -> ignore x st |x| > \sqrt{2} n/|p|) THL: Is any reason use deque<bool> or bitset<int>? Just curious about this. DL: Because it is wrong to adopt vector<bool> based on its usage on the suggestion code. Refer to Effective STL "avoid to use vector<bool>" THL: Awesome! I should read that book since we use lots of STL. (I only read the effective C++).

I saw the usage of vector<bool> in many different places. I would suggestion that the author should consider to change it to either deque<bool> or bitset<int> 1.2.1 DL

Problem & page numbers may differ slightly across versions. Use the Table of Contents available here http://bit.ly/14BWO13 to cross reference.
Version Chapter Reviewer Location Description template <typename XaxisType, typename ColorType, typename HeightType> class LineSegment { public: XaxisType left, right; // specifies the interval ColorType color; HeightType height; const bool operator<(const LineSegment &that) const { return height < that.height; } }; Question> Is the operator< defined in above code used by the program? Based on my understanding, the LineSegment::operator< is not used by the program at all. Problem 14.21, Why the max function only return one client instead of a full set of clients? 1.2.1 14 DL 331 question Category Discussions THL: You are right, it was used to before for testing. Just removed it.

1.2.1

DL

328

question

THL: I think it is part of design choices. Because considering the situation of tie, it may end up with O(n) time for performing max(). DL: Then you may consider to change the description of the problem. THL: Agree, we will change the problem description to "return any one of that". THL: I think it is possible to solve this problem even if BST contains duplicate, however, it requires a tie-breaker mechanism which eventually can form a total order of elements of BST. DL: The point is that if the BST contains duplicate elements then the description of the solution is not quite right.

Problem 14.22, I assume the solution is based on the fact that the given BST has distinct values. Is that correct?

1.2.1

14

DL

331

question For example, "If v is present, when we reach the first occurrence of v, ..." The found v may not be the first v presented on the BST. THL: It can be right if we meant the first occurrence of v in the total order. It is not so clear I would say that. THL: We cannot find what you mean the duplicate is? Could you elaborate it? DL: In my text, here is what I have seen,

Problem 15.4, The last two paragraphs contain duplicate sentences. 1.2.1 15 DL 118 suggestion

"A common mistake in solving DP problems is trying ..." has been repeated twice. THL: fixed. THL: Agree, fixed.

Solution 15.16, On the bottom of the page: 1.2.1 15 DL 356 As indicated by the comment on Line 6, there is no way to start from (0, 0) if B[0][0] is true. suggestion

1.3.0

16

ZD via quora

401

1.2.1

15

DL

359

I suggestion that the code should immediate return 0 on Line 7 instead of going throug the loops. need to clarify directed/undirected, also why nC2, n^4 AA: have described worst case scenario, clarification overall stated it's undirected Solution 15.20, THL: I think you are right, it should be ((m+1) 2) * ((n+1) 2). Fixed. Why the cahe has no more than ((m+1)x(n+1) 2) entries? This formula even doesn't hold for the case where both m and n are 1. question Given a 1 x 1 grid, the total number of sub-rectangle is 1. I assume I misunderstand the meaning of the formula. FYI: I think the correct answer is ((m+1) 2) * ((n+1) 2).

Problem & page numbers may differ slightly across versions. Use the Table of Contents available here http://bit.ly/14BWO13 to cross reference.
Version 1.2.1 1.3.0 Chapter 15 8 Reviewer DL JJ Location 15.29 Proof, 373 236 Suppose x is not diverse => suppose the vertex x is not diverse ring n-1 should be ring n Solution 16.5 void DFS(GraphVertex* cur, const int &time, vector<GraphVertex*> &contacts) { for (GraphVertex* &next : cur->edges) { if (next->visitTime != time) { // check I next->visitTime = time; contacts.emplace_back(next); DFS(next, time, contacts); } } } void transitive_closure(vector<GraphVertex> &G) { // Build extended contacts for each vertex for (int i = 0; i < G.size(); ++i) { if (G[i].visitTime != i) { // check II G[i].visitTime = i; DFS(&G[i], i, G[i].extendedContacts); } } } Description Category Discussions THL: make sense, fixed. suggestion typo THL: fixed THL: Good catch, it is redundent. I have removed that check.

1.2.1

16

DL

380

question

Question> What is the purpose of check II? Based on my understanding, check I is used to guaranteed that the same contact will not be checked twice. However, I have problems to understand the meaning of check II. In each DFS iteratation, all extened contact of a vertex will be assigned with the visitTIme as that of the vertex. Since we process the time in increasing order, there is no chance that a later vertex can share the same value as a new i. In other words, based on my understanding the following condition always holds,

1.3.0

11

AL

89

if (G[i].visitedTime != i) { ...} middle of the 4th line of section 11.12, repeated "is is"; The "therefor" before "is is" is also missing an 'e' Solution 20.8, Based on the description of the solution, I don't get the idea for the following formula:

typo

THL: Good catch! Fixed! THL: Good catch! Fixed.

1.2.1

20

DL

432

Pr(r, n) = Pr(r-1, n-1)Pn + Pr(r-1, n)(1-Pn) To me the correct one should be the following instead: Pr(r, n) = Pr(r-1, n-1)Pn + Pr(r, n-1)(1-Pn) Please help me check.

typo

Você também pode gostar