Random commentary about Machine Learning, BigData, Spark, Deep Learning, C++, STL, Boost, Perl, Python, Algorithms, Problem Solving and Web Search
Assume the stream of integers is appearing in array 'a'.Initialize - majority = a[0];counter = 1;As the number comes in (i=1 onwards) -if a[i] == majority -> counter++;else if counter == 0 -> majority = a[i];else counter--;At the end, if there is majority, it is saved in the variable majority.
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
1 comment:
Assume the stream of integers is appearing in array 'a'.
Reply DeleteInitialize -
majority = a[0];
counter = 1;
As the number comes in (i=1 onwards) -
if a[i] == majority -> counter++;
else if counter == 0 -> majority = a[i];
else counter--;
At the end, if there is majority, it is saved in the variable majority.