# Daily Coding Challenge #12

This is a series of Daily Coding Challenge. Each day I show a few solutions written in C++. The questions are from coding practice/contest sites such as HackerRank, LeetCode, Codeforces, Atcoder and etc.

``````/*
LeetCode - Baseball Game

You're now a baseball game point recorder.

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

Integer (one round's score): Directly represents the number of points you get in this round.
"+" (one round's score): Represents that the points you get in this round are the sum of the last two valid round's points.
"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.
"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 1:
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.
Example 2:
Input: ["5","-2","4","C","D","9","+","+"]
Output: 27
Explanation:
Round 1: You could get 5 points. The sum is: 5.
Round 2: You could get -2 points. The sum is: 3.
Round 3: You could get 4 points. The sum is: 7.
Operation 1: The round 3's data is invalid. The sum is: 3.
Round 4: You could get -4 points (the round 3's data has been removed). The sum is: -1.
Round 5: You could get 9 points. The sum is: 8.
Round 6: You could get -4 + 9 = 5 points. The sum is 13.
Round 7: You could get 9 + 5 = 14 points. The sum is 27.
Note:
The size of the input list will be between 1 and 1000.
Every integer represented in the list will be between -30000 and 30000.
*/

class Solution {
public:
int calPoints(vector<string>& ops) {
stack<int> s;
int sum=0,point;
for(string ch:ops){
if(ch=="C"){
// "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.
// pop the last valid round's point
s.pop();
}else if(ch=="D") {
// "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.
// get the last valid round's point and multiply it by 2
point=s.top()*2;
s.push(point);
}else if(ch=="+"){
// "+" (one round's score): Represents that the points you get in this round are the sum of the last two valid round's points.
// e.g [5,10]
int a=s.top(); // get top which is 10
s.pop(); // pop 10
int b=s.top(); // get top which is 5
s.push(a); // push 10 back
s.push(a+b); // push the sum of last 2 valid round's points
// [5,10,15]
}else {
// Integer (one round's score): Directly represents the number of points you get in this round.
// convert it to integer and push it to the stack
s.push(stoi(ch));
}
}
// the ans is the sum of the values in the stack
while(!s.empty()) {
sum+=s.top();
s.pop();
}
return sum;
}
};

``````

``````/*
LeetCode - Contains Duplicate

Given an array of integers, find if the array contains any duplicates.

Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

Example 1:

Input: [1,2,3,1]
Output: true
Example 2:

Input: [1,2,3,4]
Output: false
Example 3:

Input: [1,1,1,3,3,4,3,2,4,2]
Output: true
*/

class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
// use set to remove duplicate values
unordered_set<int> s;
for(int n:nums) s.insert(n);
// if dulicate values exist, their sizes won't be same
return s.size()!=nums.size();
}
};
``````

``````/*
LeetCode - Contains Duplicate II

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.

Example 1:

Input: nums = [1,2,3,1], k = 3
Output: true
Example 2:

Input: nums = [1,0,1,1], k = 1
Output: true
Example 3:

Input: nums = [1,2,3,1,2,3], k = 2
Output: false
*/

class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map<int,int> m;
for(int i=0;i<nums.size();i++){
// use map to check if it is duplicate
if(m[nums[i]]){
// if so, check if the diff between two indexes is at most k
if(i-m[nums[i]]+1<=k){
return true;
}
}
// store i+1 to avoid if(0) case
m[nums[i]]=i+1;
}
return false;
}
};
``````

The source code is available in corresponding repo below. Star and watch for timely updates!

## wingkwong / atcoder

### Discussion   