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

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 a roman numeral, convert it to an integer.

 

Example 1:

Input: s = "III" Output: 3

Example 2:

Input: s = "IV" Output: 4

Example 3:

Input: s = "IX" Output: 9

Example 4:

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

Example 5:

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

 

Constraints:

  • 1 <= s.length <= 15
  • s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
  • It is guaranteed that s is a valid roman numeral in the range [1, 3999].
class Solution {
public:
    int romanToInt(string s) {
        unordered_map<char, int> map;
        map['I'] = 1;
        map['V'] = 5;
        map['X'] = 10;
        map['L'] = 50;
        map['C'] = 100;
        map['D'] = 500;
        map['M'] = 1000;
        int sum = 0;
        for (int i = 0; i < s.length(); i++)
        {
            if (map.find(s[i+1]) != map.end())
            {
                if (map.find(s[i+1])->second > map.find(s[i])->second)
                {
                    sum -= map.find(s[i])->second;
                }
                else
                {
                    sum += map.find(s[i])->second;
                }
            }
            else
            {
                sum += map.find(s[i])->second;
            }
        }
        return sum;
    }
};

'Challenge' 카테고리의 다른 글

[LeetCode] Longest Common Prefix  (0) 2021.09.29
[LeetCode] Integer to Roman  (0) 2021.09.29
[Codility] CountDiv  (0) 2021.09.27
[Codility] Passing Cars  (0) 2021.09.27
[LeetCode] Palindrome Number  (0) 2021.09.24

+ Recent posts