Algorithm correctness for Bank Kattis problem

Algorithm correctness for Bank Kattis problem

<a href="https://open.kattis.com/problems/bank" target="_blank">This</a>&nbsp;is the problem I am referring to. In quick summary:

This is the problem I am referring to. In quick summary:

Input: An integer time T; the time in minutes in which a bank closes and a set of pairs c and t that denotes the amount of cash (integer) this person carries and the time in minutes from now after which person leaves if not served. It takes one minute to serve a person and you must begin serving a person at time t at the latest.

Output: Maximum amount of money that can be collected within closing time.

My approach was this: place all the people in a map that maps money to time. I sort this map by money. I then make a queue-like structure where I take the person with the most money and place him/her as far back as possible. If the spot is occupied, then I move forward until I find a spot. If I can't then I just don't add the person.

Below is my helper function to determine whether or not I can insert a person.

// returns index where we can insert, otherwise -1
int canInsert(bool* queue, int timeToInsert) {
    if (!queue[timeToInsert]) {
        return timeToInsert;
    } else {
        int index = timeToInsert-1;
        while (index >= 0) {
            if (!queue[index]) {
                return index;
            } else {
                index--;
            }
        }
        return -1;
    }
}

Here is the main driver function:

// moneyToTime is a map that maps a person's money to the time value
int Bank(int T, map<int, int> moneyToTime) {
    int answer = 0;
    bool queue[47] = {0};
    for (map<int,int>::reverse_iterator i = moneyToTime.rbegin(); i != moneyToTime.rend(); i++) {
        if (T > 0) {
            // try to insert. If we can, then add to sum. Otherwise, don't.
            int potentialIndex = canInsert(queue, i->second);
            if (potentialIndex != -1) {
                queue[potentialIndex] = 1;
                answer += i->first;
                T--;
            }
        } else {
            break;
        }
    }
    return answer;
}

Logically, this makes sense to me and it is passing almost all the test cases. There are a couple that are failing; I can not see what they are. The test case errors are in fact indicating wrong answer, as opposed to bad runtime errors. Can someone help me see the fallacy in my approach?

c++ algorithm

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Dicey Issues in C/C++

C/C++ problems. If you are familiar with C/C++then you must have come across some unusual things and if you haven’t, then you are about to. The below codes are checked twice before adding, so feel free to share this article with your friends.

Loops in C++ | For, While, and Do While Loops in C++

In this Video We are going to see how to use Loops in C++. We will see How to use For, While, and Do While Loops in C++.

Using isdigit() in C/C++

In this article, we'll take a look at using the isdigit() function in C/C++. This is a very simple way to check if any value is a digit or not. Let's look

Object Oriented Programming in C++ | C++ OOPs Concepts | Learn Object Oriented C++

C++ is general purpose, compiled, object-oriented programming language and its concepts served as the basis for several other languages such as Java, Python, Ruby, Perl etc.

A Complete Guide to fread() in C/C++

In this article, we’ll take a look at using fread() in C/C++.