Editorial for Casino Strategy
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
The aim is to only play the game if its value is strictly greater than the last time you saw it.
The first time you see the value, you always play the game (since implicitly, the "previously seen" value is ). Then, we need a way to keep track of each game and their previously seen value.
We can use a hash map to store the previously seen values for each game. When we encounter the game, we check in the hash map to see if the current value is greater than the stored value. If it is, we play; if it isn't, we skip. Then, we store the previously seen value in the hashmap.
This will be in terms of both time and space complexity, as we are looping over all lines once and we are using a map to store previously seen values of games.
C++ solution:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
unordered_map<string, int> values;
string name;
int value;
while (n--) {
cin >> name >> value;
if (value > values[name]) cout << "Play " << name << endl;
else cout << "Skip " << name << endl;
values[name] = value;
}
}
Python solution:
from collections import defaultdict
vals = defaultdict(int)
n = int(input())
for _ in range(n):
name, val = input().split()
val = int(val)
print(f"Play {name}" if val > vals[name] else f"Skip {name}")
vals[name] = val
Comments