Блог пользователя white_square

Автор white_square, история, 2 года назад, По-английски

Question:- 181B - Number of Triplets

Question

Solution that got accepted-: 178183074

Solution that got rejected-: 178183367

Other solution using same concept

Summary: The logic of both code is same, I've tried using HashMap and HashSet but the solution does not work.

I do not understand why don't the code using HasHmap/HashSet work.

Any help is appreciated.

Thank you.

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
2 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

String-based HashSet? Bad idea... The string formed by the numbers 1 and 23 is the same as the string formed by the numbers 12 and 3 — you can guess how this can go terribly wrong.

A HashMap / HashSet solution is easily possible by using a unique concatenation of numbers. Example: if the numbers are between -1000 and 1000, then you can artificially add 1000 to make them positive and then set: key = number1 * 2001 + number2. Thus, the key's remainder when divided by 2001 is definitely number2+1000, and its quotient is definitely number1+1000.

(Your frequency array solution does exactly this while also making use of the fact that the bounds on numbers are relatively small.)

TL;DR avoid string-based concatenation of numbers