Code Monkey home page Code Monkey logo

exercise's People

Contributors

adrianromanko avatar

Watchers

 avatar  avatar

exercise's Issues

Algorithm Improvement

Instead of unfolding the full combination tree and storing all possibilities there is a simpler
way to hack this problem.

Result:
O(1) for Next/Previous paths
O(N) for generating larger intervals of the combination tree. Assuming the user would like to display a groups of branches of the combination tree on the UI then a tree would be drawn and branch highlighted using Canvas or SVG/CSS.

Logic

We know that in a nxn grid, there must be n-1 down moves and n-1 right moves.
Then:
(n-1)+(n-1) === 2(n-1)

We are assuming that the order of combinations (paths) doesn't matter.
For instance, a 3x3 grid will have a binary sequence with a maximum of 4 binary digits or steps:
2(n-1)
=2(3-1)
=4
Thus, 4 steps.

All possibilities in an 3x3 grid:

Binary Decimal
0011 3
0101 5
0110 6
1001 9
1010 10
1100 12

Proposal

From the pattern above we know that there must be two 0s and two '1s' in each sequence of steps.
An algorithm could be written to iterate over all binary possibilities with these conditions without having to store each path independently. If there are 1 billion combinations, use an index to increment/decrement or for larger sets split the binary intervals into buckets and iterate over smaller intervals that can be handled by the data type. In JavaScript the max integer is 2^53-1, so use a BigNum library to avoid overflow.

From sequence 1010 to 1100 there is 1 in between that does not meet the requirements (1011) and that sequence will be ignored, thus 1 lost cycle in a while loop.

Psuedo Code

gridSize = 3; // 3x3 grid
maxValue = 15 // maximum binary value '1111'
digitsAllowed = 4 // maximum of four binary digits

for (i = 1; i <= maxValue; i++) {
if (isValid(numToBinary(i), digitsAllowed ) {
// store
} else {
// next
}
}

isValid(n, digitsAllowed) {
// count the number of 1s and 0s
// int nOnes
// int nZeros
if ((digitsAllowed-2)=== (nOnes-1) + (nZeros-1)) {
return true
} else {
return false
}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.