class ParkingSystem {
static int big = 0;
static int medium = 0;
static int small = 0;
public ParkingSystem(int big1, int medium1, int small1) {
big = big1;
medium = medium1;
small = small1;
}
public boolean addCar(int carType) {
if(carType == 1){
if(big >= 1){
big--;
return true;
}else return false;
}else if(carType == 2){
if(medium >= 1){
medium--;
return true;
} else return false;
}else if (carType == 3){
if(small >= 1){
small--;
return true;
}else return false;
}
return false;
}
}
/**
* Your ParkingSystem object will be instantiated and called as such:
* ParkingSystem obj = new ParkingSystem(big, medium, small);
* boolean param_1 = obj.addCar(carType);
*/
Category Archives: Computer Science
2114. Maximum Number of Words Found in Sentences
2114. Maximum Number of Words Found in Sentences
class Solution {
public int mostWordsFound(String[] sentences) {
int max = Integer.MIN_VALUE;
for(int i = 0; i < sentences.length; i++){
max = Math.max(max, sentences[i].split(" ").length);
}
return max;
}
}
1492. The kth Factor of n
class Solution {
public int kthFactor(int n, int k) {
for(int i = 1 ; i <= n; i++){
if((n%i) ==0 ){
if( k == 1) return i;
k--;
}
}
return -1;
}
}
1491. Average Salary Excluding the Minimum and Maximum Salary
1491. Average Salary Excluding the Minimum and Maximum Salary
class Solution {
public double average(int[] salary) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
double sum = 0;
for(int i = 0; i < salary.length; i++){
sum+=salary[i];
min = Integer.min(min,salary[i]);
max = Integer.max(max, salary[i]);
}
return (sum-min-max)/(salary.length - 2);
}
}
1523. Count Odd Numbers in an Interval Range
1523. Count Odd Numbers in an Interval Range
class Solution {
public int countOdds(int low, int high) {
int count = (high-low)/2;
if(high%2 ==0 && low%2 == 0){}
else count +=1;
return count;
}
}
414. Third Maximum Number
290. Word Pattern
class Solution {
public boolean wordPattern(String pattern, String s) {
HashMap<Character,String> map = new HashMap<>();
String[] sWords = s.split(" ");
if(sWords.length != pattern.length()) return false;
for(int i = 0; i < pattern.length(); i++){
for (Map.Entry<Character, String> entry : map.entrySet()) {
if(entry.getValue().equals(sWords[i]) && entry.getKey() != pattern.charAt(i)) return false;
if(!entry.getValue().equals(sWords[i]) && entry.getKey() == pattern.charAt(i)) return false;
}
map.put(pattern.charAt(i),sWords[i]);
}
return true;
}
}
Udacity – Angular Development Cross Skilling
Omar GamalEldeen
has successfully completed the
Angular Development Cross Skilling Nanodegree program
offered by Udacity
Certificate link:
Appian Certified Associate Developer
Balanced Brackets
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'isBalanced' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/
public static String isBalanced(String s) {
// Write your code here
Stack<Character> stack = new Stack<Character>();
for(int i = 0; i < s.length(); i++){
if("{([".contains(s.charAt(i)+"")) stack.push(s.charAt(i));
else if(s.charAt(i) == ']'){
if(stack.isEmpty()) return "NO";
if(stack.peek() != '[') return "NO";
stack.pop();
}
else if(s.charAt(i) == '}'){
if(stack.isEmpty()) return "NO";
if(stack.peek() != '{') return "NO";
stack.pop();
}
else if(s.charAt(i) == ')'){
if(stack.isEmpty()) return "NO";
if(stack.peek() != '(') return "NO";
stack.pop();
}
}
if(!stack.isEmpty()) return "NO";
return "YES";
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int t = Integer.parseInt(bufferedReader.readLine().trim());
IntStream.range(0, t).forEach(tItr -> {
try {
String s = bufferedReader.readLine();
String result = Result.isBalanced(s);
bufferedWriter.write(result);
bufferedWriter.newLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
bufferedReader.close();
bufferedWriter.close();
}
}
