注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
all numbers in +ve ints random, no duplicates.
turn: we pick one card, read value , suppose it's x, we delete x, x+1 and x-1 from the cards array if they exist.
repeat turns till no card is left.
1 1 1 1 1
5,7,8,12,13
1->3,4->5->7
O(n)->N*O(N)
//greedy algorithm, x
//brute force:iteration.->x that has no neighbor
SCAN
//greedy algorithm
int maxTurns(int[] cards) {
Arrays.sort(cards);
int n=cards.length;
//record which has been deleted
boolean[] bt = new boolean[n];
Arrays.fill(bt,true);
int counter=0;
int maxTurn =0;
while(counter<n){
maxTurn++;
//scan from left to right to find the best choice for this turn.
int minTimes=0;
int minIndex = 0;
t>
n/=10;
length++;
}
//length,upper,lower
if(length<lower){
return 0;
}else if(length<=upper){//n=999 lower=2,upper=3 10-299
return n-(Math.pow(10,lower-1)-1);
}else{//length>upper n=1m,lower=2,upper=3 10-99
return (Math.pow(10,upper)-1)-(Math.pow(10,lower-1)-1);
}
}
坑爹的阿三面的,说话完全听不懂,各种催毛求疵,。。。。,不过楼主第一题代码是有点问题就是了,复杂度太高,
|