Bounded Counter Class

Problem

A counter has a smallest value LOW, a largest value HIGH and a current value. The first line has LOW HIGH START. Commands: inc K (add K, but never go above HIGH), dec K (subtract K, but never go below LOW), reset (go back to LOW), show (print the current value).

Constraints

  • LOW <= START <= HIGH
  • 1 <= commands <= 100

Examples

Input

0 10 5
inc 100
show

Output

10

Solutions

Best approachTime O(n)Space O(1)

The limits are stored inside the object and applied in every change method, so the rule is written once.

Same code works in Java810172125Basic features only, so nothing changes between versions.
/*
 * Problem : Run the commands.
 * Input   : Command lines.
 * Output  : One line per answer.
 * Approach: Counter class
 * Time    : O(n)
 * Space   : O(1)
 * Java    : 8 or newer.
 */
import java.util.Scanner;

public class Main {
        // Step 1: The class owns the limits. Nobody outside can put the value out of range.
        static class Counter {
            private final long low;
            private final long high;
            private long value;

            Counter(long low, long high, long start) {
                this.low = low;
                this.high = high;
                this.value = start;
            }

            // Step 2: Each change is cut back to the allowed range.
            void inc(long k) {
                value = Math.min(high, value + k);
            }

            void dec(long k) {
                value = Math.max(low, value - k);
            }

            void reset() {
                value = low;
            }

            long get() {
                return value;
            }
        }

    // This method solves the task. It receives the input and returns the answer.
    static String[] solve(String[] lines) {
        String[] head = lines[0].split(" ");
        Counter counter = new Counter(Long.parseLong(head[0]), Long.parseLong(head[1]), Long.parseLong(head[2]));
        java.util.List<String> out = new java.util.ArrayList<String>();
        for (int i = 1; i < lines.length; i++) {
            String[] part = lines[i].split(" ");
            if (part[0].equals("inc")) {
                counter.inc(Long.parseLong(part[1]));
            } else if (part[0].equals("dec")) {
                counter.dec(Long.parseLong(part[1]));
            } else if (part[0].equals("reset")) {
                counter.reset();
            } else {
                out.add(String.valueOf(counter.get()));
            }
        }
        return out.toArray(new String[0]);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        java.util.List<String> all = new java.util.ArrayList<>();
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine().trim();
            if (!line.isEmpty()) {
                all.add(line);
            }
        }
        String[] lines = all.toArray(new String[0]);
        System.out.println(String.join("\n", solve(lines)));
    }
}
Hints (1)
  1. Where is the range rule written?