paizaラーニング解答: スタック・キューメニュー[Ruby]

# スタック・キューメニュー
https://paiza.jp/works/mondai/stack_queue/problem_index?language_uid=ruby
## STEP: 1 スタック・キュー実装編( 共通問題 ) step 1
```ruby
n = gets.to_i
a = n.times.map { gets.to_i }
puts n
puts a
```

## FINAL問題 スタック・キュー実装編( 共通問題 ) step 2
```ruby
a = []
q = gets.to_i
query = q.times.map { gets.split.map(&:to_i) }
query.each do |que|
    if que[0] == 1
        a << que[1]
    end
end
puts a.length
puts a
```

## STEP: 1 スタック実装編 step 1
```ruby
a = []
q = gets.to_i
query = q.times.map { gets.split.map(&:to_i) }
query.each do |que|
    if que[0] == 1
        a << que[1]
    else
        a.pop
    end
    puts a.join(' ')
end
```
popメソッドで配列の後ろを取り除ける。
## FINAL問題 スタック実装編 step 2
```ruby
a = []
q = gets.to_i
query = q.times.map { gets.chomp.split }
query.each do |que|
    if que[0].to_i == 1
        a << que[1]
    else
        puts a.pop
    end
    puts a.join(' ')
end
```

## STEP: 1 キュー実装編 step 1
```ruby
a = []
q = gets.to_i
query = q.times.map { gets.split.map(&:to_i) }
query.each do |que|
    if que[0] == 1
        a << que[1]
    else
        a.shift
    end
    puts a.join(' ')
end
```

## FINAL問題 キュー実装編 step 2
```ruby
a = []
q = gets.to_i
query = q.times.map { gets.chomp.split }
query.each do |que|
    if que[0] == "1"
        a << que[1]
    else
        puts a.shift
    end
    puts a.join(' ')
end
```

## STEP: 1 2 つのキュー
```ruby
queue1 = []
queue2 = []
q = gets.to_i
query = q.times.map { gets.chomp.split }
query.each do |que|
    if que[0] == "1" && que[1] == "1"
        queue1 << que[2]
    elsif que[0] == "1" && que[1] == "2"
        queue2 << que[2]
    elsif que[0] == "2" && que[1] == "1"
        puts queue1.shift
    elsif que[0] == "2" && que[1] == "2"
        puts queue2.shift
    else
        puts "#{queue1.length} #{queue2.length}"
    end
end
```

## STEP: 2 最大の区間和
```ruby
n, x = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)

left_num = a[0]
max_sum = a.take(x).sum
section = max_sum

(n - x).times do |i|
    section -= a[i]
    section += a[i + x]
    if section > max_sum
        max_sum = section
        left_num = a[i + 1]
    end
end
puts "#{max_sum} #{left_num}"
```
takeメソッドで先頭から引数のまでの配列を取得
## STEP: 3 逆ポーランド記法
```ruby
n = gets.to_i
a = gets.chomp.split
stack = []
n.times do |i|
    if a[i] == "+"
        x = stack.pop
        y = stack.pop
        stack << y + x
    elsif a[i] == "-"
        x = stack.pop
        y = stack.pop
        stack << y - x
    else
        stack << a[i].to_i
    end
end
puts stack
```

## STEP: 4 括弧列
```ruby
n = gets.to_i
a = gets.chomp.split('')
stack = []
n.times do |i|
    if !stack.empty? && stack[-1] == "(" && a[i] == ")"
        stack.pop
    else
        stack << a[i]
    end
end
puts stack.empty? ? "Yes" : "No"
```

## STEP: 5 エスカレーター
```ruby
n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
queue = [0] * k
(1..a[-1]).each do |i|
    queue.shift
    if i == a[0]
        a.shift
        queue << 1
        puts queue.count(1)
    else
        queue << 0
    end
end
```

## FINAL問題 箱とボール
```ruby
n = gets.to_i
a = gets.split.map(&:to_i)
stack = [a[0]]
(1...n).each do |i|
    stack << a[i]
    top_1 = stack.pop
    top_2 = stack.pop
    while top_1 == top_2
        stack << top_1 * 2
        top_1 = stack.pop
        top_2 = stack.pop
    end
    stack << top_2
    stack << top_1
end
puts stack.reverse
```