-
Notifications
You must be signed in to change notification settings - Fork 7
651. 4 Keys Keyboard #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
651. 4 Keys Keyboard #301
Conversation
|
Then, the reason that "3" (AAA) is the basis for the solution is: |
True, until 7, we don't do anything more than incrementing. |
Then can we claim: |
| @@ -0,0 +1,27 @@ | |||
| package algorithms.curated170.medium.fourkeyskeyboard; | |||
|
|
|||
| public class FourKeysKeyboardDP { | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can choose:
| 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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| @@ -0,0 +1,23 @@ | |||
| package algorithms.curated170.medium.fourkeyskeyboard; | |||
|
|
|||
| public class FourKeysKeyboardMath { | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
altay9
left a comment
There was a problem hiding this 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.



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:
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.

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
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.