๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“šAlgorithm ------------/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ์™„์ „ํƒ์ƒ‰/๋ชจ์˜๊ณ ์‚ฌ/C++

 

๋ฌธ์ œ ์„ค๋ช…

์ˆ˜ํฌ์ž๋Š” ์ˆ˜ํ•™์„ ํฌ๊ธฐํ•œ ์‚ฌ๋žŒ์˜ ์ค€๋ง์ž…๋‹ˆ๋‹ค.

์ˆ˜ํฌ์ž ์‚ผ์ธ๋ฐฉ์€ ๋ชจ์˜๊ณ ์‚ฌ์— ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ์ „๋ถ€ ์ฐ์œผ๋ ค ํ•ฉ๋‹ˆ๋‹ค.

์ˆ˜ํฌ์ž๋Š” 1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฐ์Šต๋‹ˆ๋‹ค.

 

1๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
2๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...
3๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...

 

1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€์˜ ์ •๋‹ต์ด ์ˆœ์„œ๋Œ€๋กœ ๋“ค์€ ๋ฐฐ์—ด answers๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ,

๊ฐ€์žฅ ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ๋งžํžŒ ์‚ฌ๋žŒ์ด ๋ˆ„๊ตฌ์ธ์ง€ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ ์กฐ๊ฑด

  • ์‹œํ—˜์€ ์ตœ๋Œ€ 10,000 ๋ฌธ์ œ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฌธ์ œ์˜ ์ •๋‹ต์€ 1, 2, 3, 4, 5์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.
  • ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋ฅผ ๋ฐ›์€ ์‚ฌ๋žŒ์ด ์—ฌ๋Ÿฟ์ผ ๊ฒฝ์šฐ, returnํ•˜๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.

 

์ž…์ถœ๋ ฅ ์˜ˆ

 

answers return
[1,2,3,4,5] [1]
[1,3,2,4,2] [1,2,3]

 

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • ์ˆ˜ํฌ์ž 1์€ ๋ชจ๋“  ๋ฌธ์ œ๋ฅผ ๋งžํ˜”์Šต๋‹ˆ๋‹ค.
  • ์ˆ˜ํฌ์ž 2๋Š” ๋ชจ๋“  ๋ฌธ์ œ๋ฅผ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค.
  • ์ˆ˜ํฌ์ž 3์€ ๋ชจ๋“  ๋ฌธ์ œ๋ฅผ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ๊ฐ€์žฅ ๋ฌธ์ œ๋ฅผ ๋งŽ์ด ๋งžํžŒ ์‚ฌ๋žŒ์€ ์ˆ˜ํฌ์ž 1์ž…๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • ๋ชจ๋“  ์‚ฌ๋žŒ์ด 2๋ฌธ์ œ์”ฉ์„ ๋งž์ท„์Šต๋‹ˆ๋‹ค.

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(vector<int> answers) {
    vector<int> answer;
    vector<int> sup1 = {1, 2, 3, 4, 5};
    vector<int> sup2 = {2, 1, 2, 3, 2, 4, 2, 5};
    vector<int> sup3 = {3, 3, 1, 1, 2, 2, 4, 4, 5, 5};
    
    vector<int> cnts = { 0, 0, 0 };
    
    if( answers.empty() )
        return answer;
    
    for( int i = 0; i < answers.size(); i++ )
    {
        if( answers[i] == sup1[i%sup1.size()])
            cnts[0]++;
         if( answers[i] == sup2[i%sup2.size()])
            cnts[1]++;
         if( answers[i] == sup3[i%sup3.size()])
            cnts[2]++;
    }
    int max = *max_element(cnts.begin(), cnts .end());
    for(int i = 0; i <3; i++)
    {
        if( max == cnts[i] )
            answer.push_back(i+1);
    }

    return answer;
}

 

๋ถ€๋„๋Ÿฝ๊ฒŒ๋„ ๋‚˜๋Š” ์™„์ „ ํƒ์ƒ‰ํ•˜๋Š” ๋ฒ•์„ ์•Œ๋ฉด์„œ ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ์™„์ „ ํƒ์ƒ‰์ด ๋ญ์ง€? ํ•˜๊ณ  ์žˆ์—ˆ๋‹ค;;

 

์ž ์‹œ ํ˜ผ๋ž€์Šค๋Ÿฌ์› ์ง€๋งŒ

for ๋ฌธ์„ ํ†ตํ•ด answers์— ์ ‘๊ทผํ•˜๋ฉด์„œ, ๋ฏธ๋ฆฌ ์ •์˜ ํ•ด๋†“์€

์ˆ˜ํฌ์ž๋“ค์˜ ์ •๋‹ต์ง€์™€ ๋น„๊ตํ•˜๋ฉด์„œ ์ •๋‹ต ๊ฐœ์ˆ˜๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด ๋˜๋Š” ๋ฌธ์ œ๋‹ค

 

cnts๋ฅผ int ํ˜• ๋ฐฐ์—ด๋กœ ํ–ˆ๋‹ค๊ฐ€ ์ตœ๋Œ€๊ฐ’ ์ฐพ๋Š” ๋“ฑ์˜ ์—ฐ์‚ฐ์„ ํ•˜๊ธฐ ์œ„ํ•ด

vector๋กœ ๋ณ€๊ฒฝํ•ด์„œ ํ’€์—ˆ๋‹ค