Answering myself- \n seems to mean that the folder is on the first layer, so a\n\tb.txt\na2\n\tb2.txt is
a
[blank and tabs] b.txt
a2
[blank and tabs] b2.txt
it would be helpful if this example can be explained in the problem description...
Answering myself- \n seems to mean that the folder is on the first layer, so a\n\tb.txt\na2\n\tb2.txt is
a
[blank and tabs] b.txt
a2
[blank and tabs] b2.txt
it would be helpful if this example can be explained in the problem description...
I'm wondering what's the expected answer for these test cases involving \n and spaces? The problem itself didn't mention how to deal with single "\n". This seems to be confusing to me
file name with space.txt = 25 // so spaces are part of file name and should be counted
dir\n file.txt = 12 // i suppose it's 4 spaces + file.txt ? how about dir here? why it's not included?
dir\n file.txt = 16 // i suppose it's 8 spaces + file.txt? same question as above
a\n\tb.txt\na2\n\tb2.txt=9 // why? what's a2 here? maybe it's a/b.txta2 but i dont see how \n is handled here
Please advise
Because you can sump up both jugs to reach the target....the traditional problem only allows one jug to match
Because of double precision, below should work. +0.5 to make it round to the closest int
public boolean isPowerOfThree(int n) {
double x = Math.log(n) / Math.log(3);
double diff = Math.abs(x -(int)(x+0.5));
return diff<=0.000001;
}
public class Solution {
private List<String> r = new ArrayList<>();
public List<String> generateAbbreviations(String word) {
if(word==null){
return r;
}
StringBuilder cur = new StringBuilder();
doGenerate(word,0,cur,-1);
return r;
}
// separate current string and trailing number, number==-1 means no trailing number
private void doGenerate(String word, int i, StringBuilder cur,int number) {
if(i==word.length()){
if(number==-1) {
r.add(cur.toString());
}else{
r.add(cur.toString()+String.valueOf(number));
}
return;
}
int oldLen = cur.length();
// case one, add char itself. append the trailing number, then the char
if(number!=-1) {
cur.append(number);
}
cur.append(word.charAt(i));
doGenerate(word,i+1,cur,-1);
cur.delete(oldLen,cur.length());
// case two add number, either combine with the previous number
if(number!=-1){
doGenerate(word,i+1,cur,number+1);
}else{
// case three, add the first "1" after a char
doGenerate(word,i+1,cur,1);
}
}
private boolean isNumber(char c) {
return c>='0' && c<='9';
}
}
Thanks that's corrected
This is equivalent to finding the first occurence of a given number in sorted array - binary search is the answer. Each time when we find the "number" (isBadVersion), we treat it as a "bigger than target" and keep moving mid until low and high pointers (l and u cross). The only difference from traditional binary search is we keep a "last" pointer to the last found one.
public class Solution extends VersionControl {
// binary search find the first occurence of some num
public int firstBadVersion(int n) {
if(n<=0){
return n;
}
int l = 0;
int u = n-1;
int last = -1;
while(l<=u){
int mid = l+(u-l)/2;
if(!isBadVersion(mid+1)){
l = mid+1;
}else{
u = mid-1;
last = mid;
}
}
return last+1;
}
}
According to that wikipedia page, f maps index to value, we reverse sort results of f, then h-index (f) = max(min (f(i), i))
public class Solution {
public int hIndex(int[] c) {
if (c == null) {
return 0;
}
List<Integer> rsorted = Arrays.stream(c).mapToObj(i->Integer.valueOf(i)).sorted(Collections.reverseOrder()).collect(Collectors.toList());
return IntStream.range(0,rsorted.size()).reduce(0,(hindex,curindex)->Math.max(hindex,Math.min(curindex+1,rsorted.get(curindex))));
}
}
Thanks for sharing. I think the key here is as you mentioned, "including nums with same value.". factor2 and factor3 may both have value = 6, but we bump both "6"s together, thus the duplicated 6 won't cause any problem. I initially put it as "else if (factor3==min)", that fell to the trap :)
Functional programming makes the code quite concise and without boilerplate code
public class Solution {
public boolean canAttendMeetings(Interval[] intervals) {
if(intervals == null || intervals.length==0){
return true;
}
Arrays.sort(intervals, (x,y)->x.start-y.start);
return !IntStream.range(1, intervals.length).anyMatch(i->intervals[i].start<intervals[i-1].end);
}
}