New answers tagged

2

Don't write using namespace std;. You certainly don't need to write it twice!! // your code goes here along with the duplicated using declaration makes me think that you did not really proof-read your own code first. Pay attention to what you're writing! It will go a long way to finding improvements and silly mistakes. #include <bits/stdc++.h> is ...


1

Starting from Quuxplusone's answer, let's get rid of some more operations. for(int a = 1; a <= n; ++a) { for(int b = a + 1; b <= n; ++b) { if( ((m%a)%b) == ((m%b)%a) ) count++; } } One thing to notice is that the quantity m%a never changes during the inner loop. So, we can move it to the outer loop. for(int a = 1; a <...


2

I'm sure the puzzle intends you to use more math and less brute force. But here's a 2x speedup right away: Any loop of the form for (int a=1; a <= n; ++a) { for (int b = 1; b <= n; ++b) { if (a < b) { do something } } } can obviously be replaced with a loop of the form for (int a=1; a <= n; ++a) { for (...


3

Both answers basically try all k-length paths and count the number of paths that remain on the board compared to all possible paths. A major difference between your answer and the recursive solution you link to is what is cached (i.e., memoized). You code caches the possible moves from a square. The linked code caches partial solutions. That is, the cache ...


0

Since this is asking for a new algorithm, I will try to provide some hints. Look at the output of from itertools import permutations sorted([a*1000 + x*100 + y*10 + z for a,x,y,z in permutations([1,2,3,4])]) Which digits are most likely to be different between consecutive members of this list? Look at the elements that start with 4. Can you see a pattern in ...


1

It's not so much the implementation. With problems like these (typical coding competition ones) it's always the same, that you have to find a clever algorithm instead of writing down as code a straightforward implementation of the problem description. I haven't myself analyzed the problem, but I'd try to answer some questions: Of all possible permutations, ...


1

The approach is too slow, further analysis is required. Let's call the number of possibilities for a single sided road F(n). If n < 0, this should be zero. Clearly F(0) = 1, since there is a single way to plan an empty street. Also, F(1) = 2, because a street with a single spot is either occupied or not. For n >= 2, the first spot is either occupied or ...


1

There is a much faster solution to this problem. If the ending character is Y, two strings can be created. But if the ending character is X, only one valid string can be created: XY. This results in the following equation, complexity O(n): def king_kohima(number: int) -> int: x = [0, 1, 1] y = [0, 1, 2] for i in range(3, number + 1): # Start ...


1

no naked new See ⧺R.11 and a few other guidelines that mention "naked new". bool* whether_itroduced = new bool[input_length + 1]{0}; Use a std::vector rather than a bare allocated array. (This also fixes the problem where you are not freeing it, as it becomes automatic). On the other hand, vector<bool> is weird. It is optimized to be "...


2

There's some straightforward and common C++ problems immediately obvious: using namespace std; - avoid that; it makes your code more fragile. It doesn't even make it shorter! Streaming input without checking. If you use >>, it's essential to only use the value if it was actually written. For a simple program like this, it's easiest to just set std:...


0

Plan A: Build a 'batch' INSERT: INSERT INTO wyciek (nick, ip) VALUES (12, 34), (234, 456), ... Then execute it. If your columns are VARCHAR instead of INT, be sure to put quotes around each value. For 100 rows, this will run about 10 times as fast as 100 single-row INSERTs. (And that is aside from having the COMMIT inside the loop!) Plan B: If your data ...


3

General tips Avoid defining your own loops. This is especially true with R because the chances are that there is another more elegant way to solve the problem using highly optimized functions. Avoid rbind function inside loops especially if your are using it as an appending mechanism. This function creates a new empty data frame with a number of rows equal ...


Top 50 recent answers are included