First I want to mention that I am not very expert in programming. I am trying to solve the repeated permutation problem but I dont understand why i always get time limit. My code is in java and I am using BigInteger for factorial and other calculation.
please help me to find out what is the reason i got time limit. I found same approach to solve the problem but that was in python. Here I provided my code. Thanks in advance.
import java.math.BigInteger;
public class Solution {
BigInteger factorial(int n){
BigInteger sum=new BigInteger(String.valueOf(1));
if(n<2) return new BigInteger(String.valueOf(n));
for(int j=2;j<=n;j++){
sum=sum.multiply(new BigInteger(String.valueOf(j)));
}
return sum;
}
public int findRank(String a) {
if(a.length()<2) return 1;
Map<Character,Integer> map1=new HashMap<Character,Integer>();
BigInteger sum=new BigInteger(String.valueOf(1));
for(int i=0;i<a.length();i++){
if(!map1.containsKey(a.charAt(i))){
map1.put(a.charAt(i),1);
}
else{
int cc=map1.get(a.charAt(i));
cc=cc+1;
map1.put(a.charAt(i),cc);
}
}
BigInteger temp1=new BigInteger(String.valueOf(1));
for (Map.Entry<Character, Integer> entry : map1.entrySet())
{
temp1=temp1.multiply(new BigInteger(String.valueOf(factorial( entry.getValue()))));
}
temp1=temp1.pow(1000001);
temp1=temp1.mod(new BigInteger(String.valueOf(1000003)));
for(int i=0;i<a.length();i++){
BigInteger rank=new BigInteger(String.valueOf(0));
for(int j=i+1;j<a.length();j++){
if(a.charAt(i)>a.charAt(j)){
rank=rank.add(new BigInteger(String.valueOf(1)));
}
}
BigInteger temp=new BigInteger(String.valueOf(factorial(a.length()-i-1)));
rank=rank.multiply(temp1);
rank=rank.multiply(temp);
sum=sum.add(rank);
sum=sum.mod(new BigInteger(String.valueOf(1000003)));
}
return sum.intValue();
}
}
You using excessive string parsing and object creation. This all are expensive for runtime.
With optimized usage of BigInteger the method will return in ~1ms on my maschine.
public class Solution {
private BigInteger factorial(final int n) {
BigInteger sum = BigInteger.ONE;
if (n < 2) {
return BigInteger.valueOf(n);
}
for (int j = 2; j <= n; j++) {
sum = sum.multiply(BigInteger.valueOf(j));
}
return sum;
}
public int findRank(final String a) {
if (a.length() < 2) {
return 1;
}
final Map<Character, Integer> map1 = new HashMap<>();
BigInteger sum = BigInteger.ONE;
for (int i = 0; i < a.length(); i++) {
final char charAt = a.charAt(i);
if (!map1.containsKey(charAt)) {
map1.put(charAt, 1);
} else {
int cc = map1.get(charAt);
map1.put(charAt, cc++);
}
}
BigInteger temp1 = BigInteger.ONE;
for (final Map.Entry<Character, Integer> entry : map1.entrySet()) {
temp1 = temp1.multiply(factorial(entry.getValue()));
}
temp1 = temp1.pow(1000001);
temp1 = temp1.mod(BigInteger.valueOf(1000003));
for (int i = 0; i < a.length(); i++) {
BigInteger rank = BigInteger.ZERO;
for (int j = i + 1; j < a.length(); j++) {
if (a.charAt(i) > a.charAt(j)) {
rank = rank.add(BigInteger.ONE);
}
}
final BigInteger temp = factorial(a.length() - i - 1);
rank = rank.multiply(temp1);
rank = rank.multiply(temp);
sum = sum.add(rank);
sum = sum.mod(BigInteger.valueOf(1000003));
}
return sum.intValue();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.