Shouldn't the new have square brackets?

Paul Smith

p...@pollyandpaul.co.uk


On Mon, 2 Mar 2020 at 17:06, Bartholomew Furrow <fur...@gmail.com> wrote:

> The problem seems to relate to the long long int*. You haven't called
> delete[] on it, but that doesn't fix the problem, and I'm actually not sure
> how to. I can't even reproduce the runtime error on my machine.
>
> Here's the input that gives a runtime error, in case that's helpful to
> someone:
> 3
> 3
> 5 1 2
> 6
> 1 3 3 2 2 15
> 1
> 100000
>
> Note that that's just the sample input, with an extra case. The extra case
> on its own does *not* cause a runtime error.
>
> Generally speaking, you don't want to make arrays like that in programming
> problems like Kick Start's. You want to use a vector instead. If you don't
> mind, I'd like to give some other suggestions about how you could write
> this code to be more readable and debuggable.
> - 0-index almost always (test case # is a reasonable exception)
> - Don't use "i" for the test case number, because you might want to use it
> later, or refer to it accidentally. I habitually use "qw" instead just so
> there's no chance I'll use it by accident.
> - Declare variables as they're used, rather than in a big block at the top.
> - Unless efficiency is a very important factor, use vector<int> instead of
> int*.
>
> Here's your code, with those changes, which passes the first test set.
> It's O(N^2), though, so it needs a new algorithm to pass the second test
> set. Good luck!
>
> Bartholomew
>
>
> #include <iostream>
> #include <vector>
> using namespace std;
>
> int main(){
>     int T;
>     cin>>T;
>     for(int qw=1; qw<=T; qw++){
>         int N;
>         cin>>N;
>         vector<int> arr;
>         int h = 1;
>         int prev = 0;
>         cout<<"Case #"<<qw<<": ";
>         for(int j=0;j<N;j++){
>             int nextReferences;
>             cin >> nextReferences;
>             arr.push_back(nextReferences);
>             int cnt=0;
>
>             for(int k=0;k<=j;k++){
>                 if(arr[k]>=h){
>                     cnt++;
>                 }
>             }
>             if(cnt==h){
>                 cout<<h<<" ";
>                 h++;
>                 prev=cnt;
>             }
>             else{
>                 cout<<prev<<" ";
>             }
>         }
>         cout<<endl;
>     }
> }
>
>
>
>
> On Mon, Mar 2, 2020 at 9:07 AM Rajshree Gupta <rajshreegupta2...@gmail.com>
> wrote:
>
>> I am getting RE test case skipped error. It is working properly with the
>> test case mention in problem. My code is:
>> #include<iostream>
>> using namespace std;
>>
>> int main(){
>> int T,i;
>> long long int N,j,k,cnt,prev,h;
>>     cin>>T;
>>     for(i=1;i<=T;i++){
>>         cin>>N;
>>         long long int *arr = new long long int(N+1);
>>         h = 1;
>>         cout<<"Case #"<<i<<": ";
>>         for(j=1;j<=N;j++){
>>             cin>>arr[j];
>>             cnt=0;
>>
>>             for(k=1;k<=j;k++){
>>                 if(arr[k]>=h){
>>                     cnt++;
>>                 }
>>             }
>>             if(cnt==h){
>>                 cout<<h<<" ";
>>                 h++;
>>                 prev=cnt;
>>             }
>>             else{
>>                 cout<<prev<<" ";
>>             }
>>         }
>>         cout<<endl;
>>     }
>> }
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Google Code Jam" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to google-code+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/google-code/35d4f626-1694-48cf-b653-acead2177775%40googlegroups.com
>> <https://groups.google.com/d/msgid/google-code/35d4f626-1694-48cf-b653-acead2177775%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Google Code Jam" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to google-code+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/google-code/CAHaiWHOhsHxK2G7hVBTFaFOHeYU_OSGNB1%3D%3D-prpXXYy%3DE8WAQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/google-code/CAHaiWHOhsHxK2G7hVBTFaFOHeYU_OSGNB1%3D%3D-prpXXYy%3DE8WAQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-code+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-code/CAJej63LkU5OeoHF%2BgwF3Pa1KPqd-WsHRb56zh5uutBzV1GSnhw%40mail.gmail.com.

Reply via email to