summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHaidong Ji2018-10-02 21:46:45 -0500
committerHaidong Ji2018-10-02 21:46:45 -0500
commit109ade52e695184b54c68ff5426a8672d7443047 (patch)
treea3e39d9db75264edd96cc0fc0222f37dce42c7d7
parent46b8bc73e7a78bdf539f7c43d849c434fed35224 (diff)
Maximize salary done!
The compare algorithm provided by bytwigorlimerick was so clever and elegant! https://courses.edx.org/courses/course-v1:UCSanDiegoX+ALGS200x+2T2017/discussion/forum/course/threads/5a427dbf44a15008cd000701
-rw-r--r--AlgoDesignAndTechniqueEdxJava/sources/LargestNumber.java45
-rw-r--r--AlgoDesignAndTechniqueEdxJava/tests/LargestNumberTest.java10
2 files changed, 35 insertions, 20 deletions
diff --git a/AlgoDesignAndTechniqueEdxJava/sources/LargestNumber.java b/AlgoDesignAndTechniqueEdxJava/sources/LargestNumber.java
index 0458b91..1ddff1e 100644
--- a/AlgoDesignAndTechniqueEdxJava/sources/LargestNumber.java
+++ b/AlgoDesignAndTechniqueEdxJava/sources/LargestNumber.java
@@ -34,26 +34,31 @@ public class LargestNumber {
return result;
}
- public static boolean compareLeadingDigit(String string, String maxDigit) {
- if (string.length() == maxDigit.length())
- return Integer.parseInt(string) > Integer.parseInt(maxDigit);
- if (string.length() < maxDigit.length()) {
- if (string.charAt(0) > maxDigit.charAt(0)) {
- return true;
- } else if (string.charAt(0) < maxDigit.charAt(0)) {
- return false;
- } else if (string.charAt(0) > maxDigit.charAt(1)) {
- return true;
- } else if (string.charAt(0) < maxDigit.charAt(1)) {
- return false;
- } else if (maxDigit.length() == 2)
- return false;
- else {
- return string.charAt(0) > maxDigit.charAt(2);
- }
- } else {
- return !compareLeadingDigit(maxDigit, string);
- }
+ public static boolean compareLeadingDigit(String s, String maxDigit) {
+//Oh my god, the algorithm for this comparison was so clever mentioned
+//by bytwigorlimerick in this page:
+//https://courses.edx.org/courses/course-v1:UCSanDiegoX+ALGS200x+2T2017/discussion/forum/course/threads/5a427dbf44a15008cd000701
+//This solved the problem nicely and easily, so elegant!
+ return Integer.parseInt(s + maxDigit) > Integer.parseInt(maxDigit + s);
+// if (s.length() == maxDigit.length())
+// return Integer.parseInt(s) > Integer.parseInt(maxDigit);
+// if (s.length() < maxDigit.length()) {
+// if (s.charAt(0) > maxDigit.charAt(0)) {
+// return true;
+// } else if (s.charAt(0) < maxDigit.charAt(0)) {
+// return false;
+// } else if (s.charAt(0) > maxDigit.charAt(1)) {
+// return true;
+// } else if (s.length() == 1) {
+// if (s.charAt(0) > maxDigit.charAt(1))
+// return true;
+// else
+// return false;
+// } else
+// return compareLeadingDigit(s.substring(1), maxDigit.substring(1));
+// } else {
+// return !compareLeadingDigit(maxDigit, s);
+// }
}
}
diff --git a/AlgoDesignAndTechniqueEdxJava/tests/LargestNumberTest.java b/AlgoDesignAndTechniqueEdxJava/tests/LargestNumberTest.java
index 285d502..4d62c6b 100644
--- a/AlgoDesignAndTechniqueEdxJava/tests/LargestNumberTest.java
+++ b/AlgoDesignAndTechniqueEdxJava/tests/LargestNumberTest.java
@@ -68,5 +68,15 @@ class LargestNumberTest {
void testCompareLeadingDigit6() {
assertTrue(LargestNumber.compareLeadingDigit("23", "2"));
}
+
+ @Test
+ void testCompareLeadingDigit7() {
+ assertTrue(LargestNumber.compareLeadingDigit("545", "54"));
+ }
+
+ @Test
+ void testCompareLeadingDigit8() {
+ assertTrue(LargestNumber.compareLeadingDigit("56", "565"));
+ }
}