金矿问题

Description:

描述:

This is a standard interview problem featured in interview coding rounds of Amazon, Flipkart.

这是亚马逊Flipkart的采访编码回合中的标准采访问题。

Problem statement:

问题陈述:

Given a gold mine of n*m dimensions, each cell in this mine contains a positive integer which is the amount of gold in tons. Initially, the miner is at the first column but can be at any row. He can move only right or right up or right down from the current cell, i.e., the miner can move to the cell diagonally up towards the right or right or diagonally down towards the right. Find out the maximum amount of gold he can collect.

给定一个 n * m尺寸 的金矿,该矿中的每个单元格都包含一个正整数,该黄金数量以吨为单位。 最初,该矿工位于第一列,但可以位于任何行。 他只能从当前牢房向右或向右或向下移动,即,矿工可以向右或向右斜向上或向右斜向向下移动到该牢房。 找出他可以收集的最大数量的黄金。

    Input:
Matrix:
{{1,5,12},
{2,4,4},
{0,6,4}
{3,0,0}	
}
Output: 
18
Input:
Matrix:
{{1,3,1,5},
{2,2,4,1},
{5,0,2,3},
{0,6,11,2}
}
Output: 
25

Explanation with example

举例说明

So, the matrix is:

因此,矩阵为:

Gold Mine Problem (1)

So, the miner starts from the first column (any row) and he has to reach the last row with maximum gold.

因此,矿工从第一列(任意行)开始,他必须以最大的金数到达最后一行。

To make a note, the possible moves from a cell (i,j) is to either of,

要说明一下,从单元格(i,j)可能移动到以下任一位置:

Gold Mine Problem (i)

Now, it seems apparently that greedy may work for the problem that is at the first column pick the cell with maximum value and then move to next best neighbouring cell.

现在,似乎贪婪似乎可以解决以下问题:在第一列中选​​择具有最大值的单元格,然后移至下一个最佳相邻单元格。

If we follow the above greedy approach,

如果我们遵循以上贪婪的方法,

We would pick (3,0) as our starting cell since that contains maximum gold out of the first column. The next best move would be (2,1) and then to (2,2).

我们将(3,0)作为起始单元格,因为它包含第一列中最多的金。 下一个最佳移动是(2,1)然后到(2,2)

So, the total gold collected is = (3+6+4) = 13

因此,收集的总金= (3 + 6 + 4)= 13

Is this the global maximum? Do our local maximum choices lead to global maximum?

这是全球最大值吗? 我们的局部最大选择会导致整体最大吗?

No, it's not the global maximum.

不,这不是全局最大值。

The global maximum path would be: (1,0) ->(0,1)->(0,2)

全局最大路径为: (1,0)->(0,1)->(0,2)

Total coin that can be achieved: (2+5+12) = 19

可以达到的总硬币数: (2 + 5 + 12)= 19

So, the local best choices don't lead to the global best and hence we need dynamic programming.

因此,本地的最佳选择不会导致全球最佳,因此我们需要动态编程。

Solution Approach:

解决方法:

Create a DP matrix of dimension m*n: DP[m][n]

创建尺寸为m * n的DP矩阵: DP [m] [n]

The first column of the DP matrix would be same as the input matrix. Rest of the columns are 0,

DP矩阵的第一列将与输入矩阵相同。 其余列为0,

    for i=0 to n-1
DP[i][0]=arr[i][0];

For the other columns, update DP value for every row. For every cell (i,j) update like following way.

对于其他列,更新每行的DP值。 对于每个单元格(i,j)都按照以下方式进行更新。

Gold Mine Problem (ii)

So, for the earlier input matrix,

因此,对于较早的输入矩阵,

Gold Mine Problem (2)

After completion of the First column (row wise computing),

完成第一列(行计算)后,

Gold Mine Problem (3)

After completion of second column (row wise computation),

完成第二列后(行计算),

Gold Mine Problem (4)

Now find the maximum of the final column, that's the maximum gold that can be collected.

现在找到最后一列的最大值,这就是可以收集的最大黄金量。

Gold Mine Problem (5)

Result=19

结果= 19

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
int GoldMine(int** arr, int n, int m)
{
//DP table
int DP[n][m];
memset(DP, 0, sizeof(DP));
for (int i = 0; i < n; i++)
DP[i][0] = arr[i][0];
//for every column
for (int j = 1; j < m; j++) {
//check which option is better accordingly
for (int i = 0; i < n; i++) {
//choosing max of possible moves
DP[i][j] = arr[i][j];
int val = DP[i][j - 1];
if (i - 1 >= 0) {
if (val < DP[i - 1][j - 1])
val = DP[i - 1][j - 1];
}
if (i + 1 < n) {
if (val < DP[i + 1][j - 1])
val = DP[i + 1][j - 1];
}
DP[i][j] += val;
}
}
// find the maximum of the last column
int gold = DP[0][m - 1];
for (int i = 1; i < n; i++) {
if (DP[i][m - 1] > gold)
gold = DP[i][m - 1];
}
return gold;
}
int main()
{
int n, item, m;
cout << "Enter matrix dimensions, m & n\n";
cin >> n >> m;
cout << "Input matrix cells\n";
int** arr = (int**)(malloc(sizeof(int*) * n));
//input array
for (int j = 0; j < n; j++) {
arr[j] = (int*)(malloc(sizeof(int) * m));
for (int k = 0; k < m; k++)
cin >> arr[j][k];
}
cout << "Max amount of gold that can be collected: " << GoldMine(arr, n, m) << endl;
return 0;
}

Output

输出量

Enter matrix dimensions, m & n
4 3
Input matrix cells
1 5 12
2 4 4
0 6 4
3 0 0
Max amount of gold that can be collected: 19

翻译自: https://www.includehelp.com/icp/gold-mine-problem.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540577.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[转载] python中的数组类型及特点

参考链接&#xff1a; Python中的Array | 数组2(简介和功能) 名称 表示方法示例 是否有序 函数方法&#xff08;增删等&#xff09; 特点 List 类型表示&#xff1a;L L [Adam, 95.5, Lisa, 85] 有序 增加&#xff1a;&#xff08;1&#xff09;L.append(Paul),增加…

puppet

Puppet前期环境&#xff08;网络、解析、yum源、NTP&#xff09;在上一章节已经准备就绪&#xff0c;接下来我们就开始安装Puppet了&#xff0c;安装Puppet其实很简单&#xff0c;官方已经提供了yum源&#xff0c;只需要自己将所需要的安装包下载下来然后做成本地yum源即可使用…

[转载] 【数学问题】利用python求解表达式

参考链接&#xff1a; Python 变量 &#xff5c;表达式 &#xff5c;条件和函数 有时候我们会遇到一些很复杂的表达式&#xff0c;或者想要求解某个表达式&#xff0c;但是手动计算的话不但耗时还费精力&#xff0c;我们能不能利用计算机来帮助我们进行计算呢&#xff1f; 1…

cesium广告牌_公路广告牌

cesium广告牌Description: 描述&#xff1a; This is a standard dynamic programing problem of finding maximum profits with some constraints. This can be featured in any interview coding rounds. 这是在某些约束条件下找到最大利润的标准动态编程问题。 这可以在任何…

你和大牛差了啥

mmp。无时无刻不在想和大牛差在哪里了。别人为什么可以那么牛逼而你tmd那么菜&#xff01;整个人顿时都颓废了。啥事儿不想干。后来想了想感情就是他比较黑吧。

[转载] python数组的使用

参考链接&#xff1a; Python中整数的最大可能值是多少&#xff1f; 原文地址为&#xff1a; python数组的使用 python数组的使用 python数组的使用 2010-07-28 17:17 1、Python的数组分三种类型&#xff1a; (1) list 普通的链表&#xff0c;初始化后可以通过特定方法…

scala中循环守卫_Scala中的循环

scala中循环守卫Scala中的循环 (Loops in Scala) In programming, many times a condition comes when we need to execute the same statement or block of code more than one time. It could be difficult to write the same code multiple times, so programing language d…

50个必备基础命令

1.tar创建一个新的tar文件$ tar cvf archive_name.tar dirname/解压tar文件$ tar xvf archive_name.tar查看tar文件$ tar tvf archive_name.tar2. grep在文件中查找字符串(不区分大小写)$ grep -i "the" demo_file输出成功匹配的行&#xff0c;以及该行之后的三行$ g…

NM的完整形式是什么?

NM&#xff1a;无消息 (NM: No Message) NM is an abbreviation of "No Message". NM是“无消息”的缩写。 It is an expression, which is commonly used in the Gmail platform. It is also written as N/M or n/m or *n/m*. It is written in the subject of the…

[转载] python中全局变量和局部变量解析

参考链接&#xff1a; Python中的全局变量和局部变量 python函数中可以访问全局变量但是不能给全局变量赋值&#xff0c;除非进行显式声明global a 比如定义了全局变量 a 在函数my_fun()中可以直接访问a的值&#xff0c;而不需要global全局变量申明。下图为上面代码运行输出 …

【iCore4 双核心板_FPGA】例程十六:基于双口RAM的ARM+FPGA数据存取实验

实验现象&#xff1a; 核心代码&#xff1a; int main(void) {/* USER CODE BEGIN 1 */int i;int address,data;char error_flag 0;char receive_data[50];char buffer[8];char *p;/* USER CODE END 1 *//* MCU Configuration-----------------------------------------------…

[转载] Python中TFTP的理解

参考链接&#xff1a; Python中的打包pack和拆包unpack参数 Num01–>TFTP协议介绍 TFTP&#xff08;Trivial File Transfer Protocol,简单文件传输协议&#xff09; 是TCP/IP协议族中的一个用来在客户端与服务器之间进行简单文件传输的协议 特点&#xff1a; 1,简单 2…

gn fast-gn_GN的完整形式是什么?

gn fast-gnGN&#xff1a;晚安 (GN: Good Night) GN is an abbreviation of "Good Night". GN是“ Good Night”的缩写 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Facebook, Yahoo Messenge…

从零开始编写自己的C#框架(27)——什么是开发框架

前言 做为一个程序员&#xff0c;在开发的过程中会发现&#xff0c;有框架同无框架&#xff0c;做起事来是完全不同的概念&#xff0c;关系到开发的效率、程序的健壮、性能、团队协作、后续功能维护、扩展......等方方面面的事情。很多朋友在学习搭建自己的框架&#xff0c;很多…

[转载] Python 递归 深入理解递归 Python递归剖析,绝对让你看懂!

参考链接&#xff1a; Python | print()中的结束参数 目录 递归剖析 递归的两个过程 return 返回值 详解 递归思路二分法和递归尾递归递归练习题 递归剖析 递归真的很重要&#xff0c;之前学的时候&#xff0c;学的一知半解&#xff0c;以为真正了解&#xff0c;每次想到递归…

laravel 项目迁移_在Laravel迁移

laravel 项目迁移Before moving forward we need to know some facts about it, 在继续前进之前&#xff0c;我们需要了解一些事实&#xff0c; Resources: In these directories, we have already a js, lang, sass and view page. Where, sass and js file holf their uncom…

Python之list对应元素求和

本次分享将讲述如何在Python中对多个list的对应元素求和&#xff0c;前提是每个list的长度一样。比如&#xff1a;a[1,2,3], b[2,3,4], c[3,4,5], 对a,b,c的对应元素求和&#xff0c;输出应为[6,9,12].    方法一&#xff1a;   直接求解&#xff0c;按照对应元素相加的…

[转载] Python中str跟int的转换

参考链接&#xff1a; Python中的类型转换 字符串str转换成int: int_value int(str_value) int转换成字符串str: str_value str(int_value) a100 b666 #int转str类型 print(int转str类型) print(int转str&#xff1a; str(a)) #str转int类型 print(str转int类型…

ot协议是什么_OT的完整形式是什么?

ot协议是什么OT&#xff1a;主题外 (OT: Off Topic) OT is an abbreviation of "Off Topic". OT是“ Off Topic”的缩写 。 It is an expression, which is commonly used in Gmail or messaging platform. It shows that the email that has been sent is irrelev…

[转载] python中字符串编码形式及其所占字节

参考链接&#xff1a; Python中的字节对象与字符串 1.常见字符串编码错误 在使用Python读文件时经常遇到编码问题引起的错误&#xff0c;比如&#xff1a; UnicodeDecodeError: gbk codec cant decode byte 0x80 in position 30: illegal multibyte sequence 遇到这种异…