Hey, I have run the code and it gives 8 as output. Check it here: 2oXGt7 - Online Java Compiler & Debugging Tool - Ideone.com .
[Note: I have used @taran_1407 's code.]
Hey, I have run the code and it gives 8 as output. Check it here: 2oXGt7 - Online Java Compiler & Debugging Tool - Ideone.com .
[Note: I have used @taran_1407 's code.]
All top solution??
Can you please mention that solution link, which gives 16 as answer.
Well, the problem states that every red marble should only have green marbles in four adjacent cells. Yes, problem doesnt mention chess board, but as i identified the pattern, i saw it similar to chess board. Where a white cell always has four black adjacent cells and same for black cells.
I mentioned chess board to make myself entirely clear.
As we move from, say N= 2 to N=3,
ans[3] = ans[2] + (4+5)(for last row except last element) + (4+5)(for last column except last element) +2*3
ans[3] = ans[2] + 2*(sum[5]-sum[3]) + V[2*3].
ans[i] = ans[i-1] + 2*(sum[2i-1]-sum[i]) + V[2i]
Cheers
Shouldnāt sum[1] be 1 instead of 0??
Thatās the only error i guess.
@taran_1407 no itās just that the array needās to be long long thanks by the way a more memory efficient is possible solution:- CodeChef: Practical coding for everyone
This is same as (i+j)%2 == 1 (in bitwise).
This condition automatically make adjacent cells of different colors.
Often you would find thatwhenever there are 1e5 test case, you need to answer test cases in O(1) or atmost O(logN) time. For that, itās very often you preprocess all answers, or enough to find all answers.
Nice one!!
I donāt think there should be such a problem, but you can refer to this memory efficient solution if you can declare an array of size 1000000.
There are two errors in your solutionā¦
First, you were not reading all the input lines. You were just reading the first line. Try putting a loop and break the loop at the end of the input file.
Second, you have commented out a portion of your solution from line 5-18
Well, it is not always necesaary that you need to preprocess the answer. Rather, take CHEFUNI of current long challenge. It too require O(1) answer, because T upto 1e5. But in that problem, we cannot even store or calculate answers for all possible values of X, Y Z, A,B and C.
Well, preprocessing often is easier but have time and memory constraints. If preprocessing is feasible with given constraints, go with preprocessing, else prefer constant time solutions (comparatively tougher than preprocessed solution as i feel).
So, it depends upon problem and constraints.
Got it!.
I was doing it only for the first line.And after I got a wrong answer, I commented the code to atleast get the 1st subtask right.Which again failed coz i was just processing first line.
You can visualise it as a 2-D matrix rather than a chessboard. These patterns become obivous after some practice on similar problems.
Ofcoursw i can check, but i would recommend u to solve this again, using the way i tell u belowā¦
First make a 2D character array, and take all input.
Now, solve this for first case, that is, if (i+j)%2 == 0, color should be green, otherwise red, and calculate cost
Now, solve this for second case, that is, if (i+j)%2 == 0, color should be red, otherwise green, and calculate cost.
Output minimum of both.
Idea Use separate loops whenever u feel it complicatedā¦
@dinesh2193 you first initialize 1D array
string a[n]
and then you doing this a[i][j] you didnāt initialize a 2d array and as taran said try to separate the for loops when getting the input and solving the problem
yeah ,i got the answer for both the above methods.but i had not used any extra space for creating a 2D array(for the above two cases) but used a single variable which can optimise the solution.but it didnt get accecpted
I want you to to solve the problem the way i mentioned, and then rewrite the solution in the way you want, Thatās how we all learnā¦
PS: Donāt mind using extra space, as long as it gets you an AC, and then reduce space complexity.
I hope you donāt mind that.