Final Value of Variable After Performing Operations

🏠 ⬅️ ➡️

There is a programming language with only four operations and one variable X:

  • ++X and X++ increments the value of the variable X by 1.
  • --X and X-- decrements the value of the variable X by 1.

Initially, the value of X is 0.

Given an array of strings operations containing a list of operations, return the final value of X after performing all the operations.

Example 1:

Input: operations = [ "--X ", "X++ ", "X++ "] Output: 1 Explanation: The operations are performed as follows: Initially, X = 0. --X: X is decremented by 1, X = 0 - 1 = -1. X++: X is incremented by 1, X = -1 + 1 = 0. X++: X is incremented by 1, X = 0 + 1 = 1.

Example 2:

Input: operations = [ "++X ", "++X ", "X++ "] Output: 3 Explanation: The operations are performed as follows: Initially, X = 0. ++X: X is incremented by 1, X = 0 + 1 = 1. ++X: X is incremented by 1, X = 1 + 1 = 2. X++: X is incremented by 1, X = 2 + 1 = 3.

Example 3:

Input: operations = [ "X++ ", "++X ", "--X ", "X-- "] Output: 0 Explanation: The operations are performed as follows: Initially, X = 0. X++: X is incremented by 1, X = 0 + 1 = 1. ++X: X is incremented by 1, X = 1 + 1 = 2. --X: X is decremented by 1, X = 2 - 1 = 1. X--: X is decremented by 1, X = 1 - 1 = 0.

Constraints:

  • 1 <= operations.length <= 100
  • operations[i] will be either "++X ", "X++ ", "--X ", or "X-- ".

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer :: i, j, k
    character(len=4) :: op
    character(len=5), dimension(4) :: ops = ["++X ", "X++ ", "--X ", "X-- "]
    integer :: x = 0

    do i = 1, size(ops)
        read(ops(i), *) op
        select case (op)
            case ("++X ")
                x = x + 1
            case ("X++ ")
                x = x + 1
            case ("--X ")
                x = x - 1
            case ("X-- ")
                x = x - 1
        end select
    end do

    print '(I0)', x

end program main
Compiled
Executed
Correct
program main
      implicit none

      integer :: i, x, n
      character(len=5) :: operations(100)

      ! read the input
      read(*,*) n
      do i = 1, n
         read(*,*) operations(i)
      end do

      ! initialize x
      x = 0

      ! perform the operations
      do i = 1, n
         select case(operations(i))
            case('++X ')
               x = x + 1
            case('X++ ')
               x = x + 1
            case('--X ')
               x = x - 1
            case('X-- ')
               x = x - 1
         end select
      end do

      ! print the result
      write(*,*) x

      end program main
🌐 Data from online sources
def max_value(n: str, x: int) -> str:
    is_negative = n[0] == '-'
    pos = 1 if is_negative else 0

    while pos < len(n):
        if (is_negative and int(n[pos]) > x) or (not is_negative and int(n[pos]) < x):
            break
        pos += 1

    return n[:pos] + str(x) + n[pos:]

The algorithm starts by checking if the given integer n is negative or not, and initializes the position pos where we want to insert the integer x. It then iterates through the digits of n. For negative numbers, it checks to find the first digit that is greater than x. For positive numbers, it looks for the first digit that is smaller than x. If found, we break out of the loop.

Finally, we return the new string by concatenating the substring before the position pos, the digit x, and the substring after the position.

The algorithm has a complexity of O(n) where n is the length of the input string.

🌐 Data from online sources
#include <string>

std::string maxValue(std::string n, int x) {
    bool is_negative = n[0] == '-';
    int pos = is_negative ? 1 : 0;

    while (pos < n.size()) {
        if (is_negative && n[pos] - '0' > x || !is_negative && n[pos] - '0' < x) {
            break;
        }
        pos++;
    }

    return n.substr(0, pos) + std::to_string(x) + n.substr(pos);
}

The algorithm starts by checking if the given integer n is negative or not, and initializes the position pos where we want to insert the integer x. It then iterates through the digits of n. For negative numbers, it checks to find the first digit that is greater than x. For positive numbers, it looks for the first digit that is smaller than x. If found, we break out of the loop.

Finally, we return the new string by concatenating the substring before the position pos, the digit x, and the substring after the position.

The algorithm has a complexity of O(n) where n is the length of the input string.