Rotate an Array using Constant Space

class Solution(object): def reverse(self, start, end, nums): while start < end: nums[start], nums[end] = nums[end], nums[start] start += 1 end -= 1 def rotate(self, nums, k): “”” :type nums: List[int] :type k: int :rtype: None Do not return anything, modify nums in-place instead. “”” if len(nums) == 0: return nums k = k % len(nums) … More Rotate an Array using Constant Space

Python Solution: House Robber Problem

class Solution(object): def __init__(self): self.maxVal = 0 self.lookup = {} def rob(self, nums): “”” :type nums: List[int] :rtype: int “”” if len(nums) == 0: return 0 if len(nums) == 1: return nums[0] if len(nums) >= 2: self.lookup[0] = nums[0] self.lookup[1] = max(nums[0], nums[1]) for index in range(2, len(nums)): # There are two options at each … More Python Solution: House Robber Problem

Pascal Triangle II

def getRow(self, rowIndex): “”” :type rowIndex: int :rtype: List[int] “”” if rowIndex == 0: return [1] numRows = rowIndex + 1 previous = [1] answer = None for x in range(2, numRows+1): row = [1]*x # Mutate the row for y in range(1, x-1): #print(“x={} y={} row={} prev={}”.format(x, y, row, previous)) row[y] = previous[y-1]+ previous[y] … More Pascal Triangle II

Pascal Triangle

def generate(self, numRows): “”” :type numRows: int :rtype: List[List[int]] “”” answer = [] if numRows == 0: return [] row = [1] answer.append(row) for x in range(2, numRows+1): # Prepare the row row = [1]*x # The first and last element on each row is 1, so skip those # indexes. for y in range(1, … More Pascal Triangle