스트리밍 사이트에서 장르 별로 가장 많이 재생된 노래를 두 개씩 모아 베스트 앨범을 출시하려 합니다. 노래는 고유 번호로 구분하며, 노래를 수록하는 기준은 다음과 같습니다.

  1. 속한 노래가 많이 재생된 장르를 먼저 수록합니다.
  2. 장르 내에서 많이 재생된 노래를 먼저 수록합니다.
  3. 장르 내에서 재생 횟수가 같은 노래 중에서는 고유 번호가 낮은 노래를 먼저 수록합니다.

노래의 장르를 나타내는 문자열 배열 genres와 노래별 재생 횟수를 나타내는 정수 배열 plays가 주어질 때, 베스트 앨범에 들어갈 노래의 고유 번호를 순서대로 return 하도록 solution 함수를 완성하세요.

제한사항

  • genres[i]는 고유번호가 i인 노래의 장르입니다.
  • plays[i]는 고유번호가 i인 노래가 재생된 횟수입니다.
  • genres와 plays의 길이는 같으며, 이는 1 이상 10,000 이하입니다.
  • 장르 종류는 100개 미만입니다.
  • 장르에 속한 곡이 하나라면, 하나의 곡만 선택합니다.
  • 모든 장르는 재생된 횟수가 다릅니다.

입출력 예

genresplaysreturn

["classic", "pop", "classic", "classic", "pop"] [500, 600, 150, 800, 2500] [4, 1, 3, 0]

입출력 예 설명

classic 장르는 1,450회 재생되었으며, classic 노래는 다음과 같습니다.

  • 고유 번호 3: 800회 재생
  • 고유 번호 0: 500회 재생
  • 고유 번호 2: 150회 재생

pop 장르는 3,100회 재생되었으며, pop 노래는 다음과 같습니다.

  • 고유 번호 4: 2,500회 재생
  • 고유 번호 1: 600회 재생

따라서 pop 장르의 [4, 1]번 노래를 먼저, classic 장르의 [3, 0]번 노래를 그다음에 수록합니다.

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

using namespace std;

vector<int> solution(vector<string> genres, vector<int> plays) {
    vector<int> answer;
    map<string, int> gArr;
    map<string, multimap<int, int, greater<int>>> pArr;
    int count = 0;
    for (int i = 0; i < genres.size(); i++)
    {
        auto it = gArr.find(genres[i]);
        if (it != gArr.end())  gArr[genres[i]] += plays[i];
        else gArr[genres[i]] = plays[i];
        pArr[genres[i]].insert(make_pair(plays[i], i));
    }
    for (auto k: gArr)
    {
        auto max = max_element(
        begin(gArr), end(gArr), 
            [] (const pair<string, int>& a, const pair<string, int>& b) {
            return a.second < b.second;
        });
        count = 0;
        for (auto p = pArr[max->first.c_str()].begin(); p != pArr[max->first.c_str()].end(); ++p)
        {
            answer.push_back(p->second);
            if (++count == 2) break;
        }
        max->second = 0;
    }
    return answer;
}

'Challenge' 카테고리의 다른 글

[Programmers] 완전탐색 - 모의고사  (0) 2021.09.30
[Programmers] Stack/Queue - 프린터  (0) 2021.09.29
[LeetCode] Container With Most Water  (0) 2021.09.29
[LeetCode] Longest Common Prefix  (0) 2021.09.29
[LeetCode] Integer to Roman  (0) 2021.09.29

Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.

Notice that you may not slant the container.

 

Example 1:

Input: height = [1,8,6,2,5,4,8,3,7] Output: 49 Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

Example 2:

Input: height = [1,1] Output: 1

Example 3:

Input: height = [4,3,2,1,4] Output: 16

Example 4:

Input: height = [1,2,1] Output: 2

 

Constraints:

  • n == height.length
  • 2 <= n <= 105
  • 0 <= height[i] <= 104
