feistc
BAN USERpublic class Coefficients {
public a;
public b;
}
public static Coefficients simplifyEq(String equation) {
if (equation == null || equation.length == 0) {
throw new IllegalArguementException();
}
int yPrefix;
int a;
int b;
int multiplier = 1;
int current;
for(int index; index < equation.length; index ++) {
char c = equation.charAt(index);
if (c == '+') {
b += multiplier * current;
current = 0;
multiplier = 1;
continue;
} else if (c == '-') {
b += multiplier * current;
current = 0;
multipiler = -1;
continue;
}
if (c == 'y') {
yPrefix += multiplier * current;
current = 0;
continue;
} else if (c == 'x') {
a += multiplier * current;
current = 0;
continue;
}
// if c is int
switch (c) {
case '0':
case '1':
case '9':
current = current * 10 + convertCharToInt(c);
break;
default:
throw new IllegalArguementException();
}
}
Coefficients coef = new Coefficients();
coef.a = a / yPrefix;
coef.b = b / yPrefix;
return coef;
}
public static int getFullestDay(List<Schedule> schedules) {
if (schedules == null || schedules.size() == 0) {
return -1;
}
final HashMap<Integer, Integer> dayAndCapacity = new HashMap<>();
for (Pair<Integer, Integer> schedule : schedules) {
for (int day = schedule.check-in; day <= schedule.check-out; day ++) {
dayAndCapacity.put(day, dayAndCapacity.get(day) + 1);
}
}
int fullestDay = 0;
int highestCapacity = 0;
for (Entry<Integer, Integer> entry : dayAndCapacity.entrySet()) {
if (entry.getValue() > highestCapacity) {
highestCapacity = entry.getValue();
fullestDay = entry.getKey();
}
}
return highestDay;
}
public class Schedule {
public int check-in;
public int check-out;
}
public static boolean isSeatingAvail(int[] row, int seatsRequests) {
if (row == null || row.length == 0 || seatsRequests < 0) {
return false;
}
final int openSeatsNeeded = seatsRequested + 2;
int openSeats = 1; // Start with one for first seat
for (int seat : row) {
if (seat == 0) {
openSeats ++;
} else {
openSeats = 0;
}
if (openSeats >= openSeatsNeeded) {
return true;
}
}
// Add one for end seat and recheck
openSeats ++;
if (openSeats >= openSeatsNeeded) {
return true;
}
return false;
}
public String getLongestDistinctString(String s, int k) {
if (s == null || k <= 0) {
return null;
}
final HashMap uniqueChars = new HashMap();
String lastString;
String longestString = "";
char currentChar;
for (int index = 0; index < s.length(); index ++) {
currentChar = s.charAt(index);
if (!uniqueChars.contains(currentChar) && uniqueChars.size() >= k) {
if (longestString.length() < lastString.lenght()) {
longestString = lastString;
}
char charToRemove;
int indexOfCharToRemove = lastString.length();
for (Entry entry : uniqueChars.entrySet) {
if (entry.getValue() < indexOfCharToRemove) {
indexOfCharToRemove = entry.getValue();
charToRemove = entry.getKey()
}
}
uniqueChars.remove(charToRemove);
final int newStartIndex = indexOfCharToRemove + 1;
lastString = lastString.subString(newStartIndex);
// Reset the indexes of the uniqueChars
for (Entry entry : uniqueChars.entrySet) {
entry.setValue(entry.getValue() - newStartIndex);
}
}
uniqueChars.put(char, lastString.length())
lastString += currentChar
}
return longestString;
}
To swap it in place:
- feistc May 14, 2017