Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 967f93b

Browse files
Gold Mine Problem
1 parent e4073c5 commit 967f93b

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""
2+
Problem Link: https://practice.geeksforgeeks.org/problems/gold-mine-problem2608/1
3+
4+
Given a gold mine called M of (n x m) dimensions. Each field in this mine contains a positive integer which is the amount
5+
of gold in tons. Initially the miner can start from any row in the first column. From a given cell, the miner can move
6+
to the cell diagonally up towards the right
7+
to the right
8+
to the cell diagonally down towards the right
9+
Find out maximum amount of gold which he can collect.
10+
11+
Example 1:
12+
Input: n = 3, m = 3
13+
M = {{1, 3, 3},
14+
{2, 1, 4},
15+
{0, 6, 4}};
16+
Output: 12
17+
18+
Explaination:
19+
The path is {(1,0) -> (2,1) -> (2,2)}.
20+
21+
Example 2:
22+
Input: n = 4, m = 4
23+
M = {{1, 3, 1, 5},
24+
{2, 2, 4, 1},
25+
{5, 0, 2, 3},
26+
{0, 6, 1, 2}};
27+
Output: 16
28+
Explaination:
29+
The path is {(2,0) -> (3,1) -> (2,2)
30+
-> (2,3)} or {(2,0) -> (1,1) -> (1,2)
31+
-> (0,3)}.
32+
33+
Your Task:
34+
You do not need to read input or print anything. Your task is to complete the function maxGold() which takes the
35+
values n, m and the mine M as input parameters and returns the maximum amount of gold that can be collected.
36+
37+
Expected Time Complexity: O(n*m)
38+
Expected Auxiliary Space: O(n*m)
39+
40+
Constraints:
41+
1 ≤ n, m ≤ 50
42+
1 ≤ M[i][j] ≤ 100
43+
"""
44+
class Solution:
45+
def maxGold(self, n, m, M):
46+
cache = [[0] * m for _ in range(n)]
47+
48+
directions = [[0, 1], [-1, 1], [1, 1]]
49+
50+
max_gold = 0
51+
for col in range(m-1, -1, -1):
52+
for row in range(n-1, -1, -1):
53+
max_val = 0
54+
for direction in directions:
55+
new_row = row + direction[0]
56+
new_col = col + direction[1]
57+
58+
if not (new_row >= 0 and new_row < n and new_col < m):
59+
continue
60+
61+
max_val = max(max_val, cache[new_row][new_col])
62+
63+
cache[row][col] = M[row][col] + max_val
64+
65+
if col == 0:
66+
max_gold = max(max_gold, cache[row][col])
67+
68+
return max_gold
69+
70+
71+
class Solution1:
72+
def maxGold(self, n, m, M):
73+
cache = [[0] * m for _ in range(n)]
74+
75+
directions = [[0, 1], [-1, 1], [1, 1]]
76+
queue = []
77+
for row in range(n):
78+
queue.append([row, 0, M[row][0]])
79+
80+
max_gold = 0
81+
while queue:
82+
83+
for _ in range(len(queue)):
84+
row, col, cur_gold = queue.pop(0)
85+
cache[row][col] = cur_gold
86+
max_gold = max(max_gold, cur_gold)
87+
88+
for direction in directions:
89+
next_row = row + direction[0]
90+
next_col = col + direction[1]
91+
if not (next_row >= 0 and next_row < n and next_col < m and cache[next_row][next_col] < cur_gold + M[next_row][next_col]):
92+
continue
93+
94+
queue.append([next_row, next_col, cur_gold + M[next_row][next_col]])
95+
96+
return max_gold

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /