Dynamic programming: Code Wars: twice linear: algorithm times out











up vote
5
down vote

favorite












I am struggling with a Kata in Code Wars:
https://www.codewars.com/kata/5672682212c8ecf83e000050/train/javascript

The idea is to create a sequence of numbers, where each number is created reclusively following this two formulas:



y=2x + 1  
z=3x + 1


With x being the current number in the sequence.



Starting with 1, the sequence would grow like this:



sequence = [1]  
x = 1
y = 2*1 +1 = 3
z = 3*1 + 1 = 4
leading to sequence = [1, 3, 4]


Applying it to the next numbers leads to:



x = 3  
y = 2*3 + 1 = 7
z = 3*3 + 1 = 10
leading to sequence = [1, 3, 4, 7, 10]

x = 4
y = 2*4 + 1 = 9
z = 3*4 + 1 = 13
sequence [1, 3, 4, 7, 9, 10, 13]


And so forth. Notice that I sorted the sequence in the last step, as the results of x=4 (9 and 13) have to be mixed with the results of x=3 (7 and 10) to keep an ordered sequence.



[1, 3, 4, 7, 9, 10, 13, 15, 19, 21, 22, 27, ...]



I was able to solve the problem correctly, but the implementation is supposed to be efficient and I am timing out. My code:



function dblLinear(n) {
cache = {};
cache[0] = 1;
res = [1];
lengthCounter = 0
if (n === 0) {
return 1;
}
for (var i = 1; i < n + 10; i++) {
//console.log("i ", i)
if (cache[i] === undefined && res.includes(i)) {
//console.log('i: ', i, ' el1: ', i * 2 + 1, ' el2: ', i * 3 + 1);
cache[i] = [i * 2 + 1, i * 3 + 1]
if (!res.includes(i * 2 + 1)) {
res.push(i * 2 + 1);
}
if (!res.includes(i * 3 + 1)) {
res.push(i * 3 + 1);
}
//console.log("res ", res)
}
if (res.length !== lengthCounter) {
var arrStart = res.slice(0, Math.floor(res.length / 2));
var arrEnd = res.slice(Math.floor(res.length / 2), res.length)
arrEnd.sort(function(a, b) {
return a - b;
});
res = arrStart.concat(arrEnd)
lengthCounter = res.length
}
}
//console.log('res ', res);
return res[n];
}


As you can see in my code, I tried some simple tricks to increase the efficiency but I 'm guessing I need more speed gains. What do you think is the problem and how could I increase the efficiency?



Any help will be greatly appreciated!



Cheers



Manuel










share|improve this question
























  • You should describe the task instead of referring to an external web site. What is x,y,z ? How is the desired sequence generated out of them?
    – MrSmith42
    Mar 19 at 11:36










  • Hi and thanks for pointing that out. I edited the code and I hope it is more understandable now.
    – Manuel Hong
    Mar 20 at 9:03















up vote
5
down vote

favorite












I am struggling with a Kata in Code Wars:
https://www.codewars.com/kata/5672682212c8ecf83e000050/train/javascript

The idea is to create a sequence of numbers, where each number is created reclusively following this two formulas:



y=2x + 1  
z=3x + 1


With x being the current number in the sequence.



Starting with 1, the sequence would grow like this:



sequence = [1]  
x = 1
y = 2*1 +1 = 3
z = 3*1 + 1 = 4
leading to sequence = [1, 3, 4]


Applying it to the next numbers leads to:



x = 3  
y = 2*3 + 1 = 7
z = 3*3 + 1 = 10
leading to sequence = [1, 3, 4, 7, 10]

x = 4
y = 2*4 + 1 = 9
z = 3*4 + 1 = 13
sequence [1, 3, 4, 7, 9, 10, 13]


And so forth. Notice that I sorted the sequence in the last step, as the results of x=4 (9 and 13) have to be mixed with the results of x=3 (7 and 10) to keep an ordered sequence.



[1, 3, 4, 7, 9, 10, 13, 15, 19, 21, 22, 27, ...]



I was able to solve the problem correctly, but the implementation is supposed to be efficient and I am timing out. My code:



function dblLinear(n) {
cache = {};
cache[0] = 1;
res = [1];
lengthCounter = 0
if (n === 0) {
return 1;
}
for (var i = 1; i < n + 10; i++) {
//console.log("i ", i)
if (cache[i] === undefined && res.includes(i)) {
//console.log('i: ', i, ' el1: ', i * 2 + 1, ' el2: ', i * 3 + 1);
cache[i] = [i * 2 + 1, i * 3 + 1]
if (!res.includes(i * 2 + 1)) {
res.push(i * 2 + 1);
}
if (!res.includes(i * 3 + 1)) {
res.push(i * 3 + 1);
}
//console.log("res ", res)
}
if (res.length !== lengthCounter) {
var arrStart = res.slice(0, Math.floor(res.length / 2));
var arrEnd = res.slice(Math.floor(res.length / 2), res.length)
arrEnd.sort(function(a, b) {
return a - b;
});
res = arrStart.concat(arrEnd)
lengthCounter = res.length
}
}
//console.log('res ', res);
return res[n];
}


As you can see in my code, I tried some simple tricks to increase the efficiency but I 'm guessing I need more speed gains. What do you think is the problem and how could I increase the efficiency?



Any help will be greatly appreciated!



Cheers



Manuel










share|improve this question
























  • You should describe the task instead of referring to an external web site. What is x,y,z ? How is the desired sequence generated out of them?
    – MrSmith42
    Mar 19 at 11:36










  • Hi and thanks for pointing that out. I edited the code and I hope it is more understandable now.
    – Manuel Hong
    Mar 20 at 9:03













up vote
5
down vote

favorite









up vote
5
down vote

favorite











I am struggling with a Kata in Code Wars:
https://www.codewars.com/kata/5672682212c8ecf83e000050/train/javascript

The idea is to create a sequence of numbers, where each number is created reclusively following this two formulas:



y=2x + 1  
z=3x + 1


With x being the current number in the sequence.



Starting with 1, the sequence would grow like this:



sequence = [1]  
x = 1
y = 2*1 +1 = 3
z = 3*1 + 1 = 4
leading to sequence = [1, 3, 4]


Applying it to the next numbers leads to:



x = 3  
y = 2*3 + 1 = 7
z = 3*3 + 1 = 10
leading to sequence = [1, 3, 4, 7, 10]

x = 4
y = 2*4 + 1 = 9
z = 3*4 + 1 = 13
sequence [1, 3, 4, 7, 9, 10, 13]


And so forth. Notice that I sorted the sequence in the last step, as the results of x=4 (9 and 13) have to be mixed with the results of x=3 (7 and 10) to keep an ordered sequence.



[1, 3, 4, 7, 9, 10, 13, 15, 19, 21, 22, 27, ...]



I was able to solve the problem correctly, but the implementation is supposed to be efficient and I am timing out. My code:



function dblLinear(n) {
cache = {};
cache[0] = 1;
res = [1];
lengthCounter = 0
if (n === 0) {
return 1;
}
for (var i = 1; i < n + 10; i++) {
//console.log("i ", i)
if (cache[i] === undefined && res.includes(i)) {
//console.log('i: ', i, ' el1: ', i * 2 + 1, ' el2: ', i * 3 + 1);
cache[i] = [i * 2 + 1, i * 3 + 1]
if (!res.includes(i * 2 + 1)) {
res.push(i * 2 + 1);
}
if (!res.includes(i * 3 + 1)) {
res.push(i * 3 + 1);
}
//console.log("res ", res)
}
if (res.length !== lengthCounter) {
var arrStart = res.slice(0, Math.floor(res.length / 2));
var arrEnd = res.slice(Math.floor(res.length / 2), res.length)
arrEnd.sort(function(a, b) {
return a - b;
});
res = arrStart.concat(arrEnd)
lengthCounter = res.length
}
}
//console.log('res ', res);
return res[n];
}


As you can see in my code, I tried some simple tricks to increase the efficiency but I 'm guessing I need more speed gains. What do you think is the problem and how could I increase the efficiency?



Any help will be greatly appreciated!



Cheers



Manuel










share|improve this question















I am struggling with a Kata in Code Wars:
https://www.codewars.com/kata/5672682212c8ecf83e000050/train/javascript

The idea is to create a sequence of numbers, where each number is created reclusively following this two formulas:



y=2x + 1  
z=3x + 1


With x being the current number in the sequence.



Starting with 1, the sequence would grow like this:



sequence = [1]  
x = 1
y = 2*1 +1 = 3
z = 3*1 + 1 = 4
leading to sequence = [1, 3, 4]


Applying it to the next numbers leads to:



x = 3  
y = 2*3 + 1 = 7
z = 3*3 + 1 = 10
leading to sequence = [1, 3, 4, 7, 10]

x = 4
y = 2*4 + 1 = 9
z = 3*4 + 1 = 13
sequence [1, 3, 4, 7, 9, 10, 13]


And so forth. Notice that I sorted the sequence in the last step, as the results of x=4 (9 and 13) have to be mixed with the results of x=3 (7 and 10) to keep an ordered sequence.



[1, 3, 4, 7, 9, 10, 13, 15, 19, 21, 22, 27, ...]



I was able to solve the problem correctly, but the implementation is supposed to be efficient and I am timing out. My code:



function dblLinear(n) {
cache = {};
cache[0] = 1;
res = [1];
lengthCounter = 0
if (n === 0) {
return 1;
}
for (var i = 1; i < n + 10; i++) {
//console.log("i ", i)
if (cache[i] === undefined && res.includes(i)) {
//console.log('i: ', i, ' el1: ', i * 2 + 1, ' el2: ', i * 3 + 1);
cache[i] = [i * 2 + 1, i * 3 + 1]
if (!res.includes(i * 2 + 1)) {
res.push(i * 2 + 1);
}
if (!res.includes(i * 3 + 1)) {
res.push(i * 3 + 1);
}
//console.log("res ", res)
}
if (res.length !== lengthCounter) {
var arrStart = res.slice(0, Math.floor(res.length / 2));
var arrEnd = res.slice(Math.floor(res.length / 2), res.length)
arrEnd.sort(function(a, b) {
return a - b;
});
res = arrStart.concat(arrEnd)
lengthCounter = res.length
}
}
//console.log('res ', res);
return res[n];
}


As you can see in my code, I tried some simple tricks to increase the efficiency but I 'm guessing I need more speed gains. What do you think is the problem and how could I increase the efficiency?



Any help will be greatly appreciated!



Cheers



Manuel







javascript algorithm performance dynamic-programming






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 20 at 9:01

























asked Mar 19 at 10:38









Manuel Hong

262




262












  • You should describe the task instead of referring to an external web site. What is x,y,z ? How is the desired sequence generated out of them?
    – MrSmith42
    Mar 19 at 11:36










  • Hi and thanks for pointing that out. I edited the code and I hope it is more understandable now.
    – Manuel Hong
    Mar 20 at 9:03


















  • You should describe the task instead of referring to an external web site. What is x,y,z ? How is the desired sequence generated out of them?
    – MrSmith42
    Mar 19 at 11:36










  • Hi and thanks for pointing that out. I edited the code and I hope it is more understandable now.
    – Manuel Hong
    Mar 20 at 9:03
















You should describe the task instead of referring to an external web site. What is x,y,z ? How is the desired sequence generated out of them?
– MrSmith42
Mar 19 at 11:36




You should describe the task instead of referring to an external web site. What is x,y,z ? How is the desired sequence generated out of them?
– MrSmith42
Mar 19 at 11:36












Hi and thanks for pointing that out. I edited the code and I hope it is more understandable now.
– Manuel Hong
Mar 20 at 9:03




Hi and thanks for pointing that out. I edited the code and I hope it is more understandable now.
– Manuel Hong
Mar 20 at 9:03












3 Answers
3






active

oldest

votes

















up vote
7
down vote













This problem can be solved in O(n). The idea is to keep track which element was generated last and add the smaller one of the two possibilities, so the elements are added in order. This code passes all the tests easily.






function dblLinear(n) {
let u = [1], x = 0, y = 0
for (let i = 0; i < n; i++) {
let nextX = 2 * u[x] + 1, nextY = 3 * u[y] + 1
if (nextX <= nextY) {
u.push(nextX)
x++
if (nextX == nextY)
y++
} else {
u.push(nextY)
y++
}
}
return u[n]
}

console.log(dblLinear(10) + " = " + 22)
console.log(dblLinear(20) + " = " + 57)
console.log(dblLinear(30) + " = " + 91)
console.log(dblLinear(50) + " = " + 175)
console.log(dblLinear(100) + " = " + 447)








share|improve this answer























  • Neat! This is very like Dijkstra's construction of Hamming Numbers. en.wikipedia.org/wiki/Regular_number#Algorithms
    – Paul Hankin
    Mar 19 at 13:54










  • I'm loving the implementation. Incredibly elegant! Thanks @maraca
    – Manuel Hong
    Mar 20 at 9:08


















up vote
3
down vote













The existing solution is great but my solution to this problem is






function dblLinear(n) {
let eq1Index = 0;
let eq2Index = 0;
let eq1Array = [3];
let eq2Array = [4];
let result = 1;
for (let i = 1; i <= n; i++) {
if (eq1Array[eq1Index] < eq2Array[eq2Index]) {
result = eq1Array[eq1Index];
eq1Index++;
} else {
result = eq2Array[eq2Index];
eq2Index++;
}

eq2Array.indexOf(2 * result + 1) == -1 && eq1Array.push(2 * result + 1);

eq2Array.push(3 * result + 1);
}
return result;
}
console.log(dblLinear(10) + " = " + 22)
console.log(dblLinear(20) + " = " + 57)
console.log(dblLinear(30) + " = " + 91)
console.log(dblLinear(50) + " = " + 175)
console.log(dblLinear(100) + " = " + 447)








