xdoer
BAN USERA little verbose, but hopefully it is easier to understand.
public static StringBuilder buildOutput(StringBuilder bdr, char target,
int rep) {
bdr.append(target);
if (rep > 0) {
bdr.append(Integer.toString(rep + 1));
}
return bdr;
}
public static String compressString(String s) {
StringBuilder buf = new StringBuilder();
if (s == null || s.length() < 2) {
return s;
}
char cur = s.charAt(0);
char pre = cur;
int rep = 0;
for (int i = 1; i < s.length(); i++) {
cur = s.charAt(i);
if (cur == pre) {
rep++;
} else {
buf = buildOutput(buf, pre, rep);
pre = cur;
rep = 0;
}
}
buf = buildOutput(buf, pre, rep);
return buf.toString();
}
With 2 eggs, with a modified version of helen2211's method, you can get the worst tries to 50 times.
Drop first egg from the 50 floor
if(first breaks){
start dropping second egg from 1st floor
// You may find egg breaks on the first floor.
// worst case, max 50 tries
}else{
start dropping the second egg from 51st going to 100th floor
// max 50 tries
}
// If listB is not sorted, sort listB in ascending order before calling
// this method.
public static void removeFromListA(LinkedList<Integer> listA, int[] listB){
int pre = 0;
for(int i=0; i<listB.length; i++){
int index = listB[i]-pre;
if(index >= 0 && index < listA.size()){
listA.remove(index);
}
pre = pre+1;
}
}
public static void testRemoveFromListA(){
LinkedList<Integer> listA = new LinkedList<Integer>();
listA.add(2);
listA.add(3);
listA.add(5);
listA.add(7);
listA.add(8);
int[] listB = {0,1,2,3,4,5};
// expected result of listA [2,5,8];
removeFromListA(listA, listB);
System.out.println("listA after removal operation: " + listA);
}
We can think of this as equivalent to: There is a 20 GB file full of unsigned integers, how to produce a file that has no duplicates?
1. Break this large file into 40 500 MB files
2. Sort the integer in each file, modify whatever sorting you are doing to eliminate duplicates and print them.
3. Merge these 400 files, (like merging 400 arrays), while merging, eliminate and print duplicates.
Repgladyskcombs, Personnel at Green Bricks
Hello, I am Gladys. I am a Industrial Photographer. I started off my photographic career as a News photographer based ...
Repalicesreedg, Accountant at ADP
Hi my name is Alice and i am working in an IT company. I am working here from last 5 ...
Repnancyhfloress, Computer Scientist at AMD
Hey there, I’m Nancy. I’m a small business owner living in Sunrise, FL 33323. I am a fan ...
Are we assuming that Input string A is always sorted? If not, this algorithm will not work.
- xdoer March 05, 2013