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

Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function).

The algorithm for myAtoi(string s) is as follows:

  1. Read in and ignore any leading whitespace.
  2. Check if the next character (if not already at the end of the string) is '-' or '+'. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present.
  3. Read in next the characters until the next non-digit charcter or the end of the input is reached. The rest of the string is ignored.
  4. Convert these digits into an integer (i.e. "123" -> 123, "0032" -> 32). If no digits were read, then the integer is 0. Change the sign as necessary (from step 2).
  5. If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then clamp the integer so that it remains in the range. Specifically, integers less than -231 should be clamped to -231, and integers greater than 231 - 1 should be clamped to 231 - 1.
  6. Return the integer as the final result.

Note:

  • Only the space character ' ' is considered a whitespace character.
  • Do not ignore any characters other than the leading whitespace or the rest of the string after the digits.
class Solution {
public:
    int64_t myAtoi(string s) {
        int64_t ans = 0;
        int start = -1;
        bool neg = false;
        map<int8_t, bool> operators;
        for (int i = 0; i < s.length(); i++)
        {
            if (s[i] >= 48 && s[i] <= 57)
            {
                ans *= 10;
                ans += (int64_t)((int)s[i] - 48);
                if (ans > (int64_t)pow(2, 31))
                {
                    ans = (int64_t)pow(2, 31);
                    break;
                }
                if (start == -1) start = i;
            }
            else
            {
                if (s[i] != 32 && s[i] != 43 && s[i] != 45) break;
                if (i > 0 && (s[i-1] >= 48 && s[i-1] <= 57)) break;
                if (s[i] == 43) operators[i] = true;
                if (s[i] == 45) operators[i] = false;
                if (operators.size() > 1) break;
            }
            if (start != -1 && operators.size() > 0 && \
                abs(std::prev(operators.end())->first - start) > 1)
            {
                ans = 0;
                break;
            }
        } 
        if (ans > 0 && operators.size() > 0)
        {
            if (abs(std::prev(operators.end())->first - start) == 1)
            {
                if (std::prev(operators.end())->second == false) ans *= -1;
            }
        }
        return (ans == (int64_t)pow(2, 31) ? --ans : ans);
    }
};

'Challenge' 카테고리의 다른 글

[Codility] Passing Cars  (0) 2021.09.27
[LeetCode] Palindrome Number  (0) 2021.09.24
[LeetCode] Reverse Integer  (0) 2021.09.14
[LeetCode] ZigZag Conversion  (0) 2021.09.13
Check for pair with a given sum  (0) 2021.09.13

+ Recent posts