share|improve this answer






























    up vote
    2
    down vote













    The good solution for this




    function dblLinear(n) {
    var ai = 0, bi = 0, eq = 0;
    var sequence = [1];
    while (ai + bi < n + eq) {
    var y = 2 * sequence[ai] + 1;
    var z = 3 * sequence[bi] + 1;
    if (y < z) { sequence.push(y); ai++; }
    else if (y > z) { sequence.push(z); bi++; }
    else { sequence.push(y); ai++; bi++; eq++; }
    }
    return sequence.pop();
    }

    console.log(dblLinear(10) + " = " + 22)
    console.log(dblLinear(20) + " = " + 57)
    console.log(dblLinear(30) + " = " + 91)
    console.log(dblLinear(50) + " = " + 175)
    console.log(dblLinear(100) + " = " + 447)








    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49360883%2fdynamic-programming-code-wars-twice-linear-algorithm-times-out%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      7
      down vote













      This problem can be solved in O(n). The idea is to keep track which element was generated last and add the smaller one of the two possibilities, so the elements are added in order. This code passes all the tests easily.






      function dblLinear(n) {
      let u = [1], x = 0, y = 0
      for (let i = 0; i < n; i++) {
      let nextX = 2 * u[x] + 1, nextY = 3 * u[y] + 1
      if (nextX <= nextY) {
      u.push(nextX)
      x++
      if (nextX == nextY)
      y++
      } else {
      u.push(nextY)
      y++
      }
      }
      return u[n]
      }

      console.log(dblLinear(10) + " = " + 22)
      console.log(dblLinear(20) + " = " + 57)
      console.log(dblLinear(30) + " = " + 91)
      console.log(dblLinear(50) + " = " + 175)
      console.log(dblLinear(100) + " = " + 447)








      share|improve this answer























      • Neat! This is very like Dijkstra's construction of Hamming Numbers. en.wikipedia.org/wiki/Regular_number#Algorithms
        – Paul Hankin
        Mar 19 at 13:54










      • I'm loving the implementation. Incredibly elegant! Thanks @maraca
        – Manuel Hong
        Mar 20 at 9:08















      up vote
      7
      down vote













      This problem can be solved in O(n). The idea is to keep track which element was generated last and add the smaller one of the two possibilities, so the elements are added in order. This code passes all the tests easily.






      function dblLinear(n) {
      let u = [1], x = 0, y = 0
      for (let i = 0; i < n; i++) {
      let nextX = 2 * u[x] + 1, nextY = 3 * u[y] + 1
      if (nextX <= nextY) {
      u.push(nextX)
      x++
      if (nextX == nextY)
      y++
      } else {
      u.push(nextY)
      y++
      }
      }
      return u[n]
      }

      console.log(dblLinear(10) + " = " + 22)
      console.log(dblLinear(20) + " = " + 57)
      console.log(dblLinear(30) + " = " + 91)
      console.log(dblLinear(50) + " = " + 175)
      console.log(dblLinear(100) + " = " + 447)








      share|improve this answer























      • Neat! This is very like Dijkstra's construction of Hamming Numbers. en.wikipedia.org/wiki/Regular_number#Algorithms
        – Paul Hankin
        Mar 19 at 13:54










      • I'm loving the implementation. Incredibly elegant! Thanks @maraca
        – Manuel Hong
        Mar 20 at 9:08













      up vote
      7
      down vote










      up vote
      7
      down vote









      This problem can be solved in O(n). The idea is to keep track which element was generated last and add the smaller one of the two possibilities, so the elements are added in order. This code passes all the tests easily.






      function dblLinear(n) {
      let u = [1], x = 0, y = 0
      for (let i = 0; i < n; i++) {
      let nextX = 2 * u[x] + 1, nextY = 3 * u[y] + 1
      if (nextX <= nextY) {
      u.push(nextX)
      x++
      if (nextX == nextY)
      y++
      } else {
      u.push(nextY)
      y++
      }
      }
      return u[n]
      }

      console.log(dblLinear(10) + " = " + 22)
      console.log(dblLinear(20) + " = " + 57)
      console.log(dblLinear(30) + " = " + 91)
      console.log(dblLinear(50) + " = " + 175)
      console.log(dblLinear(100) + " = " + 447)








      share|improve this answer














      This problem can be solved in O(n). The idea is to keep track which element was generated last and add the smaller one of the two possibilities, so the elements are added in order. This code passes all the tests easily.






      function dblLinear(n) {
      let u = [1], x = 0, y = 0
      for (let i = 0; i < n; i++) {
      let nextX = 2 * u[x] + 1, nextY = 3 * u[y] + 1
      if (nextX <= nextY) {
      u.push(nextX)
      x++
      if (nextX == nextY)
      y++
      } else {
      u.push(nextY)
      y++
      }
      }
      return u[n]
      }

      console.log(dblLinear(10) + " = " + 22)
      console.log(dblLinear(20) + " = " + 57)
      console.log(dblLinear(30) + " = " + 91)
      console.log(dblLinear(50) + " = " + 175)
      console.log(dblLinear(100) + " = " + 447)








      function dblLinear(n) {
      let u = [1], x = 0, y = 0
      for (let i = 0; i < n; i++) {
      let nextX = 2 * u[x] + 1, nextY = 3 * u[y] + 1
      if (nextX <= nextY) {
      u.push(nextX)
      x++
      if (nextX == nextY)
      y++
      } else {
      u.push(nextY)
      y++
      }
      }
      return u[n]
      }

      console.log(dblLinear(10) + " = " + 22)
      console.log(dblLinear(20) + " = " + 57)
      console.log(dblLinear(30) + " = " + 91)
      console.log(dblLinear(50) + " = " + 175)
      console.log(dblLinear(100) + " = " + 447)





      function dblLinear(n) {
      let u = [1], x = 0, y = 0
      for (let i = 0; i < n; i++) {
      let nextX = 2 * u[x] + 1, nextY = 3 * u[y] + 1
      if (nextX <= nextY) {
      u.push(nextX)
      x++
      if (nextX == nextY)
      y++
      } else {
      u.push(nextY)
      y++
      }
      }
      return u[n]
      }

      console.log(dblLinear(10) + " = " + 22)
      console.log(dblLinear(20) + " = " + 57)
      console.log(dblLinear(30) + " = " + 91)
      console.log(dblLinear(50) + " = " + 175)
      console.log(dblLinear(100) + " = " + 447)






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 19 at 12:29

























      answered Mar 19 at 12:23









      maraca

      5,70831438




      5,70831438












      • Neat! This is very like Dijkstra's construction of Hamming Numbers. en.wikipedia.org/wiki/Regular_number#Algorithms
        – Paul Hankin
        Mar 19 at 13:54










      • I'm loving the implementation. Incredibly elegant! Thanks @maraca
        – Manuel Hong
        Mar 20 at 9:08


















      • Neat! This is very like Dijkstra's construction of Hamming Numbers. en.wikipedia.org/wiki/Regular_number#Algorithms
        – Paul Hankin
        Mar 19 at 13:54










      • I'm loving the implementation. Incredibly elegant! Thanks @maraca
        – Manuel Hong
        Mar 20 at 9:08
















      Neat! This is very like Dijkstra's construction of Hamming Numbers. en.wikipedia.org/wiki/Regular_number#Algorithms
      – Paul Hankin
      Mar 19 at 13:54




      Neat! This is very like Dijkstra's construction of Hamming Numbers. en.wikipedia.org/wiki/Regular_number#Algorithms
      – Paul Hankin
      Mar 19 at 13:54












      I'm loving the implementation. Incredibly elegant! Thanks @maraca
      – Manuel Hong
      Mar 20 at 9:08




      I'm loving the implementation. Incredibly elegant! Thanks @maraca
      – Manuel Hong
      Mar 20 at 9:08












      up vote
      3
      down vote













      The existing solution is great but my solution to this problem is






      function dblLinear(n) {
      let eq1Index = 0;
      let eq2Index = 0;
      let eq1Array = [3];
      let eq2Array = [4];
      let result = 1;
      for (let i = 1; i <= n; i++) {
      if (eq1Array[eq1Index] < eq2Array[eq2Index]) {
      result = eq1Array[eq1Index];
      eq1Index++;
      } else {
      result = eq2Array[eq2Index];
      eq2Index++;
      }

      eq2Array.indexOf(2 * result + 1) == -1 && eq1Array.push(2 * result + 1);

      eq2Array.push(3 * result + 1);
      }
      return result;
      }
      console.log(dblLinear(10) + " = " + 22)
      console.log(dblLinear(20) + " = " + 57)
      console.log(dblLinear(30) + " = " + 91)
      console.log(dblLinear(50) + " = " + 175)
      console.log(dblLinear(100) + " = " + 447)








      share|improve this answer



























        up vote
        3
        down vote













        The existing solution is great but my solution to this problem is






        function dblLinear(n) {
        let eq1Index = 0;
        let eq2Index = 0;
        let eq1Array = [3];
        let eq2Array = [4];
        let result = 1;
        for (let i = 1; i <= n; i++) {
        if (eq1Array[eq1Index] < eq2Array[eq2Index]) {
        result = eq1Array[eq1Index];
        eq1Index++;
        } else {
        result = eq2Array[eq2Index];
        eq2Index++;
        }

        eq2Array.indexOf(2 * result + 1) == -1 && eq1Array.push(2 * result + 1);

        eq2Array.push(3 * result + 1);
        }
        return result;
        }
        console.log(dblLinear(10) + " = " + 22)
        console.log(dblLinear(20) + " = " + 57)
        console.log(dblLinear(30) + " = " + 91)
        console.log(dblLinear(50) + " = " + 175)
        console.log(dblLinear(100) + " = " + 447)








        share|improve this answer

























          up vote
          3
          down vote










          up vote
          3
          down vote









          The existing solution is great but my solution to this problem is






          function dblLinear(n) {
          let eq1Index = 0;
          let eq2Index = 0;
          let eq1Array = [3];
          let eq2Array = [4];
          let result = 1;
          for (let i = 1; i <= n; i++) {
          if (eq1Array[eq1Index] < eq2Array[eq2Index]) {
          result = eq1Array[eq1Index];
          eq1Index++;
          } else {
          result = eq2Array[eq2Index];
          eq2Index++;
          }

          eq2Array.indexOf(2 * result + 1) == -1 && eq1Array.push(2 * result + 1);

          eq2Array.push(3 * result + 1);
          }
          return result;
          }
          console.log(dblLinear(10) + " = " + 22)
          console.log(dblLinear(20) + " = " + 57)
          console.log(dblLinear(30) + " = " + 91)
          console.log(dblLinear(50) + " = " + 175)
          console.log(dblLinear(100) + " = " + 447)








          share|improve this answer














          The existing solution is great but my solution to this problem is






          function dblLinear(n) {
          let eq1Index = 0;
          let eq2Index = 0;
          let eq1Array = [3];
          let eq2Array = [4];
          let result = 1;
          for (let i = 1; i <= n; i++) {
          if (eq1Array[eq1Index] < eq2Array[eq2Index]) {
          result = eq1Array[eq1Index];
          eq1Index++;
          } else {
          result = eq2Array[eq2Index];
          eq2Index++;
          }

          eq2Array.indexOf(2 * result + 1) == -1 && eq1Array.push(2 * result + 1);

          eq2Array.push(3 * result + 1);
          }
          return result;
          }
          console.log(dblLinear(10) + " = " + 22)
          console.log(dblLinear(20) + " = " + 57)
          console.log(dblLinear(30) + " = " + 91)
          console.log(dblLinear(50) + " = " + 175)
          console.log(dblLinear(100) + " = " + 447)








          function dblLinear(n) {
          let eq1Index = 0;
          let eq2Index = 0;
          let eq1Array = [3];
          let eq2Array = [4];
          let result = 1;
          for (let i = 1; i <= n; i++) {
          if (eq1Array[eq1Index] < eq2Array[eq2Index]) {
          result = eq1Array[eq1Index];
          eq1Index++;
          } else {
          result = eq2Array[eq2Index];
          eq2Index++;
          }

          eq2Array.indexOf(2 * result + 1) == -1 && eq1Array.push(2 * result + 1);

          eq2Array.push(3 * result + 1);
          }
          return result;
          }
          console.log(dblLinear(10) + " = " + 22)
          console.log(dblLinear(20) + " = " + 57)
          console.log(dblLinear(30) + " = " + 91)
          console.log(dblLinear(50) + " = " + 175)
          console.log(dblLinear(100) + " = " + 447)





          function dblLinear(n) {
          let eq1Index = 0;
          let eq2Index = 0;
          let eq1Array = [3];
          let eq2Array = [4];
          let result = 1;
          for (let i = 1; i <= n; i++) {
          if (eq1Array[eq1Index] < eq2Array[eq2Index]) {
          result = eq1Array[eq1Index];
          eq1Index++;
          } else {
          result = eq2Array[eq2Index];
          eq2Index++;
          }

          eq2Array.indexOf(2 * result + 1) == -1 && eq1Array.push(2 * result + 1);

          eq2Array.push(3 * result + 1);
          }
          return result;
          }
          console.log(dblLinear(10) + " = " + 22)
          console.log(dblLinear(20) + " = " + 57)
          console.log(dblLinear(30) + " = " + 91)
          console.log(dblLinear(50) + " = " + 175)
          console.log(dblLinear(100) + " = " + 447)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 6 at 10:43









          Moritz

          57k19131184




          57k19131184










          answered Oct 6 at 10:16









          Saurabh Siddhu

          685




          685






















              up vote
              2
              down vote













              The good solution for this




              function dblLinear(n) {
              var ai = 0, bi = 0, eq = 0;
              var sequence = [1];
              while (ai + bi < n + eq) {
              var y = 2 * sequence[ai] + 1;
              var z = 3 * sequence[bi] + 1;
              if (y < z) { sequence.push(y); ai++; }
              else if (y > z) { sequence.push(z); bi++; }
              else { sequence.push(y); ai++; bi++; eq++; }
              }
              return sequence.pop();
              }

              console.log(dblLinear(10) + " = " + 22)
              console.log(dblLinear(20) + " = " + 57)
              console.log(dblLinear(30) + " = " + 91)
              console.log(dblLinear(50) + " = " + 175)
              console.log(dblLinear(100) + " = " + 447)








              share|improve this answer



























                up vote
                2
                down vote













                The good solution for this




                function dblLinear(n) {
                var ai = 0, bi = 0, eq = 0;
                var sequence = [1];
                while (ai + bi < n + eq) {
                var y = 2 * sequence[ai] + 1;
                var z = 3 * sequence[bi] + 1;
                if (y < z) { sequence.push(y); ai++; }
                else if (y > z) { sequence.push(z); bi++; }
                else { sequence.push(y); ai++; bi++; eq++; }
                }
                return sequence.pop();
                }

                console.log(dblLinear(10) + " = " + 22)
                console.log(dblLinear(20) + " = " + 57)
                console.log(dblLinear(30) + " = " + 91)
                console.log(dblLinear(50) + " = " + 175)
                console.log(dblLinear(100) + " = " + 447)








                share|improve this answer

























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  The good solution for this




                  function dblLinear(n) {
                  var ai = 0, bi = 0, eq = 0;
                  var sequence = [1];
                  while (ai + bi < n + eq) {
                  var y = 2 * sequence[ai] + 1;
                  var z = 3 * sequence[bi] + 1;
                  if (y < z) { sequence.push(y); ai++; }
                  else if (y > z) { sequence.push(z); bi++; }
                  else { sequence.push(y); ai++; bi++; eq++; }
                  }
                  return sequence.pop();
                  }

                  console.log(dblLinear(10) + " = " + 22)
                  console.log(dblLinear(20) + " = " + 57)
                  console.log(dblLinear(30) + " = " + 91)
                  console.log(dblLinear(50) + " = " + 175)
                  console.log(dblLinear(100) + " = " + 447)








                  share|improve this answer














                  The good solution for this




                  function dblLinear(n) {
                  var ai = 0, bi = 0, eq = 0;
                  var sequence = [1];
                  while (ai + bi < n + eq) {
                  var y = 2 * sequence[ai] + 1;
                  var z = 3 * sequence[bi] + 1;
                  if (y < z) { sequence.push(y); ai++; }
                  else if (y > z) { sequence.push(z); bi++; }
                  else { sequence.push(y); ai++; bi++; eq++; }
                  }
                  return sequence.pop();
                  }

                  console.log(dblLinear(10) + " = " + 22)
                  console.log(dblLinear(20) + " = " + 57)
                  console.log(dblLinear(30) + " = " + 91)
                  console.log(dblLinear(50) + " = " + 175)
                  console.log(dblLinear(100) + " = " + 447)








                  function dblLinear(n) {
                  var ai = 0, bi = 0, eq = 0;
                  var sequence = [1];
                  while (ai + bi < n + eq) {
                  var y = 2 * sequence[ai] + 1;
                  var z = 3 * sequence[bi] + 1;
                  if (y < z) { sequence.push(y); ai++; }
                  else if (y > z) { sequence.push(z); bi++; }
                  else { sequence.push(y); ai++; bi++; eq++; }
                  }
                  return sequence.pop();
                  }

                  console.log(dblLinear(10) + " = " + 22)
                  console.log(dblLinear(20) + " = " + 57)
                  console.log(dblLinear(30) + " = " + 91)
                  console.log(dblLinear(50) + " = " + 175)
                  console.log(dblLinear(100) + " = " + 447)





                  function dblLinear(n) {
                  var ai = 0, bi = 0, eq = 0;
                  var sequence = [1];
                  while (ai + bi < n + eq) {
                  var y = 2 * sequence[ai] + 1;
                  var z = 3 * sequence[bi] + 1;
                  if (y < z) { sequence.push(y); ai++; }
                  else if (y > z) { sequence.push(z); bi++; }
                  else { sequence.push(y); ai++; bi++; eq++; }
                  }
                  return sequence.pop();
                  }

                  console.log(dblLinear(10) + " = " + 22)
                  console.log(dblLinear(20) + " = " + 57)
                  console.log(dblLinear(30) + " = " + 91)
                  console.log(dblLinear(50) + " = " + 175)
                  console.log(dblLinear(100) + " = " + 447)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 11 at 7:25









                  Saurabh Siddhu

                  685




                  685










                  answered Nov 11 at 7:16









                  Pratyush Gupta

                  285




                  285






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49360883%2fdynamic-programming-code-wars-twice-linear-algorithm-times-out%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python