Skip to content

Conversation

@ErdemT09
Copy link
Collaborator

@ErdemT09 ErdemT09 commented Jun 6, 2021

Resolves: #47

The thing that we have to notice is that select all and copy operations are always done together. After having copied some 'A's, it is always (except when we have copied just one character, then it is just equal) more profitable to paste than to insert new single characters. After copying:

  • pasting 1 time would mean doubling the count (cost = 3)
  • pasting 2 times would mean tripling the count (cost = 4)
  • pasting 3 times would mean quadrupling the count (cost = 5)
  • pasting 4 times would mean quintupling the count (cost = 6)
  • pasting W-2 times would mean multiplying the count by W-1 (cost = W)

Multiplying by 6 is never more profitable than multiplying by 3 and then 2 (cost 7 vs 7), or by 7 never more profitable than multiplying by 4 and then 2 (cost 8 vs 8, but actually 1 more character gain). Referring to the image, multiplying by any number greater than 5 (N*2 = 6) is never profitable.
image
Multiplying by 2 two times is never more profitable than multiplying by multiplying by four.
That is why only amount of multiplications that we should consider are 3, 4 and 5.

At some presses P, we can always go back to

  • the best of 6 presses ago and multiply it by 5 and compare
  • the best of 5 presses ago and multiply it by 4 and compare
  • the best of 4 presses ago and multiply it by 3 and compare

In this way, our algorithm stores the best of previous number of presses and compares them with the current.

Eventually, 4 must be the most optimal choice:
For the same cost of 20, we can either multiply by a = 3^5 or a < 4^4
For the same cost of 30, we can either multiply by a = 5^5 or a < 4^6

Therefore, we can state that we will always be multiplying by 4 from some point on. We can calculate that point using the dynamic programming algorithm and just have it some lookup array. If the given number of presses is larger than the size of that array, we just look up its corresponding value 5*A ago and multiply that looked up value by 4^A.

@altay9
Copy link
Collaborator

altay9 commented Jun 6, 2021

Then, the reason that "3" (AAA) is the basis for the solution is: Ctrl-A + Ctrl-C + Ctrl-V can be done in at least 3 steps.
If one had just "n=3" unit currency, then he would spend it to insert just three single chars instead of using Ctrl-A + Ctrl-C + Ctrl-V

@ErdemT09
Copy link
Collaborator Author

ErdemT09 commented Jun 6, 2021

Then, the reason that "3" (AAA) is the basis for the solution is: Ctrl-A + Ctrl-C + Ctrl-V can be done in at least 3 steps.
If one had just "n=3" unit currency, then he would spend it to insert just three single chars instead of using Ctrl-A + Ctrl-C + Ctrl-V

True, until 7, we don't do anything more than incrementing.

@ErdemT09 ErdemT09 marked this pull request as ready for review June 6, 2021 16:07
@altay9
Copy link
Collaborator

altay9 commented Jun 6, 2021

Then, the reason that "3" (AAA) is the basis for the solution is: Ctrl-A + Ctrl-C + Ctrl-V can be done in at least 3 steps.

If one had just "n=3" unit currency, then he would spend it to insert just three single chars instead of using Ctrl-A + Ctrl-C + Ctrl-V

True, until 7, we don't do anything more than incrementing.

Then can we claim:
If n<7, f(n)=n

@altay9 altay9 closed this Jun 6, 2021
@altay9 altay9 reopened this Jun 6, 2021
@@ -0,0 +1,27 @@
package algorithms.curated170.medium.fourkeyskeyboard;

public class FourKeysKeyboardDP {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Comment on lines 11 to 13
best[press] = Math.max(best[press], best[press - 6] * 5);
best[press] = Math.max(best[press], best[press - 5] * 4);
best[press] = Math.max(best[press], best[press - 4] * 3);
Copy link
Collaborator

@altay9 altay9 Jun 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can choose:

Suggested change
best[press] = Math.max(best[press], best[press - 6] * 5);
best[press] = Math.max(best[press], best[press - 5] * 4);
best[press] = Math.max(best[press], best[press - 4] * 3);
best[press] = Math.max(best[press], best[press - 3] * 2);
best[press] = Math.max(best[press], best[press - 4] * 3);
best[press] = Math.max(best[press], best[press - 5] * 4);

It is more relatable for me. Because it represents the selection between those choices cleaner:
after f[i - 3], by ACP total length : f[i - 3] * 2
after f[i - 4], by ACP + P total length : f[i - 4] * 3
after f[i - 5], by ACP + PP total length : f[i - 5] * 4

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be a multiplication by 3, 4 and 5. Otherwise, I'm changing then.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should also work like that:

               best[press] = Math.max(best[press], best[press - 3] * 2);
                best[press] = Math.max(best[press], best[press - 4] * 3);
                best[press] = Math.max(best[press], best[press - 5] * 4);

But no problem. This is fine. I approve.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we never multiply by 5. But theoretically, we may be.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe there is a test-case we need. But according to LeetCode, we don't need it.

image

@@ -0,0 +1,23 @@
package algorithms.curated170.medium.fourkeyskeyboard;

public class FourKeysKeyboardMath {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Collaborator

@altay9 altay9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your valuable contribution.

@ErdemT09 ErdemT09 merged commit ea4a022 into master Jun 6, 2021
@ErdemT09 ErdemT09 deleted the 651.-4-Keys-Keyboard branch June 6, 2021 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

651. 4 Keys Keyboard

3 participants