prime is a positive integer X that has exactly two distinct divisors: 1 and X. The first few prime integers are 2, 3, 5, 7, 11 and 13.

semiprime is a natural number that is the product of two (not necessarily distinct) prime numbers. The first few semiprimes are 4, 6, 9, 10, 14, 15, 21, 22, 25, 26.

You are given two non-empty arrays P and Q, each consisting of M integers. These arrays represent queries about the number of semiprimes within specified ranges.

Query K requires you to find the number of semiprimes within the range (P[K], Q[K]), where 1 ≤ P[K] ≤ Q[K] ≤ N.

For example, consider an integer N = 26 and arrays P, Q such that:

P[0] = 1 Q[0] = 26 P[1] = 4 Q[1] = 10 P[2] = 16 Q[2] = 20

The number of semiprimes within each of these ranges is as follows:

  • (1, 26) is 10,
  • (4, 10) is 4,
  • (16, 20) is 0.

Write a function:

class Solution { public int[] solution(int N, int[] P, int[] Q); }

that, given an integer N and two non-empty arrays P and Q consisting of M integers, returns an array consisting of M elements specifying the consecutive answers to all the queries.

For example, given an integer N = 26 and arrays P, Q such that:

P[0] = 1 Q[0] = 26 P[1] = 4 Q[1] = 10 P[2] = 16 Q[2] = 20

the function should return the values [10, 4, 0], as explained above.

Assume that:

  • N is an integer within the range [1..50,000];
  • M is an integer within the range [1..30,000];
  • each element of arrays P, Q is an integer within the range [1..N];
  • P[i] ≤ Q[i].

Complexity:

  • expected worst-case time complexity is O(N*log(log(N))+M);
  • expected worst-case space complexity is O(N+M), beyond input storage (not counting the storage required for input arguments).



prime   = 소수 (1과 자기자신과 나누어 지는수)

semiprime =반소수 (자기자신및 다른 소수와 곱한값)


P  와 Q 사이에 반소수의 개수를 구하면 되는거다.


아래는 나의 솔루선

너무 어렵고 힘든 문제다 아래소스는 답만 나오는데 중점을 두었지 전혀 만족스러운 코드가 아니다.

담에 다시 도전하겠다.



// you can also use imports, for example:

import java.util.*;


// you can write to stdout for debugging purposes, e.g.

// System.out.println("this is a debug message");


class Solution {

    public int[] solution(int N, int[] P, int[] Q) {

        

        //first, get  primes and then get semiprimes.

        Set<Integer> prime = new LinkedHashSet<Integer>();

        for(int i=2; i< N; i++){

            prime.add(i);

            for(int j =1; j<i; j++){

                if( i%j == 0 && j !=1 && j !=i){

                    prime.remove(i);

                }

            }

        }

       // System.out.println("prime = " + prime);

        

        Set<Integer> semiPrime = new LinkedHashSet<Integer>();

        for(int i=0; i< prime.size(); i++){

            int getPrime = prime.stream().skip(prime.size()-(i+1)).findFirst().get();

             semiPrime.add(getPrime * getPrime);

             for(int j =1; j< prime.size(); j++){

                 int getPrime1 = prime.stream().skip(prime.size()-(i+1)).findFirst().get();

                 int getPrime2 = prime.stream().skip(prime.size()-(j+1)).findFirst().get();

               semiPrime.add(getPrime1 * getPrime2);

            }

        }

        //System.out.println("semiPrime = " + semiPrime);

        

        List<Integer> list = new ArrayList<Integer>(semiPrime);

      

        int[] ret = new int[3];

        int cnt = 0;

        for(int i=0 ;i < P.length; i++){

               for(int j =0; j< list.size() ; j++){

                       if(list.get(j) >= P[i] && list.get(j)  <= Q[i] ){

                           cnt++;

                     }

               }

               ret[i] = cnt;

               cnt = 0;

        }

        // System.out.println("ret = " + ret);

         

        return ret;

        

    }

}

블로그 이미지

낭만가을

,