Skip to main content
Code Review

Return to Question

Question Protected by Jamal
Add link to original contest
Source Link
janos
  • 112.9k
  • 15
  • 154
  • 396

I am trying to solve a problem in hacker rankproblem on Hacker Rank and the question is as follows:

I am trying to solve a problem in hacker rank and the question is as follows:

I am trying to solve a problem on Hacker Rank and the question is as follows:

Tweeted twitter.com/StackCodeReview/status/695096346798333952
added 29 characters in body
Source Link
Quill
  • 12k
  • 5
  • 41
  • 93

Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K\$K\$. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with:

He repeats this procedure until all the cookies in his collection have a sweetness ≥K\$\ge K\$. You are given Jesse's cookies. Print the number of operations required to give the cookies a sweetness ≥K\$\ge K\$. Print −1\$−1\$ if this isn't possible.

Input##Input Format

The first line consists of integers N\$N\$, the number of cookies and K\$K\$, the minimum required sweetness, separated by a space. The next line contains N\$N\$ integers describing the array A\$A\$ where Ai\$Ai\$ is the sweetness of the ith\$i\$th cookie in Jesse's collection.

Constraints##Constraints

1≤N≤10^6 0≤K≤10^9 0≤Ai≤10^6

\1ドル\le N\le 10^6\$
\0ドル\le K\le 10^9\$
\0ドル\le Ai\le 10^6\$

Output##Output Format

Output the number of operations that are needed to increase the cookie's sweetness ≥K\$\ge k\$. Output −1\$−1\$ if this isn't possible.

Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with:

He repeats this procedure until all the cookies in his collection have a sweetness ≥K. You are given Jesse's cookies. Print the number of operations required to give the cookies a sweetness ≥K. Print −1 if this isn't possible.

Input Format

The first line consists of integers N, the number of cookies and K, the minimum required sweetness, separated by a space. The next line contains N integers describing the array A where Ai is the sweetness of the ith cookie in Jesse's collection.

Constraints

1≤N≤10^6 0≤K≤10^9 0≤Ai≤10^6

Output Format

Output the number of operations that are needed to increase the cookie's sweetness ≥K. Output −1 if this isn't possible.

Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value \$K\$. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with:

He repeats this procedure until all the cookies in his collection have a sweetness \$\ge K\$. You are given Jesse's cookies. Print the number of operations required to give the cookies a sweetness \$\ge K\$. Print \$−1\$ if this isn't possible.

##Input Format

The first line consists of integers \$N\$, the number of cookies and \$K\$, the minimum required sweetness, separated by a space. The next line contains \$N\$ integers describing the array \$A\$ where \$Ai\$ is the sweetness of the \$i\$th cookie in Jesse's collection.

##Constraints

\1ドル\le N\le 10^6\$
\0ドル\le K\le 10^9\$
\0ドル\le Ai\le 10^6\$

##Output Format

Output the number of operations that are needed to increase the cookie's sweetness \$\ge k\$. Output \$−1\$ if this isn't possible.

added 27 characters in body; edited tags
Source Link
Mast
  • 13.8k
  • 12
  • 57
  • 127

I am trying to solve a problem in hacker rank and the question is as follows

Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with:

sweetness =(×ばつ Least sweet cookie + ×ばつ 2nd least sweet cookie).

Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with:

He repeats this procedure until all the cookies in his collection have a sweetness ≥K. You are given Jesse's cookies. Print the number of operations required to give the cookies a sweetness ≥K. Print −1 if this isn't possible.

sweetness =(×ばつ Least sweet cookie + ×ばつ 2nd least sweet cookie).

Input Format

He repeats this procedure until all the cookies in his collection have a sweetness ≥K. You are given Jesse's cookies. Print the number of operations required to give the cookies a sweetness ≥K. Print −1 if this isn't possible.

The first line consists of integers N, the number of cookies and K, the minimum required sweetness, separated by a space. The next line contains N integers describing the array A where Ai is the sweetness of the ith cookie in Jesse's collection.

Input Format

Constraints

The first line consists of integers N, the number of cookies and K, the minimum required sweetness, separated by a space. The next line contains N integers describing the array A where Ai is the sweetness of the ith cookie in Jesse's collection.

1≤N≤10^6 0≤K≤10^9 0≤Ai≤10^6

Constraints

Output Format

1≤N≤10^6 0≤K≤10^9 0≤Ai≤10^6

Output the number of operations that are needed to increase the cookie's sweetness ≥K. Output −1 if this isn't possible.

Output Format

Output the number of operations that are needed to increase the cookie's sweetness ≥K. Output −1 if this isn't possible.

forFor this I have written code in javaJava like this:

forFor the above code I am getting half of my test cases right and the other half is giving time out exception(taking more than 4 seconds to give output).My My question is basically: where can I improve the performance of the code.? Is there any other approach which I should go about.?

I am trying to solve a problem in hacker rank and the question is as follows

Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with:

sweetness =(×ばつ Least sweet cookie + ×ばつ 2nd least sweet cookie).

He repeats this procedure until all the cookies in his collection have a sweetness ≥K. You are given Jesse's cookies. Print the number of operations required to give the cookies a sweetness ≥K. Print −1 if this isn't possible.

Input Format

The first line consists of integers N, the number of cookies and K, the minimum required sweetness, separated by a space. The next line contains N integers describing the array A where Ai is the sweetness of the ith cookie in Jesse's collection.

Constraints

1≤N≤10^6 0≤K≤10^9 0≤Ai≤10^6

Output Format

Output the number of operations that are needed to increase the cookie's sweetness ≥K. Output −1 if this isn't possible.

for this I have written code in java like this

for the above code I am getting half of my test cases right and the other half is giving time out exception(taking more than 4 seconds to give output).My question is basically where can I improve the performance of the code. Is there any other approach which I should go about.

I am trying to solve a problem in hacker rank and the question is as follows:

Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with:

sweetness =(×ばつ Least sweet cookie + ×ばつ 2nd least sweet cookie).

He repeats this procedure until all the cookies in his collection have a sweetness ≥K. You are given Jesse's cookies. Print the number of operations required to give the cookies a sweetness ≥K. Print −1 if this isn't possible.

Input Format

The first line consists of integers N, the number of cookies and K, the minimum required sweetness, separated by a space. The next line contains N integers describing the array A where Ai is the sweetness of the ith cookie in Jesse's collection.

Constraints

1≤N≤10^6 0≤K≤10^9 0≤Ai≤10^6

Output Format

Output the number of operations that are needed to increase the cookie's sweetness ≥K. Output −1 if this isn't possible.

For this I have written code in Java like this:

For the above code I am getting half of my test cases right and the other half is giving time out exception(taking more than 4 seconds to give output). My question is basically: where can I improve the performance of the code? Is there any other approach which I should go about?

Source Link
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /