Robert Eisele
Engineer, Systems Architect and DBA

Codingame: The greatest element

Original Problem

Goal

You're given several relations, either < or >. Output the greatest element. If there exists more than one possible output, sort it alphabetically.
Input
Line: Number N of relations
Line: Relation 1
...
Line: Relation N
Output
Line 1: Space separated list of elements.
Constraints
Elements are limited to A-Z.

Solution

We need a way to represent the relation with a data structure. I think the easiest way is to see every input line as a new competition where we mark every winner as a possible max, if we haven't seen it before. Every loser will never become the maximum, so we mark it accordingly. Holding these informtion in a hash map allows us to filter all positive candidates and print the sorted list like this:

var N = +readline()
var H = {}
var A = []
for (var i = 0; i < N; i++) {
  var min, max, line = readline()
  if (line[1] == '<') {
    min = line[0]
    max = line[2]
  } else {
    min = line[2]
    max = line[0]
  }
  H[min] = false
  H[max] = max in H ? H[max] : true
}
for (var i in H) {
  if (H[i])
    A.push(i)
}
A.sort();
print(A.join(" "))

« Back to problem overview