Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

def fullJustify(self, words, maxWidth):

result = [ ]
curr_line = [ ]
num_letters = 0

for word in words:


if len(word) + num_letters + len(curr_line) > maxWidth:

# come to new_line
size = max(1, len(curr_line) - 1) # 1

# Add spaces to words curr_line


for i in range(maxWidth - num_letters):
index = i % size
curr_line[index] += ' '

# Append curr_line to final answer


result.append(''.join(curr_line))
curr_line, num_letters = [], 0

curr_line.append(word)
num_letters += len(word)

result.append(' '.join(curr_line).ljust(maxWidth))

return result

def nextPermutation(self, nums):


"""
:type nums: List[int]
:rtype: None Do not return anything, modify nums in-place instead.
"""
#1354321
# Find pivot element from right end

N = len(nums)
pivot = 0

for i in range(N-1, 0, -1):


if nums[i-1] < nums[i]:
pivot = i
break

if pivot == 0:
nums.sort()
# use reverse here instead of sort
return
swap = N - 1
while nums[pivot - 1] >= nums[swap]:
swap -= 1

nums[swap], nums[pivot - 1] = nums[pivot- 1], nums[swap]

#1453321
# Reverse from pivot to end

nums[pivot :] = sorted(nums[pivot :])

You might also like