|
| 1 | +''' |
| 2 | +
|
| 3 | +Description: |
| 4 | +
|
| 5 | +Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order. |
| 6 | +
|
| 7 | +Since the answer may not fit in an integer data type, return the answer as a string. |
| 8 | +
|
| 9 | +If there is no answer return an empty string. |
| 10 | +
|
| 11 | + |
| 12 | +
|
| 13 | +Example 1: |
| 14 | +
|
| 15 | +Input: digits = [8,1,9] |
| 16 | +Output: "981" |
| 17 | +Example 2: |
| 18 | +
|
| 19 | +Input: digits = [8,6,7,1,0] |
| 20 | +Output: "8760" |
| 21 | +Example 3: |
| 22 | +
|
| 23 | +Input: digits = [1] |
| 24 | +Output: "" |
| 25 | +Example 4: |
| 26 | +
|
| 27 | +Input: digits = [0,0,0,0,0,0] |
| 28 | +Output: "0" |
| 29 | + |
| 30 | +
|
| 31 | +Constraints: |
| 32 | +
|
| 33 | +1 <= digits.length <= 10^4 |
| 34 | +0 <= digits[i] <= 9 |
| 35 | +The returning answer must not contain unnecessary leading zeros. |
| 36 | +
|
| 37 | +''' |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +from collections import defaultdict |
| 42 | +from functools import reduce |
| 43 | +from typing import List |
| 44 | + |
| 45 | +class Solution: |
| 46 | + def largestMultipleOfThree(self, digits: List[int]) -> str: |
| 47 | + |
| 48 | + digits.sort(reverse = True) |
| 49 | + |
| 50 | + remainder_dict = defaultdict( list) |
| 51 | + summation = 0 |
| 52 | + |
| 53 | + for number in digits: |
| 54 | + |
| 55 | + summation += number |
| 56 | + remainder_dict[number % 3].append( number ) |
| 57 | + |
| 58 | + |
| 59 | + if summation == 0: |
| 60 | + # Quick response for all-zero digits case |
| 61 | + return "0" |
| 62 | + |
| 63 | + ## Redundant remainder removal for modulo 3 system |
| 64 | + redundant = summation % 3 |
| 65 | + if redundant != 0: |
| 66 | + |
| 67 | + if remainder_dict[redundant]: |
| 68 | + remainder_dict[redundant].pop() |
| 69 | + else: |
| 70 | + |
| 71 | + if len(remainder_dict[3-redundant]) >= 2: |
| 72 | + remainder_dict[3-redundant].pop() |
| 73 | + remainder_dict[3-redundant].pop() |
| 74 | + else: |
| 75 | + return "" |
| 76 | + |
| 77 | + candidates = remainder_dict[0] + remainder_dict[1] + remainder_dict[2] |
| 78 | + |
| 79 | + if not candidates: |
| 80 | + # Quck response if no digits remain after removal of redundant remainder |
| 81 | + return "" |
| 82 | + |
| 83 | + # Keep digits in descending order |
| 84 | + candidates.sort( reverse = True ) |
| 85 | + |
| 86 | + # Make the number |
| 87 | + func = lambda x, y: 10*x + y |
| 88 | + value = reduce( func, candidates) |
| 89 | + |
| 90 | + # Convert to string form |
| 91 | + return str(value) |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +# n : the length input array, digits. |
| 96 | + |
| 97 | +## Time Complexity: O( n log n) |
| 98 | +# |
| 99 | +# The overhead in time is the cost of sorting, which is of O( n log n ) in Python by timSort. |
| 100 | + |
| 101 | +## Space Complexity: O( n ) |
| 102 | +# |
| 103 | +# The overhead in space is the storage for dictionary, remainder_dict, which is of O( n ) |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +from collections import namedtuple |
| 108 | +TestEntry = namedtuple('TestEntry', 'sequence') |
| 109 | + |
| 110 | +def test_bench(): |
| 111 | + |
| 112 | + test_data = [ |
| 113 | + TestEntry( sequence = [8,1,9]), |
| 114 | + TestEntry( sequence = [8,6,7,1,0]), |
| 115 | + TestEntry( sequence = [1]), |
| 116 | + TestEntry( sequence = [0,0,0,0,0,0]), |
| 117 | + TestEntry( sequence = [9,8,6,8,6]), |
| 118 | + |
| 119 | + ] |
| 120 | + |
| 121 | + # expected output: |
| 122 | + # Note: the third output is empty string '', which means no solution by definition. |
| 123 | + ''' |
| 124 | + 981 |
| 125 | + 8760 |
| 126 | +
|
| 127 | + 0 |
| 128 | + 966 |
| 129 | + ''' |
| 130 | + |
| 131 | + for t in test_data: |
| 132 | + |
| 133 | + print( Solution().largestMultipleOfThree( t.sequence) ) |
| 134 | + |
| 135 | + return |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == '__main__': |
| 140 | + |
| 141 | + test_bench() |
| 142 | + |
0 commit comments