Skip to content

Commit 545b336

Browse files
author
Partho Biswas
committed
algoexpert - min rewards
1 parent 006c325 commit 545b336

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ A place where I upload my daily practice on Data Structure and Algorithm problem
8888
|77| [#724 - Find Pivot Index](https://leetcode.com/problems/find-pivot-index/)| [Python](leetcode.com/python/%23724%20-%20Find%20Pivot%20Index.py)| |
8989
|78| [#747 - Largest Number At Least Twice of Others](https://leetcode.com/problems/largest-number-at-least-twice-of-others/)| [Python](leetcode.com/python/%23747%20-%20Largest%20Number%20At%20Least%20Twice%20of%20Others.py)| |
9090
|79| [#581 - Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray/solution/)| [Python](leetcode.com/python/%23581%20-%20Shortest%20Unsorted%20Continuous%20Subarray.py)| |
91+
|80| [#482 - License Key Formatting](https://leetcode.com/problems/license-key-formatting/)| [Python](leetcode.com/python/%23482%20-%20License%20Key%20Formatting.py)| |
92+
|81| [#904 - Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/solution/)| [Python](leetcode.com/python/%23904%20-%20Fruit%20Into%20Baskets.py)| [Article](https://www.educative.io/courses/grokking-the-coding-interview/Bn2KLlOR0lQ), [Video 1](https://www.youtube.com/watch?v=s_zu2dOkq80), [Video 2](https://www.youtube.com/watch?v=za2YuucS0tw)|
9193

9294

9395

@@ -135,6 +137,7 @@ A place where I upload my daily practice on Data Structure and Algorithm problem
135137
|39| [Four_Number_Sum](algoexpert.io/questions/Four_Number_Sum.md) | [Python](algoexpert.io/python/Four_Number_Sum.py) |
136138
|40| [Subarray_Sort](algoexpert.io/questions/Subarray_Sort.md) | [Python](algoexpert.io/python/Subarray_Sort.py) |
137139
|41| [Largest_Range](algoexpert.io/questions/Largest_Range.md) | [Python](algoexpert.io/python/Largest_Range.py) |
140+
|42| [Min_Rewards](algoexpert.io/questions/Min_Rewards.md) | [Python](algoexpert.io/python/Min_Rewards.py) |
138141

139142

140143

algoexpert.io/python/Min_Rewards.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
3+
4+
# Solution #1
5+
# O(n^2) time | O(n) space
6+
def minRewards(score):
7+
rewards = [1 for _ in score]
8+
for i in range(1, len(score)):
9+
j = i - 1
10+
if score[i] > score[j]:
11+
rewards[i] = rewards[j] + 1
12+
else:
13+
while j >=0 and score[j] > score[j + 1]:
14+
rewards[j] = max(rewards[j], rewards[j + 1] + 1)
15+
j -= 1
16+
return sum(rewards)
17+
18+
19+
20+
# Solution #2
21+
# O(n) time | O(n) space
22+
def minRewards(score):
23+
pass
24+
25+
26+
27+
# Solution #3
28+
# O(n) time | O(n) space
29+
def minRewards(score):
30+
rewards = [1 for _ in score]
31+
for i in range(1, len(score)):
32+
if score[i] > score[i - 1]:
33+
rewards[i] = rewards[i - 1] + 1
34+
for i in reversed((range(len(score) - 1))):
35+
if score[i] > score[i + 1]:
36+
rewards[i] = max(rewards[i], rewards[i + 1] + 1)
37+
return sum(rewards)

0 commit comments

Comments
 (0)