class Solution(object):
def maximumSwap(self, num):
"""
:type num: int
:rtype: int
"""
best = num
n = len(str(num))
j, second = 0, num%10
for i in range(1, n):
first = (num%10**(i+1))/10**i
new = numfirst*10**isecond*10**j+first*10**j+second*10**i
best = max(best, new)
if first > second:
j, second = i, first
return best
bmay
@bmay
Posts made by bmay

Python, single pass with DP

Python, 12 lines
class Solution(object): def canIWin(self, maxChoosableInteger, desiredTotal): """ :type maxChoosableInteger: int :type desiredTotal: int :rtype: bool """ if maxChoosableInteger**2+maxChoosableInteger < desiredTotal*2: return False return self.helper(maxChoosableInteger, desiredTotal, 0, {}) def helper(self, max, total, used, cached): if used not in cached: cached[used] = False for num in range(1, max+1): if used & 1 << num: continue if num >= total or not self.helper(max, totalnum, used  1 << num, cached): cached[used] = True break return cached[used]

Clear DP Python using hashmap and hashset
The dictionary key is the stone and the value is a set that contains the units for each jump that can land on that particular stone.
class Solution(object): def canCross(self, stones): """ :type stones: List[int] :rtype: bool """ landings = collections.defaultdict(set) landings[1].add(1) for i in range(1, len(stones)1): stone = stones[i] if stone not in landings: continue for k in landings[stone]: for new_k in k1, k, k+1: if not new_k: continue landings[stone+new_k].add(new_k) return stones[1] in landings

RE: My solution does not need a table for palindrome, is it right ? It uses only O(n) space.
@kq2 this is an elegant, awesome, and truly underrated solution!

RE: My DP Solution ( explanation and code)
Python:
class Solution(object): def minCut(self, s): """ :type s: str :rtype: int """ pal = [[False for _ in range(len(s))] for _ in range(len(s))] cuts = [len(s)i1 for i in range(len(s))] for start in range(len(s)1,1,1): for end in range(start, len(s)): if s[start] == s[end] and (endstart < 2 or pal[start+1][end1]): pal[start][end] = True if end == len(s)1: cuts[start] = 0 else: cuts[start] = min(cuts[start], 1+cuts[end+1]) return cuts[0]

RE: Python, Straightforward with Explanation
@awice Thanks! I rattled my brain a little bit before I noticed those issues, but your explanation helped a lot!

RE: Python, Straightforward with Explanation
@awice said in Python, Straightforward with Explanation:
This partial schedule would have length L = (M1) * (N1) + Mct.
Shouldn't this be
L = (M  1) * (N
+1) + Mct
?After we have inserted all tasks of frequency Mct  1 (which clearly will not collide), other tasks of frequency lower than Mct  1 will never have any task written collide with it's leftneighbor (because of the writing order)
Don't you mean
M  1
, notMct  1
? 
RE: Java intuitive 22ms solution with stack
@FutureVolador that would return False during the loop for pos = 3

RE: 3 methods for python with explains
so N % 9 = a[0] + a[1] + ..a[n]
I don't understand how this follows...

RE: Scrolling bug in forum page is really annoying
@daydayup Totally agree. I experience the same issue in Chrome and Firefox. It is pretty infuriating.