Repository navigation
feat: add aggressive cows algorithm - #3233
aartigawade2586 wants to merge 3 commits into
Conversation
|
could you add some references as to where this problem originates from? |
Added the reference to the problem and its original source (USACO February 2005 Gold Division) in the file header. |
realstealthninja
left a comment
There was a problem hiding this comment.
Thank you for contributing! A few changes need to be made. Please read the contributing.md in the root of the repository and the doxygen documentation.
| * (https://www.spoj.com/problems/AGGRCOW/) | ||
| * problem from the USACO February 2005 Gold Division. | ||
| */ | ||
| /* Aggressive cow | ||
| *Problem : |
There was a problem hiding this comment.
| * (https://www.spoj.com/problems/AGGRCOW/) | |
| * problem from the USACO February 2005 Gold Division. | |
| */ | |
| /* Aggressive cow | |
| *Problem : | |
| * @details | |
| * [Aggressive Cows](https://www.spoj.com/problems/AGGRCOW/) |
| */ | ||
| /* |
There was a problem hiding this comment.
| */ | |
| /* | |
| * | |
| * |
| * The minimum distance between any two cows must be maximized. | ||
| */ | ||
| /* | ||
| ###Time and Space complexity |
There was a problem hiding this comment.
| ###Time and Space complexity | |
| * ###Time and Space complexity |
| * Time complexity in worst case is O(n log n + n log(maxDistance)) | ||
| * Time complexity for best case is O(n log n) | ||
| * Time complexity for average case is O(n log n + n log(maxDistance)) | ||
| * Space complexity is O(1) , excluding sorting overhead |
There was a problem hiding this comment.
You should format these time/space complexities using latex .i.e. \f$ O(n) \f$
| #include <cassert> /// for std::assert | ||
| #include <iostream> // for IO operations | ||
| #include <vector> // for std::vector | ||
| using namespace std; |
There was a problem hiding this comment.
| using namespace std; |
By using namespace std, you'll pollute the global name space, It is bad practice to do that.
| // checking if given distance is valid for given number of cows | ||
| bool isValid(int mid, int n, const vector<int>& stalls) { |
There was a problem hiding this comment.
function is not documented in doxygen style.
| test2(); // run self-test implementation #2 | ||
| test3(); // run self-test implementation #3 | ||
| return 0; | ||
| } No newline at end of file |
There was a problem hiding this comment.
| } | |
| } | |
| int main() { | ||
| test1(); // run self-test implementation #1 | ||
| test2(); // run self-test implementation #2 | ||
| test3(); // run self-test implementation #3 |
There was a problem hiding this comment.
prefer wrapping all these functions into one static tests function.
| **************************************************************************************************************** | ||
| */ | ||
| // Will return maximum minimum distance between two cows | ||
| int aggressiveCows(vector<int>& stalls, int n) { |
There was a problem hiding this comment.
must wrap all non test functions in the search name space.
| * If we are given stall positions 1 and 5, then the distance between | ||
| * these two stalls will be 4, i.e., '5 - 1'. | ||
| * Here, as the cows are aggressive, we need to allocate them stalls in such | ||
| * a way that there is a maximum distance between any two cows. |
There was a problem hiding this comment.
you should write about what kind of search algorithm is actually used here.
There was a problem hiding this comment.
Addressed the review comments: updated Doxygen documentation, clarified the search algorithm, fixed complexity formatting, removed using namespace std, added documentation for isValid, and cleaned up the test structure.
Description of Change
Added an implementation of the Aggressive Cows problem using binary search
on the answer and a greedy feasibility check.
The solution:
assert().Checklist
Notes: Added
search/aggressive_cows.cppwith a binary-search-based solution and self-tests.