Given N different tasks to be completed where the ith task belongs to a certain category, denoted by category[i], and has an associated reward represented by reward[i].
The objective is to determine the optimal order in which to complete these tasks to maximize the total reward. The reward obtained for completing a task is calculated as the product of the reward for that task and the number of distinct categories of tasks completed before (including the current task's category).
Find the maximum possible total reward that can be achieved by strategically ordering the completion of tasks.
Examples:
Input: N = 4, category = [3, 1, 2, 3] and reward = [2, 1, 4, 4]
Output: 29
Explanation: For the given input, the optimal order of completion is as follows:
- Complete the 2nd task: category[2] = 1, reward[2] = 1, number of distinct categories completed = 1, resulting in a profit of 1 x 1 = 1.
- Complete the 1st task: category[1] = 3, reward[1] = 2, number of distinct categories completed = 2, resulting in a profit of 2 x 2 = 4.
- Complete the 3rd task: category[3] = 2, reward[3] = 4, number of distinct categories completed = 3, resulting in a profit of 4 x 3 = 12.
- Complete the 4th task: category[4] = 3, reward[4] = 4, number of distinct categories completed = 3, resulting in a profit of 4 x 3 = 12.
Therefore, the total profit is 1 + 4 + 12 + 12 = 29.
Input: N = 6, category = [2, 2, 2, 1, 3, 3], reward = [1, 2, 6, 4, 3, 5]
Output: 58
Explanation: For the given input, the optimal order of completion is as follows:
- Complete the 1st task: category[1] = 2, reward[1] = 1, number of distinct categories completed = 1, resulting in a profit of 1 x 1 = 1.
- Complete the 5th task: category[5] = 3, reward[5] = 3, number of distinct categories completed = 2, resulting in a profit of 2 x 3 = 6.
- Complete the 4th task: category[4] = 1, reward[4] = 4, number of distinct categories completed = 3, resulting in a profit of 3 x 4 = 12.
- Complete the 2nd task: category[2] = 2, reward[2] = 2, number of distinct categories completed = 3, resulting in a profit of 3 x 2 = 6.
- Complete the 6th task: category[6] = 3, reward[6] = 5, number of distinct categories completed = 3, resulting in a profit of 3 x 5 = 15.
- Complete the 3rd task: category[3] = 2, reward[3] = 6, number of distinct categories completed = 3, resulting in a profit of 3 x 6 = 18.
Therefore, the total profit is 1 + 6 + 12 + 6 + 15 + 18 = 58.
Approach: To solve the problem, follow the below idea:
The problem can be solved by taking one task with minimum reward from each category and perform them in increasing order of their reward. For the remaining tasks, they can be performed in any order as all the categories would have been covered earlier.
Step-by-step algorithm:
- Initialize two maps: categorySumOfRewards to store the sum of rewards for each task category, and categoryMinimumReward to store the minimum reward for each task category.
- Iterate through the tasks, updating the maps based on the task category and reward.
- Create a vector minRewards to store the minimum rewards for each category and initialize maxTotalReward to 0.
- Iterate through categorySumOfRewards, calculate the contribution to maxTotalReward for each category by subtracting the minimum reward and multiplying it by the count of distinct categories completed before.
- Sort minRewards in ascending order.
- Iterate through minRewards, adding additional rewards to maxTotalReward based on the sorted minimum rewards.
- Return maxTotalReward as the maximum possible total reward.
Below is the implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum total reward from
// strategically ordering the completion of tasks
long findMaximumTotalReward(vector<int>& taskCategories,
vector<int>& taskRewards)
{
// Maps to store the sum of rewards and minimum reward
// for each task category
map<int, long long> categorySumOfRewards;
map<int, int> categoryMinimumReward;
int n = taskCategories.size();
// Calculate the sum of rewards and minimum reward for
// each task category
for (int i = 0; i < n; i++) {
// Update sum of rewards for the task category
if (!categorySumOfRewards.count(taskCategories[i])) {
categorySumOfRewards.insert({ taskCategories[i], 0 });
}
categorySumOfRewards[taskCategories[i]]
+= taskRewards[i];
// Update minimum reward for the task category
if (!categoryMinimumReward.count(
taskCategories[i])) {
categoryMinimumReward.insert(
{ taskCategories[i], INT_MAX });
}
categoryMinimumReward[taskCategories[i]]
= min(categoryMinimumReward[taskCategories[i]],
taskRewards[i]);
}
vector<int> minRewards;
long long maxTotalReward = 0LL;
int categoryCount = categoryMinimumReward.size();
// Calculate maximum total reward using accumulated sum
// of rewards and minimum rewards
for (auto it = categorySumOfRewards.begin();
it != categorySumOfRewards.end(); ++it) {
int taskCategory = it->first;
long long sum = it->second;
maxTotalReward += (long long)
(sum - categoryMinimumReward[taskCategory])
* (categoryCount);
minRewards.push_back(
categoryMinimumReward[taskCategory]);
}
// Sort the minimum rewards in ascending order
sort(minRewards.begin(), minRewards.end());
// Calculate additional reward based on the sorted
// minimum rewards
for (int i = 0; i < minRewards.size(); i++) {
maxTotalReward
+= (long long)minRewards[i] * (i + 1);
}
return (long)maxTotalReward;
}
int main()
{
// Test case
vector<int> taskCategories = { 3, 1, 2, 3 };
vector<int> taskRewards = { 2, 1, 4, 4 };
// Output the maximum total reward
cout << "Max Total Rewards: "
<< findMaximumTotalReward(taskCategories,
taskRewards)
<< endl;
return 0;
}
Java
import java.util.*;
public class Main {
// Function to find the maximum total reward from
// strategically ordering the completion of tasks
static long findMaximumTotalReward(ArrayList<Integer> taskCategories, ArrayList<Integer> taskRewards) {
// Maps to store the sum of rewards and minimum reward
// for each task category
HashMap<Integer, Long> categorySumOfRewards = new HashMap<>();
HashMap<Integer, Integer> categoryMinimumReward = new HashMap<>();
int n = taskCategories.size();
// Calculate the sum of rewards and minimum reward for
// each task category
for (int i = 0; i < n; i++) {
// Update sum of rewards for the task category
categorySumOfRewards.put(taskCategories.get(i), categorySumOfRewards.getOrDefault(taskCategories.get(i), 0L) + taskRewards.get(i));
// Update minimum reward for the task category
categoryMinimumReward.put(taskCategories.get(i), Math.min(categoryMinimumReward.getOrDefault(taskCategories.get(i), Integer.MAX_VALUE), taskRewards.get(i)));
}
ArrayList<Integer> minRewards = new ArrayList<>();
long maxTotalReward = 0L;
int categoryCount = categoryMinimumReward.size();
// Calculate maximum total reward using accumulated sum
// of rewards and minimum rewards
for (Map.Entry<Integer, Long> entry : categorySumOfRewards.entrySet()) {
int taskCategory = entry.getKey();
long sum = entry.getValue();
maxTotalReward += (sum - categoryMinimumReward.get(taskCategory)) * categoryCount;
minRewards.add(categoryMinimumReward.get(taskCategory));
}
// Sort the minimum rewards in ascending order
Collections.sort(minRewards);
// Calculate additional reward based on the sorted
// minimum rewards
for (int i = 0; i < minRewards.size(); i++) {
maxTotalReward += minRewards.get(i) * (i + 1);
}
return maxTotalReward;
}
public static void main(String[] args) {
// Test case
ArrayList<Integer> taskCategories = new ArrayList<>(Arrays.asList(3, 1, 2, 3));
ArrayList<Integer> taskRewards = new ArrayList<>(Arrays.asList(2, 1, 4, 4));
// Output the maximum total reward
System.out.println("Max Total Rewards: " + findMaximumTotalReward(taskCategories, taskRewards));
}
}
// This code is contributed by rambabuguphka
Python3
def find_maximum_total_reward(task_categories, task_rewards):
# Dictionary to store the sum of rewards and minimum reward
# for each task category
category_sum_of_rewards = {}
category_minimum_reward = {}
n = len(task_categories)
# Calculate the sum of rewards and minimum reward for
# each task category
for i in range(n):
# Update sum of rewards for the task category
if task_categories[i] not in category_sum_of_rewards:
category_sum_of_rewards[task_categories[i]] = 0
category_sum_of_rewards[task_categories[i]] += task_rewards[i]
# Update minimum reward for the task category
if task_categories[i] not in category_minimum_reward:
category_minimum_reward[task_categories[i]] = float('inf')
category_minimum_reward[task_categories[i]] = min(
category_minimum_reward[task_categories[i]],
task_rewards[i]
)
min_rewards = []
max_total_reward = 0
# Calculate maximum total reward using accumulated sum
# of rewards and minimum rewards
for task_category, reward_sum in category_sum_of_rewards.items():
sum_rewards = reward_sum
max_total_reward += (sum_rewards -
category_minimum_reward[task_category]) * len(category_minimum_reward)
min_rewards.append(category_minimum_reward[task_category])
# Sort the minimum rewards in ascending order
min_rewards.sort()
# Calculate additional reward based on the sorted
# minimum rewards
for i in range(len(min_rewards)):
max_total_reward += min_rewards[i] * (i + 1)
return max_total_reward
# Test case
task_categories = [3, 1, 2, 3]
task_rewards = [2, 1, 4, 4]
# Output the maximum total reward
print("Max Total Rewards:", find_maximum_total_reward(task_categories, task_rewards))
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
// Function to find the maximum total reward from
// strategically ordering the completion of tasks
static long FindMaximumTotalReward(List<int> taskCategories, List<int> taskRewards)
{
// Dictionaries to store the sum of rewards and minimum reward
// for each task category
Dictionary<int, long> categorySumOfRewards = new Dictionary<int, long>();
Dictionary<int, int> categoryMinimumReward = new Dictionary<int, int>();
int n = taskCategories.Count;
// Calculate the sum of rewards and minimum reward for
// each task category
for (int i = 0; i < n; i++)
{
// Update sum of rewards for the task category
if (!categorySumOfRewards.ContainsKey(taskCategories[i]))
{
categorySumOfRewards[taskCategories[i]] = 0;
}
categorySumOfRewards[taskCategories[i]] += taskRewards[i];
// Update minimum reward for the task category
if (!categoryMinimumReward.ContainsKey(taskCategories[i]))
{
categoryMinimumReward[taskCategories[i]] = int.MaxValue;
}
categoryMinimumReward[taskCategories[i]] = Math.Min(categoryMinimumReward[taskCategories[i]], taskRewards[i]);
}
List<int> minRewards = new List<int>();
long maxTotalReward = 0;
int categoryCount = categoryMinimumReward.Count;
// Calculate maximum total reward using accumulated sum
// of rewards and minimum rewards
foreach (var entry in categorySumOfRewards)
{
int taskCategory = entry.Key;
long sum = entry.Value;
maxTotalReward += (sum - categoryMinimumReward[taskCategory]) * categoryCount;
minRewards.Add(categoryMinimumReward[taskCategory]);
}
// Sort the minimum rewards in ascending order
minRewards.Sort();
// Calculate additional reward based on the sorted
// minimum rewards
for (int i = 0; i < minRewards.Count; i++)
{
maxTotalReward += minRewards[i] * (i + 1);
}
return maxTotalReward;
}
static void Main(string[] args)
{
// Test case
List<int> taskCategories = new List<int> { 3, 1, 2, 3 };
List<int> taskRewards = new List<int> { 2, 1, 4, 4 };
// Output the maximum total reward
Console.WriteLine("Max Total Rewards: " + FindMaximumTotalReward(taskCategories, taskRewards));
}
}
JavaScript
// Function to find the maximum total reward from strategically ordering the completion of tasks
function findMaximumTotalReward(taskCategories, taskRewards) {
// Maps to store the sum of rewards and minimum reward for each task category
let categorySumOfRewards = {};
let categoryMinimumReward = {};
let n = taskCategories.length;
// Calculate the sum of rewards and minimum reward for each task category
for (let i = 0; i < n; i++) {
// Update sum of rewards for the task category
if (!categorySumOfRewards.hasOwnProperty(taskCategories[i])) {
categorySumOfRewards[taskCategories[i]] = 0;
}
categorySumOfRewards[taskCategories[i]] += taskRewards[i];
// Update minimum reward for the task category
if (!categoryMinimumReward.hasOwnProperty(taskCategories[i])) {
categoryMinimumReward[taskCategories[i]] = Number.MAX_SAFE_INTEGER;
}
categoryMinimumReward[taskCategories[i]] = Math.min(categoryMinimumReward[taskCategories[i]], taskRewards[i]);
}
let minRewards = [];
let maxTotalReward = 0;
let categoryCount = Object.keys(categoryMinimumReward).length;
// Calculate maximum total reward using accumulated sum of rewards and minimum rewards
for (let taskCategory in categorySumOfRewards) {
let sum = categorySumOfRewards[taskCategory];
maxTotalReward += (sum - categoryMinimumReward[taskCategory]) * categoryCount;
minRewards.push(categoryMinimumReward[taskCategory]);
}
// Sort the minimum rewards in ascending order
minRewards.sort((a, b) => a - b);
// Calculate additional reward based on the sorted minimum rewards
for (let i = 0; i < minRewards.length; i++) {
maxTotalReward += minRewards[i] * (i + 1);
}
return maxTotalReward;
}
// Test case
let taskCategories = [3, 1, 2, 3];
let taskRewards = [2, 1, 4, 4];
// Output the maximum total reward
console.log("Max Total Rewards: " + findMaximumTotalReward(taskCategories, taskRewards));
OutputMax Total Rewards: 29
Time Complexity: O(N * log N), where N is the number of tasks.
Auxiliary Space: O(C), where C is the number of distinct task categories.
Similar Reads
Basics & Prerequisites
Data Structures
Getting Started with Array Data StructureArray is a collection of items of the same variable type that are stored at contiguous memory locations. It is one of the most popular and simple data structures used in programming. Basic terminologies of ArrayArray Index: In an array, elements are identified by their indexes. Array index starts fr
14 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem