27.10.2014 Views

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

Cracking the Coding Interview, 4 Edition - 150 Programming Interview Questions and Solutions

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Solutions</strong> to Chapter 20 | Hard<br />

17 }<br />

18 }<br />

19 }<br />

20 return null;<br />

21 }<br />

22<br />

23 private Rectangle makeRectangle(int length, int height) {<br />

24 if (groupList[length - 1] == null ||<br />

25 groupList[height - 1] == null) {<br />

26 return null;<br />

27 }<br />

28 if (trieList[height - 1] == null) {<br />

29 LinkedList words = groupList[height - 1].getWords();<br />

30 trieList[height - 1] = new Trie(words);<br />

31 }<br />

32 return makePartialRectangle(length, height,<br />

33 new Rectangle(length));<br />

34 }<br />

35<br />

36 private Rectangle makePartialRectangle(int l, int h,<br />

37 Rectangle rectangle) {<br />

38 if (rectangle.height == h) { // Check if complete rectangle<br />

39 if (rectangle.isComplete(l, h, groupList[h - 1])) {<br />

40 return rectangle;<br />

41 } else {<br />

42 return null;<br />

43 }<br />

44 }<br />

45<br />

46 // Compare columns to trie to see if potentially valid rect */<br />

47 if (!rectangle.isPartialOK(l, trieList[h - 1])) return null;<br />

48<br />

49 for (int i = 0; i < groupList[l-1].length(); i++) {<br />

50 Rectangle org_plus =<br />

51 rectangle.append(groupList[l-1].getWord(i));<br />

52 Rectangle rect = makePartialRectangle(l, h, org_plus);<br />

53 if (rect != null) {<br />

54 return rect;<br />

55 }<br />

56 }<br />

57 return null;<br />

58 }<br />

NOTE: See code attachment for full code.<br />

2 9 9<br />

<strong>Cracking</strong> <strong>the</strong> <strong>Coding</strong> <strong>Interview</strong> | Additional Review Problems

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!