Bracket Sequence: Difference between revisions

From programming_contest
Jump to navigation Jump to search
imported>Ajg51
Created page with "==Introduction== You are given n servers each of which has a number of tasks. The goal is to have all n servers have the same number of tasks. In cases where the number of tot..."
 
imported>Kmk21
No edit summary
 
(4 intermediate revisions by one other user not shown)
Line 1: Line 1:
==Introduction==
==Introduction==
You are given n servers each of which has a number of tasks. The goal is to have all n servers have the same number of tasks. In cases where the number of total tasks is not divisible by the number of servers, the number of tasks between any servers can have a maximum range of 1. You can move a single task from one server to another every second. Write a program that finds the minimum number of seconds before the servers are balanced. 
You are given a string consisting of the following bracket types: <>, {}, [], (). There are open and close brackets. For example, an open bracket is < and { while a close bracket is > and }. You want to know if the string given is a regular bracket sequence (RBS). A regular bracket sequence is a sequence of brackets such that brackets of the same type open then close, and within that opening and closing, there can be other sequences of brackets that also open and close. It is similar to how in math and in coding, brackets are opened and can have nested brackets within them before being closed. An empty string is also RBS.
 
Examples of RBS:
< { ( [ ] ) } >
<>{ } [ <> ( ) ]
 
Examples of NOT RBS:
< { > }
<>{ } > <


==Solution==
==Solution==
===Idea===
===Idea===
The naive solution would be to move a single task from the most busy server to least busy server each second until the load is balanced. This solution does not finish in time (complexity is O(servers*tasks))
It makes sense to use a stack to solve this problem because the last bracket opened needs to be closed before any of the brackets before that one. So, the general idea is to: <br>
 
1. iterate through the string and add if we detect an open bracket, we push the same type's close bracket <br>
A better solution would be to calculate the average number of tasks (desired number of output tasks), and either add or remove tasks to/from a server. If the average rounds up, a number of servers (the "rounded" servers) have to add 1 to their tasks. If the average rounds down, a number of servers have to subtract 1 from their tasks. This ensures the correct answer in cases where the average number of tasks is not an integer. The number of seconds can then be calculated by adding together the difference between each servers' number of tasks and the rounded average. The answer is this number divided by 2 (because we need half the difference).  
2. If we detect a character to be a close bracket, we first check if the stack is empty. If this is the case, we have no open brackets that need to be closed which means there is a close bracket without an open bracket, which is not RBS. <br>
3. If the stack is not empty, we move on to peek at the top of the stack to check if the close bracket's type matches the last pushed open bracket's type. <br>
4. If it is the same type, we do not need to replace the bracket so we simply pop the stack. <br>
5. If the close bracket is a different type, we would have to replace it to match the open bracket type, so we increment the number of replaces then pop the stack. <br>


===Runtime===
===Runtime===
Worst case runtime is O(servers).
Worst case runtime is O(length of input string).


===Code===
===Code===
Line 95: Line 106:
[[Category:Algorithm Easy]]
[[Category:Algorithm Easy]]
[[Category:Implementation Easy]]
[[Category:Implementation Easy]]
[[Category:Stack]]

Latest revision as of 06:00, 24 August 2016

Introduction

You are given a string consisting of the following bracket types: <>, {}, [], (). There are open and close brackets. For example, an open bracket is < and { while a close bracket is > and }. You want to know if the string given is a regular bracket sequence (RBS). A regular bracket sequence is a sequence of brackets such that brackets of the same type open then close, and within that opening and closing, there can be other sequences of brackets that also open and close. It is similar to how in math and in coding, brackets are opened and can have nested brackets within them before being closed. An empty string is also RBS.

Examples of RBS: < { ( [ ] ) } > <>{ } [ <> ( ) ]

Examples of NOT RBS: < { > } <>{ } > <

Solution

Idea

It makes sense to use a stack to solve this problem because the last bracket opened needs to be closed before any of the brackets before that one. So, the general idea is to:
1. iterate through the string and add if we detect an open bracket, we push the same type's close bracket
2. If we detect a character to be a close bracket, we first check if the stack is empty. If this is the case, we have no open brackets that need to be closed which means there is a close bracket without an open bracket, which is not RBS.
3. If the stack is not empty, we move on to peek at the top of the stack to check if the close bracket's type matches the last pushed open bracket's type.
4. If it is the same type, we do not need to replace the bracket so we simply pop the stack.
5. If the close bracket is a different type, we would have to replace it to match the open bracket type, so we increment the number of replaces then pop the stack.

Runtime

Worst case runtime is O(length of input string).

Code

Solution - Java

import java.util.*;
/**
 * Created by alanguo on 2/29/16.
 */
public class bracketSequence {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String input = in.next();
        int num = 0;
        Stack<Character> s = new Stack<Character>();
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c == '}') {
                if(s.empty()) {
                    System.out.println("Impossible");
                    return;
                }
                if (s.peek() == c) {
                    s.pop();
                } else {
                    s.pop();
                    num++;
                }
            } else if (c == '>') {
                if(s.empty()) {
                    System.out.println("Impossible");
                    return;
                }
                if (s.peek() == c) {
                    s.pop();
                } else {
                    s.pop();
                    num++;
                }
            } else if (c == ']') {
                if(s.empty()) {
                    System.out.println("Impossible");
                    return;
                }
                if (s.peek() == c) {
                    s.pop();
                } else {
                    s.pop();
                    num++;
                }
            } else if (c == ')') {
                if(s.empty()) {
                    System.out.println("Impossible");
                    return;
                }
                if (s.peek() == c) {
                    s.pop();
                } else {
                    s.pop();
                    num++;
                }
            } else if (c=='{') {
                s.push('}');
            } else if (c=='[') {
                s.push(']');
            } else if (c=='<') {
                s.push('>');
            } else if (c=='(') {
                s.push(')');
            }

        }
        if(!s.empty()) {
            System.out.println("Impossible");
            return;
        }
        System.out.println(num);
        return;
    }
}