LoginSignup
0
0

More than 5 years have passed since last update.

InterestingParty

Last updated at Posted at 2015-01-09

TopCoder problem "InterestingParty" used in Member SRM 494 (Division II Level One)
http://topcoder.bgcoder.com/print.php?id=2802

Mr. White is a very versatile person - absolutely everything is interesting to him. Perhaps this is why he has many friends. Quite unfortunately, however, none of his friends are versatile at all. Each of them is interested only in two topics and refuses to talk about anything else. Therefore, each time Mr. White organizes a party, it's a big problem for him to decide whom to invite so that the party is interesting to everybody. Now that Mr. White has a lot of experience in organizing parties, he knows for sure that a party will be interesting if and only if there's a topic interesting to each of the invited friends.
You will be given String[]s first and second. The i-th friend of Mr. White is interested in topics first[i] and second[i]. Return the largest number of friends that Mr. White can invite to his party so that the party will be interesting.

パーティーに人を招待したいのだが、その人達はそれぞれ2つの話題にしか興味がない。共通の話題について話せる最大の人数を求めよ。

という問題を考えてみる。

  1. 話題の辞書を用意する
  2. ヒットした場合カウントアップする
  3. 辞書内の最大の値を返す

で解けそうである。よって、コードは

InterestingParty.js
var Solver = { 
    solve: function(first, second) {
        return this.countUp(first.concat(second));
    },
    countUp: function(values)
    {
        var dict = {};
        var max = 0;
        for(var i = 0; i < values.length; ++i)
        {
            var value = values[i];
            dict[value] = 1 + (value in dict ? dict[value] : 0);
            max = max > dict[value] ? max : dict[value];
        }
        return max;
    }
};

単体テストコードは

InterestingPartyTest.js
TestCase("InterestingPartyTest", {
    setUp: function() {
        this.solver = Object.create(Solver);
    },
    "test Example0 should return 4": function() {
        assertEquals(4, this.solver.solve(['fishing', 'gardening', 'swimming', 'fishing'], ['hunting', 'fishing', 'fishing', 'biting']));
    },  
    "test Example1 should return 1": function() {
        assertEquals(1, this.solver.solve(['variety', 'diversity', 'loquacity', 'courtesy'], ['talking', 'speaking', 'discussion', 'meeting']));
    },  
    "test Example2 should return 3": function() {
        assertEquals(3, this.solver.solve(['snakes', 'programming', 'cobra', 'monty'], ['python', 'python', 'anaconda', 'python']));
    },  
    "test Example3 should return 6": function() {
        assertEquals(6, this.solver.solve(['t', 'o', 'p', 'c', 'o', 'd', 'e', 'r', 's', 'i', 'n', 'g', 'l', 'e', 'r', 'o', 'u', 'n', 'd', 'm', 'a', 't', 'c', 'h', 'f', 'o', 'u', 'r', 'n', 'i'], ['n', 'e', 'f', 'o', 'u', 'r', 'j', 'a', 'n', 'u', 'a', 'r', 'y', 't', 'w', 'e', 'n', 't', 'y', 't', 'w', 'o', 's', 'a', 't', 'u', 'r', 'd', 'a', 'y']));
    }
 });

一応githubにソースコードは保存しています。
https://github.com/bloodysnowx/ProgramContest/tree/master/js/InterestingParty

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0