@mzchen I don't understand. Do you mean the docstring?
Tay Yang Shun
@yangshun
Posts made by yangshun

RE: Easy to Understand Python Solution

RE: Python Efficient O(k) Insert and Sum using Trie
@jerom.chaigmail.com Yep good catch, have fixed it. Thanks!

Python Efficient O(k) Insert and Sum using Trie
A standard
Trie
based solution where each node keeps track of the total count of its children.For inserting, we first determine if the string already exists in the Trie. If it does, we calculate the difference in the previous and new value, and update the nodes with the difference as we traverse down the Trie nodes.
Sum is simple because each node already holds the sum of its children and we simply have to traverse to the node and obtain its count.
This results in both operations being O(k), where k is the length of the string/prefix.
class TrieNode(): def __init__(self, count = 0): self.count = count self.children = {} class MapSum(object): def __init__(self): """ Initialize your data structure here. """ self.root = TrieNode() self.keys = {} def insert(self, key, val): """ :type key: str :type val: int :rtype: void """ # Time: O(k) curr = self.root delta = val  self.keys.get(key, 0) self.keys[key] = val curr = self.root curr.count += delta for char in key: if char not in curr.children: curr.children[char] = TrieNode() curr = curr.children[char] curr.count += delta def sum(self, prefix): """ :type prefix: str :rtype: int """ # Time: O(k) curr = self.root for char in prefix: if char not in curr.children: return 0 curr = curr.children[char] return curr.count

Python with explanation  Generate All Possibilities
 Use
itertools.permutations
to generate all the possible operands and operators to form an array of length 7, representing an equation of 4 operands and 3 operators.  The
possible
function tries to evaluate the equation with different combinations of brackets, terminating as soon as an equation evaluates to 24. Each timeevaluate
is called, it reduces the length of the equation by 2, as it takes a triplet (operand, operator, operand) and evaluates into a value.  Compare the final result of the equation with a small delta because of floating point inaccuracies.
By Yang Shun
class Solution(object): def judgePoint24(self, nums): """ :type nums: List[int] :rtype: bool """ import itertools TARGET = 24 EQN_LEN = 3 # (Operand, Operator, Operand) triplet. # Generate all possible number sequences. Convert to float string so that # division does not result in truncation. number_orders = set(tuple(itertools.permutations([str(num) + '.0' for num in nums]))) # Generate all possible operator sequences. operator_orders = set(tuple(itertools.permutations('***///+++', len(nums)  1))) # Evaluate an equation with different permutation of brackets # and return True if any of them evaluate to the target. def possible(equation): found = [False] def evaluate(eqn): # Reduces an equation by length 2 each time: # An equation of ['1.0', '*', '2.0', '+', '3.0', '/', '4.0'] becomes: #  [2.0', '+', '3.0', '/', '4.0'] (1.0 * 2.0 reduced to 2.0) #  [1.0', '*', '5.0', '/', '4.0'] (2.0 + 3.0 reduced to 5.0) #  [1.0', '*', '2.0', '+', '0.75'] (3.0 / 4.0 reduced to 0.75) if found[0]: return if len(eqn) == EQN_LEN: val = eval(''.join(eqn)) # Compare against a delta because of floating point inaccuracies. if abs(val  TARGET) < 0.0001: found[0] = True return # Recursively try different permutations # of operands + operators triplets, simulating brackets. for i in range(0, len(eqn)  1, 2): try: # Wrap in try/except as there can be a division by 0 error. evaluate(eqn[:i] + [str(eval(''.join(eqn[i:i + EQN_LEN])))] + eqn[i + EQN_LEN:]) except: pass evaluate(equation) return found[0] for number_order in number_orders: for operator_order in operator_orders: equation = [None] * (len(number_order) + len(operator_order)) for i, number in enumerate(number_order): equation[0 + i * 2] = number for i, operator in enumerate(operator_order): equation[1 + i * 2] = operator # Generate an equation to test whether it is possible to get 24 using it. # Example equation: ['1.0', '*', '2.0', '+', '3.0', '/', '4.0'] if possible(equation): return True return False
 Use

Easy to Understand Python Solution
We can use the standard twopointer approach that starts at the left and right of the string and move inwards. Whenever there is a mismatch, we can either exclude the character at the left or the right pointer. We then take the two remaining substrings and compare against its reversed and see if either one is a palindrome.
By Yang Shun
class Solution(object): def validPalindrome(self, s): """ :type s: str :rtype: bool """ # Time: O(n) # Space: O(n) left, right = 0, len(s)  1 while left < right: if s[left] != s[right]: one, two = s[left:right], s[left + 1:right + 1] return one == one[::1] or two == two[::1] left, right = left + 1, right  1 return True

Simple answer using join
Join two tables of left and right seats and check for the conditions where middle and left or right are empty.
SELECT middle_seat.seat_id FROM cinema middle_seat LEFT JOIN cinema left_seat ON middle_seat.seat_id  left_seat.seat_id = 1 LEFT JOIN cinema right_seat ON right_seat.seat_id  middle_seat.seat_id = 1 WHERE middle_seat.free = 1 AND (left_seat.free = 1 OR right_seat.free = 1) ORDER BY middle_seat.seat_id ;

Simple solution using awk
awk '/^(\([09]{3}\) [09]{3})[09]{3}[09]{4}$/' file.txt

RE: Python Simple Solution
@Ellest You're right, I missed that out. Have edited the answer to account for that. Thanks for pointing it out (:

RE: Python DP with explanation (Beats 88%)
@XKlight @songzy12 I just tried submitting thrice and got "Accepted" for all three times.
EDIT: I optimized the original version and made it twice as fast by counting as I iterate the inner loop. It should not TLE for you now.
Could you try again?