Day 27 — Baseball Game

Day 27 — Baseball Game

100 Days to Amazon

Image for post

Photo by Jose Morales on Unsplash

Out of Free Stories? Here is my** Friend Link.**

Introduction

Hey Guys, Today is day 27 **of the challenge that I took. Wherein I will be solving every day for 100 days the [programming questions** ](https://medium.com/hackernoon/50-data-structure-and-algorithms-interview-questions-for-programmers-b4b1ac61f5b0)that have been asked in** previous interviews.**

You have a bonus at the end if you keep reading. You can find out the companies that have asked these questions in real interviews.

All these problems are taken from the following [e-book_](https://www.amazon.com/dp/B081969QH5/ref=cm_sw_r_cp_apa_i_wqVZDbCVY8RV6). 🎓_

This is completely free 🆓 if you have an amazon kindle subscription.

This e-book contains 100 coding problems that have been asked in top tech interview questions. It also has a guide to solving all the problems in 200+ ways. **These problems I assure you** has been asked in previous interviews.

You have to decide whether you want to go** unprepared *for a tech interview or go ahead and **quick search for this guide *to solve the 100 problems.

Begin Your ascent to greatness

Day 27 — Baseball Game

AIM🏹

You’re now a baseball game point recorder.

Given a list of strings, each string can be one of the 4 following types:

  1. Integer (one round's score): Directly represents the number of points you get in this round.
  2. "+" (one round's score): Represents that the points you get in this round are the sum of the last two valid round's points.
  3. "D" (one round's score): Represents that the points you get in this round are the doubled data of the last valid round's points.
  4. "C" (an operation, which isn't a round's score): Represents the last valid round's points you get were invalid and should be removed.

Each round’s operation is permanent and could have an impact on the round before and the round after.

You need to return the sum of the points you could get in all the rounds.

Example

Input: ["5","2","C","D","+"]
Output: 30
Explanation: 
Round 1: You could get 5 points. The sum is: 5.
Round 2: You could get 2 points. The sum is: 7.
Operation 1: The round 2's data was invalid. The sum is: 5\.  
Round 3: You could get 10 points (the round 2's data has been removed). The sum is: 15.
Round 4: You could get 5 + 10 = 15 points. The sum is: 30.

Code👇

class Solution {
        public int calPoints(String[] ops) {
            Stack<Integer> s = new Stack();
        int res = 0;

            for(String str : ops) {
                if (str.equals("+")) {
                    int top = s.pop();
                    int newtop = top + s.peek();
                    s.push(top);
                    s.push(newtop);
                } else if (str.equals("C")) {
                    s.pop();
                } else if (str.equals("D")) {
                    s.push(2 * s.peek());
                } else {
                    s.push(Integer.valueOf(str));
                }
            }

            for(int total : s) res +=total;
            return res;
        }
    }

Author: Akshay Ravindran

Algorithm👨‍🎓

  1. Create a *stack *that will store the values after each operation.
  2. If the value is C. It is just a *pop() *operation.
  3. If the value is an integer we have to push *that *value *into the *Stack.
  4. If the operation *is *D. We will get the top value of the stack and multiply it by two and push it into the stack.
  5. If the operation is +. Remove the top element add the current top with the previous top element.
  6. Push this new value *onto the *Stack.
  7. When you *reach *the end of the array, Traverse through the stack.
  8. Adding each value of the stack.
  9. *Return *the total 🔚

programming coding java interview software-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

5 Core Criteria for Selecting Software Development Company - TopDevelopers.co

Check out these five criteria for the selection of your software vendor, and you will never regret having the wrong quality product made for you.

3 Best Programming Languages for Java Developers

In this article, I’ll share 3 JVM Programming languages Java programmers can learn and why you should learn them. Being a Polyglot developer is of good quality and often valued highly in interviews.

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

How to Prepare for a Coding Interview in 8 Weeks

As of this writing, the market is tough. We’ve been hit hard with a deadly pandemic that left thousands of people unemployed. It’s layoffs everywhere and the companies are being conservative when it comes to hiring.