剑指offer python版 礼物的最大价值

class Solution:
    def getmaxValue(self, values, rows, cols):
        if not values or rows<=0 or cols <=0:
            return 0
        # 用于存放中间数值的临时数组
        temp = [0] * cols
        for i in range(rows):
            for j in range(cols):
                left = 0
                up = 0
                if i > 0:
                    up = temp[j]
                if j > 0:
                    left = temp[j-1]
                temp[j] = max(up,left) + values[i*rows+j]
        return temp[-1]
s = Solution()
print(s.getmaxValue([1,10,3,8,12,2,9,6,5,7,4,11,3,7,16,5],4,4))
            

原文地址:https://www.cnblogs.com/xzm123/p/9857356.html

时间: 2024-08-30 15:49:16

剑指offer python版 礼物的最大价值的相关文章

剑指Offer对答如流系列 - 礼物的最大价值

面试题47:礼物的最大价值 题目描述 在一个m×n的棋盘的每一格都放有一个礼物,每个礼物都有一定的价值(价值大于0).你可以从棋盘的左上角开始拿格子里的礼物,并每次向左或者向下移动一格直到到达棋盘的右下角.给定一个棋盘及其上面的礼物,请计算你最多能拿到多少价值的礼物? 比如下面的棋盘中,如果按照红色数字的路线走可以拿到最大价值为53的礼物 问题分析 动态规划:定义f(i,j)为到达(i,j)位置格子时能拿到的礼物总和的最大值,则有:f(i,j)=max{f(i-1,j),f(i,j-1)}+va

剑指offer python版 二维数组的查找

def find_integer(matrix, num): """ :param matrix: [[]] :param num: int :return: bool """ if not matrix: return False rows, cols = len(matrix), len(matrix[0]) row, col = rows - 1, 0 while row >= 0 and col <= cols - 1: if

剑指offer python版 实现单例模式

class singleTon(object): def __init__(self,x): self.val=x single2=singleTon(2) a=single2 b=single2 print(a==b) print(a.val) a.val=334 print(b.val) 原文地址:https://www.cnblogs.com/xzm123/p/9847787.html

剑指offer python版 替换空格

print('dd dd add'.replace(' ','dd')) 原文地址:https://www.cnblogs.com/xzm123/p/9847854.html

剑指offer python版 删除链表中重复的结点

class ListNode(object): def __init__(self,x): self.val=x self.next=None class Link(object): def __init__(self,values=None): self.nodes=self.set_link(values) if values else None def get_link(self): return self.nodes def set_link(self,values): if not v

剑指offer python版 反转链表

class ListNode(object): def __init__(self,x): self.val=x self.next=None class Link(object): def __init__(self,values=None): self.nodes=self.set_link(values) if values else None def get_link(self): return self.nodes def set_link(self,values): if not v

剑指offer python版 顺时针打印矩阵

def aa(matrix): rows=len(matrix) cols=len(matrix[0]) start=0 ret=[] while start*2 <rows and start*2<cols: bb(matrix,rows,cols,start,ret) start +=1 return ret def bb(matrix,rows,cols,start,ret): row=rows-start-1 col=cols-start-1 for c in range(start,

剑指offer python版 数组中出现次数超过一半的数字

def aa(nums): if not nums: return False hashes={} ret=[] for s in nums: hashes[s]=hashes[s]+1 if hashes.get(s) else 1 if hashes[s] >len(nums)/2: ret.append(s) return list(set(ret)) print(aa([1,2,3,2,2,2,2,2,3,2,3,3,4])) 原文地址:https://www.cnblogs.com/x

剑指offer python版 数组中数值和下标相等的元素

def aa(nums): leng=len(nums) for i in range(leng): if i==nums[i]: print (i) i+=1 return None print(aa([0,1,2,3,4,5])) 原文地址:https://www.cnblogs.com/xzm123/p/9869255.html