|
| 1 | +"""A sequence of integers is called a zigzag sequence if each of its elements is either strictly less than both neighbors or strictly greater than both neighbors. For example, the sequence 4 2 3 1 5 3 is a zigzag, but 7 3 5 5 2 and 3 8 6 4 5 aren't. |
| 2 | +
|
| 3 | +For a given array of integers return the length of its longest contiguous sub-array that is a zigzag sequence. |
| 4 | +
|
| 5 | +Example |
| 6 | +
|
| 7 | +For a = [9, 8, 8, 5, 3, 5, 3, 2, 8, 6], the output should be |
| 8 | +zigzag(a) = 4. |
| 9 | +
|
| 10 | +The longest zigzag sub-arrays are [5, 3, 5, 3] and [3, 2, 8, 6] and they both have length 4. |
| 11 | +
|
| 12 | +Input/Output |
| 13 | +
|
| 14 | +[time limit] 4000ms (py) |
| 15 | +[input] array.integer a |
| 16 | +
|
| 17 | +Guaranteed constraints: |
| 18 | +2 <= a.length <= 25, |
| 19 | +0 <= a[i] <= 100. |
| 20 | +
|
| 21 | +[output] integer""" |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +def zigzag(a): |
| 26 | + """ |
| 27 | + >>> zigzag([9, 8, 8, 5, 3, 5, 3, 2, 8, 6]) |
| 28 | + 4 |
| 29 | +
|
| 30 | + >>> zigzag([2, 3, 1, 0, 2]) |
| 31 | + 3 |
| 32 | +
|
| 33 | + >>> zigzag([1, 2, 3, 2, 1]) |
| 34 | + 3 |
| 35 | +
|
| 36 | + >>> zigzag([2, 3, 1, 4, 2]) |
| 37 | + 5 |
| 38 | +
|
| 39 | + >>> zigzag([1, 2, 0, 3, 2, 1, 3, 2, 4, 0]) |
| 40 | + 6 |
| 41 | +
|
| 42 | + >>> zigzag([1, 2]) |
| 43 | + 2 |
| 44 | +
|
| 45 | + >>> zigzag([1, 2, 1]) |
| 46 | + 3 |
| 47 | +
|
| 48 | + >>> zigzag([1, 1]) |
| 49 | + 1 |
| 50 | +
|
| 51 | + """ |
| 52 | + |
| 53 | + # time: O(n) |
| 54 | + # space: O(1) |
| 55 | + |
| 56 | + longest = 1 |
| 57 | + curr_length = 1 |
| 58 | + |
| 59 | + if len(a) == 2 and a[0] != a[1]: |
| 60 | + return len(a) |
| 61 | + |
| 62 | + |
| 63 | + for i in range(len(a)-2): |
| 64 | + prev = a[i] |
| 65 | + curr = a[i+1] |
| 66 | + nxt = a[i+2] |
| 67 | + |
| 68 | + if (prev < curr and curr > nxt) or (prev > curr and curr < nxt): |
| 69 | + if nxt == a[-1]: |
| 70 | + curr_length += 2 |
| 71 | + else: |
| 72 | + curr_length += 1 |
| 73 | + |
| 74 | + longest = max(longest, curr_length) |
| 75 | + |
| 76 | + else: |
| 77 | + curr_length += 1 |
| 78 | + longest = max(longest, curr_length) |
| 79 | + curr_length = 1 |
| 80 | + |
| 81 | + return longest |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +def zigzag_recursive(a): |
| 86 | + """ |
| 87 | + >>> zigzag_recursive([9, 8, 8, 5, 3, 5, 3, 2, 8, 6]) |
| 88 | + 4 |
| 89 | +
|
| 90 | + >>> zigzag_recursive([2, 3, 1, 0, 2]) |
| 91 | + 3 |
| 92 | +
|
| 93 | + >>> zigzag_recursive([1, 2, 3, 2, 1]) |
| 94 | + 3 |
| 95 | +
|
| 96 | + >>> zigzag_recursive([2, 3, 1, 4, 2]) |
| 97 | + 5 |
| 98 | +
|
| 99 | + >>> zigzag_recursive([1, 2, 0, 3, 2, 1, 3, 2, 4, 0]) |
| 100 | + 6 |
| 101 | +
|
| 102 | + >>> zigzag_recursive([1, 2]) |
| 103 | + 2 |
| 104 | +
|
| 105 | + >>> zigzag_recursive([1, 2, 1]) |
| 106 | + 3 |
| 107 | +
|
| 108 | + >>> zigzag_recursive([1, 1]) |
| 109 | + 1 |
| 110 | +
|
| 111 | + """ |
| 112 | + |
| 113 | + # time: O(n) |
| 114 | + # space: O(n) |
| 115 | + |
| 116 | + |
| 117 | + if len(a) < 2: |
| 118 | + return len(a) |
| 119 | + |
| 120 | + if len(a) == 2 and a[0] != a[1]: |
| 121 | + return len(a) |
| 122 | + |
| 123 | + longest = 1 |
| 124 | + i = 1 |
| 125 | + good = True |
| 126 | + |
| 127 | + while good and i < len(a) - 1: |
| 128 | + curr = a[i] |
| 129 | + prev = a[i-1] |
| 130 | + nxt = a[i+1] |
| 131 | + |
| 132 | + if (prev < curr and curr > nxt) or (prev > curr and curr < nxt): |
| 133 | + i +=1 |
| 134 | + if i == len(a)-1: |
| 135 | + longest += 1 |
| 136 | + else: |
| 137 | + good = False |
| 138 | + longest += 1 |
| 139 | + |
| 140 | + return max(longest, zigzag_recursive(a[i:])) |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | +if __name__ == "__main__": |
| 145 | + import doctest |
| 146 | + results = doctest.testmod() |
| 147 | + |
| 148 | + if not results.failed: |
| 149 | + print "All tests passed" |
0 commit comments