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

Write a function:

int solution(int A, int B, int K);

that, given three integers A, B and K, returns the number of integers within the range [A..B] that are divisible by K, i.e.:

{ i : A ≤ i ≤ B, i mod K = 0 }

For example, for A = 6, B = 11 and K = 2, your function should return 3, because there are three numbers divisible by 2 within the range [6..11], namely 6, 8 and 10.

Write an efficient algorithm for the following assumptions:

  • A and B are integers within the range [0..2,000,000,000];
  • K is an integer within the range [1..2,000,000,000];
  • A ≤ B.
int solution(int A, int B, int K) {
    // write your code in C++14 (g++ 6.2.0)
    int ans = (B / K) - (A / K);
    return (A % K == 0 ? ans + 1 : ans);
}

'Challenge' 카테고리의 다른 글

[LeetCode] Integer to Roman  (0) 2021.09.29
[LeetCode] Roman to Integer  (0) 2021.09.29
[Codility] Passing Cars  (0) 2021.09.27
[LeetCode] Palindrome Number  (0) 2021.09.24
[LeetCode] String to Integer (atoi)  (0) 2021.09.24

A non-empty array A consisting of N integers is given. The consecutive elements of array A represent consecutive cars on a road.

Array A contains only 0s and/or 1s:

  • 0 represents a car traveling east,
  • 1 represents a car traveling west.

The goal is to count passing cars. We say that a pair of cars (P, Q), where 0 ≤ P < Q < N, is passing when P is traveling to the east and Q is traveling to the west.

For example, consider array A such that:

A[0] = 0 A[1] = 1 A[2] = 0 A[3] = 1 A[4] = 1

We have five pairs of passing cars: (0, 1), (0, 3), (0, 4), (2, 3), (2, 4).

Write a function:

int solution(vector<int> &A);

that, given a non-empty array A of N integers, returns the number of pairs of passing cars.

The function should return −1 if the number of pairs of passing cars exceeds 1,000,000,000.

For example, given:

A[0] = 0 A[1] = 1 A[2] = 0 A[3] = 1 A[4] = 1

the function should return 5, as explained above.

Write an efficient algorithm for the following assumptions:

  • N is an integer within the range [1..100,000];
  • each element of array A is an integer that can have one of the following values: 0, 1.
#define MAX_VAL 1000000000

int solution(vector<int> &A) {
    // write your code in C++14 (g++ 6.2.0)
    int eastCount = 0, sum = 0;
    for (int i = 0; i < A.size(); i++)
    {
        if (A[i] == 0)
        {
            eastCount++;
        }
        else if (A[i] == 1)
        {
            sum += eastCount;
            if (sum > MAX_VAL) return -1;
        }
    }
    return sum;
}

'Challenge' 카테고리의 다른 글

[LeetCode] Roman to Integer  (0) 2021.09.29
[Codility] CountDiv  (0) 2021.09.27
[LeetCode] Palindrome Number  (0) 2021.09.24
[LeetCode] String to Integer (atoi)  (0) 2021.09.24
[LeetCode] Reverse Integer  (0) 2021.09.14

Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward. For example, 121 is palindrome while 123 is not.

 

Example 1:Input: x = 121 Output: true

Example 2:

Input: x = -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: x = 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Example 4:

Input: x = -101 Output: false

 

Constraints:

  • -231 <= x <= 231 - 1
class Solution {
public:
    bool isPalindrome(int x) {
        string s = to_string(x);
        int m = floor((s.length() + 1) / 2);
        m = (s.length() % 2 == 0 ? m : m - 1);
        for (int i = 0; i < m; i++)
        {
            if (s[i] != s[(s.length() - 1) - i]) return false;
        }
        return true;
    }
};

 

'Challenge' 카테고리의 다른 글

[Codility] CountDiv  (0) 2021.09.27
[Codility] Passing Cars  (0) 2021.09.27
[LeetCode] String to Integer (atoi)  (0) 2021.09.24
[LeetCode] Reverse Integer  (0) 2021.09.14
[LeetCode] ZigZag Conversion  (0) 2021.09.13

+ Recent posts