class Solution {
public:
    int maxArea(vector<int>& height) {
        int maxVal = 0;
        int left = 0;
        int right = height.size() - 1;
        while (left <= right)
        {
            maxVal = max(maxVal, min(height[left], height[right]) * (right - left));
            if (height[left] < height[right]) left++;
            else right--;
        }
        return maxVal;
    }
};

'Challenge' 카테고리의 다른 글

[Programmers] Stack/Queue - 프린터  (0) 2021.09.29
[Programmers] Hash - 베스트앨범  (0) 2021.09.29
[LeetCode] Longest Common Prefix  (0) 2021.09.29
[LeetCode] Integer to Roman  (0) 2021.09.29
[LeetCode] Roman to Integer  (0) 2021.09.29

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

 

Example 1:

Input: strs = ["flower","flow","flight"] Output: "fl"

Example 2:

Input: strs = ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings.

 

Constraints:

  • 1 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] consists of only lower-case English letters.
class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        string str = "";
        if (strs.size() == 1)
        {
            str = strs[0];
        }
        else
        {
            for (int i = 1 ; i <= strs[0].length(); i++)
            {
                string temp = strs[0].substr(0, i);
                for (int j = 1; j < strs.size(); j++)
                {
                    if (strs[j].find(temp) == 0)
                    {
                        if (j == strs.size() - 1)
                        {
                            str = temp; 
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
        return str;
    }
};

'Challenge' 카테고리의 다른 글

[Programmers] Hash - 베스트앨범  (0) 2021.09.29
[LeetCode] Container With Most Water  (0) 2021.09.29
[LeetCode] Integer to Roman  (0) 2021.09.29
[LeetCode] Roman to Integer  (0) 2021.09.29
[Codility] CountDiv  (0) 2021.09.27

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000

For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9. 
  • X can be placed before L (50) and C (100) to make 40 and 90. 
  • C can be placed before D (500) and M (1000) to make 400 and 900.

Given an integer, convert it to a roman numeral.

 

Example 1:

Input: num = 3 Output: "III"

Example 2:

Input: num = 4 Output: "IV"

Example 3:

Input: num = 9 Output: "IX"

Example 4:

Input: num = 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3.

Example 5:

Input: num = 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

 

Constraints:

  • 1 <= num <= 3999
class Solution {
public:
    string intToRoman(int num) {
        unordered_map<int, char> map;
        map[1] = 'I';
        map[5] = 'V';
        map[10] = 'X';
        map[50] = 'L';
        map[100] = 'C';
        map[500] = 'D';
        map[1000] = 'M';
        string ans = "", result = "";
        int exp = 1, loops = 0;
        while (num > 0)
        {
            ans = "";
            exp *= 10;
            if (map.find(num % exp) != map.end())
            {
                ans += map.find(num % exp)->second;
            }
            else
            {
                if ((num % exp) / (exp / 10) == 9)
                {
                    ans += map.find(exp - (num % exp))->second;
                    ans += map.find(exp)->second;
                }
                else if ((num % exp) / (exp / 10) == 4)
                {
                    ans += map.find((exp / 2) - (num % exp))->second;
                    ans += map.find(exp / 2)->second;
                }
                else
                {
                    if ((num % exp) < (exp / 2))
                    {
                        loops = ((num % exp) / (exp / 10));
                    }
                    else
                    {
                        ans += map.find(exp / 2)->second;
                        loops = (((num % exp) - (exp / 2)) / (exp / 10));
                    }
                    for (int i = 0; i < loops; i++)
                    {
                        ans += (map.find(exp / 10)->second);
                    }
                }
            }
            num -= (num % exp);
            result = (ans + result);
        }
        return result;
    }
};

'Challenge' 카테고리의 다른 글

[LeetCode] Container With Most Water  (0) 2021.09.29
[LeetCode] Longest Common Prefix  (0) 2021.09.29
[LeetCode] Roman to Integer  (0) 2021.09.29
[Codility] CountDiv  (0) 2021.09.27
[Codility] Passing Cars  (0) 2021.09.27

+ Recent posts