diff --git a/src/main/java/g0601_0700/s0639_decode_ways_ii/Solution.java b/src/main/java/g0601_0700/s0639_decode_ways_ii/Solution.java new file mode 100644 index 000000000..a8c4c26f1 --- /dev/null +++ b/src/main/java/g0601_0700/s0639_decode_ways_ii/Solution.java @@ -0,0 +1,51 @@ +package g0601_0700.s0639_decode_ways_ii; + +// #Hard #String #Dynamic_Programming + +public class Solution { + public int numDecodings(String s) { + if (s.charAt(0) == '0') { + return 0; + } + long[] dp = new long[s.length() + 1]; + dp[0] = 1; + dp[1] = s.charAt(0) == '*' ? 9 : 1; + char[] ch = s.toCharArray(); + + for (int i = 2; i <= ch.length; i++) { + if (ch[i - 1] == '0') { + if (ch[i - 2] == '1' || ch[i - 2] == '2') { + dp[i] = dp[i - 2]; + } else if (ch[i - 2] == '*') { + dp[i] = 2 * dp[i - 2]; + } else { + return 0; + } + } else if (ch[i - 1]>= '1' && ch[i - 1] <= '6') { + dp[i] = dp[i - 1]; + if (ch[i - 2] == '1' || ch[i - 2] == '2') { + dp[i] += dp[i - 2]; + } else if (ch[i - 2] == '*') { + dp[i] += 2 * dp[i - 2]; + } + } else if (ch[i - 1]>= '7' && ch[i - 1] <= '9') { + dp[i] = dp[i - 1]; + if (ch[i - 2] == '1' || ch[i - 2] == '*') { + dp[i] += dp[i - 2]; + } + } else if (ch[i - 1] == '*') { + dp[i] = 9 * dp[i - 1]; + if (ch[i - 2] == '1') { + dp[i] += 9 * dp[i - 2]; + } else if (ch[i - 2] == '2') { + dp[i] += 6 * dp[i - 2]; + } else if (ch[i - 2] == '*') { + dp[i] += 15 * dp[i - 2]; + } + } + dp[i] %= 1000000007; + } + + return (int) dp[s.length()]; + } +} diff --git a/src/main/java/g0601_0700/s0639_decode_ways_ii/readme.md b/src/main/java/g0601_0700/s0639_decode_ways_ii/readme.md new file mode 100644 index 000000000..73636800d --- /dev/null +++ b/src/main/java/g0601_0700/s0639_decode_ways_ii/readme.md @@ -0,0 +1,49 @@ +639\. Decode Ways II + +Hard + +A message containing letters from `A-Z` can be **encoded** into numbers using the following mapping: + +'A' -> "1" 'B' -> "2" ... 'Z' -> "26" + +To **decode** an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, `"11106"` can be mapped into: + +* `"AAJF"` with the grouping `(1 1 10 6)` +* `"KJF"` with the grouping `(11 10 6)` + +Note that the grouping `(1 11 06)` is invalid because `"06"` cannot be mapped into `'F'` since `"6"` is different from `"06"`. + +**In addition** to the mapping above, an encoded message may contain the `'*'` character, which can represent any digit from `'1'` to `'9'` (`'0'` is excluded). For example, the encoded message `"1*"` may represent any of the encoded messages `"11"`, `"12"`, `"13"`, `"14"`, `"15"`, `"16"`, `"17"`, `"18"`, or `"19"`. Decoding `"1*"` is equivalent to decoding **any** of the encoded messages it can represent. + +Given a string `s` consisting of digits and `'*'` characters, return _the **number** of ways to **decode** it_. + +Since the answer may be very large, return it **modulo** 109 + 7. + +**Example 1:** + +**Input:** s = "\*" + +**Output:** 9 + +**Explanation:** The encoded message can represent any of the encoded messages "1", "2", "3", "4", "5", "6", "7", "8", or "9". Each of these can be decoded to the strings "A", "B", "C", "D", "E", "F", "G", "H", and "I" respectively. Hence, there are a total of 9 ways to decode "\*". + +**Example 2:** + +**Input:** s = "1\*" + +**Output:** 18 + +**Explanation:** The encoded message can represent any of the encoded messages "11", "12", "13", "14", "15", "16", "17", "18", or "19". Each of these encoded messages have 2 ways to be decoded (e.g. "11" can be decoded to "AA" or "K"). Hence, there are a total of 9 \* 2 = 18 ways to decode "1\*". + +**Example 3:** + +**Input:** s = "2\*" + +**Output:** 15 + +**Explanation:** The encoded message can represent any of the encoded messages "21", "22", "23", "24", "25", "26", "27", "28", or "29". "21", "22", "23", "24", "25", and "26" have 2 ways of being decoded, but "27", "28", and "29" only have 1 way. Hence, there are a total of (6 \* 2) + (3 \* 1) = 12 + 3 = 15 ways to decode "2\*". + +**Constraints:** + +* 1 <= s.length <= 105 +* `s[i]` is a digit or `'*'`. \ No newline at end of file diff --git a/src/main/java/g0601_0700/s0640_solve_the_equation/Solution.java b/src/main/java/g0601_0700/s0640_solve_the_equation/Solution.java new file mode 100644 index 000000000..cdd3ff168 --- /dev/null +++ b/src/main/java/g0601_0700/s0640_solve_the_equation/Solution.java @@ -0,0 +1,71 @@ +package g0601_0700.s0640_solve_the_equation; + +// #Medium #String #Math #Simulation + +public class Solution { + public String solveEquation(String equation) { + String[] eqs = equation.split("="); + + int[] arr1 = evaluate(eqs[0]); + int[] arr2 = evaluate(eqs[1]); + + if (arr1[0] == arr2[0] && arr1[1] == arr2[1]) { + return "Infinite solutions"; + } else if (arr1[0] == arr2[0]) { + return "No solution"; + } else { + return "x=" + (arr2[1] - arr1[1]) / (arr1[0] - arr2[0]); + } + } + + private int[] evaluate(String eq) { + char[] arr = eq.toCharArray(); + boolean f = false; + int a = 0; + int b = 0; + + int i = 0; + if (arr[0] == '-') { + f = true; + i++; + } + while (i < arr.length) { + if (arr[i] == '-') { + f = true; + i++; + } else if (arr[i] == '+') { + i++; + } + StringBuilder sb = new StringBuilder(); + while (i < arr.length && Character.isDigit(arr[i])) { + sb.append(arr[i]); + i++; + } + String n = sb.toString(); + if (i < arr.length && arr[i] == 'x') { + int number; + if (n.equals("")) { + number = 1; + } else { + number = Integer.parseInt(n); + } + if (f) { + number = -number; + } + a += number; + i++; + } else { + int number = Integer.parseInt(n); + if (f) { + number = -number; + } + b += number; + } + f = false; + } + int[] op = new int[2]; + op[0] = a; + op[1] = b; + return op; + } +} diff --git a/src/main/java/g0601_0700/s0640_solve_the_equation/readme.md b/src/main/java/g0601_0700/s0640_solve_the_equation/readme.md new file mode 100644 index 000000000..461aed1d0 --- /dev/null +++ b/src/main/java/g0601_0700/s0640_solve_the_equation/readme.md @@ -0,0 +1,31 @@ +640\. Solve the Equation + +Medium + +Solve a given equation and return the value of `'x'` in the form of a string `"x=#value"`. The equation contains only `'+'`, `'-'` operation, the variable `'x'` and its coefficient. You should return `"No solution"` if there is no solution for the equation, or `"Infinite solutions"` if there are infinite solutions for the equation. + +If there is exactly one solution for the equation, we ensure that the value of `'x'` is an integer. + +**Example 1:** + +**Input:** equation = "x+5-3+x=6+x-2" + +**Output:** "x=2" + +**Example 2:** + +**Input:** equation = "x=x" + +**Output:** "Infinite solutions" + +**Example 3:** + +**Input:** equation = "2x=x" + +**Output:** "x=0" + +**Constraints:** + +* `3 <= equation.length <= 1000` +* `equation` has exactly one `'='`. +* `equation` consists of integers with an absolute value in the range `[0, 100]` without any leading zeros, and the variable `'x'`. \ No newline at end of file diff --git a/src/main/java/g0601_0700/s0641_design_circular_deque/MyCircularDeque.java b/src/main/java/g0601_0700/s0641_design_circular_deque/MyCircularDeque.java new file mode 100644 index 000000000..44c1ea60d --- /dev/null +++ b/src/main/java/g0601_0700/s0641_design_circular_deque/MyCircularDeque.java @@ -0,0 +1,78 @@ +package g0601_0700.s0641_design_circular_deque; + +// #Medium #Array #Design #Linked_List #Queue + +public class MyCircularDeque { + private final int[] data; + private int front; + private int rear; + private int size; + + public MyCircularDeque(int k) { + data = new int[k]; + front = 0; + rear = k - 1; + size = 0; + } + + public boolean insertFront(int value) { + if (size == data.length) { + return false; + } + data[front] = value; + front = (front + 1) % data.length; + size++; + return true; + } + + public boolean insertLast(int value) { + if (size == data.length) { + return false; + } + data[rear] = value; + rear = (rear - 1 + data.length) % data.length; + size++; + return true; + } + + public boolean deleteFront() { + if (size == 0) { + return false; + } + front = (front - 1 + data.length) % data.length; + size--; + return true; + } + + public boolean deleteLast() { + if (size == 0) { + return false; + } + rear = (rear + 1) % data.length; + size--; + return true; + } + + public int getFront() { + if (size == 0) { + return -1; + } + + return data[(front - 1 + data.length) % data.length]; + } + + public int getRear() { + if (size == 0) { + return -1; + } + return data[(rear + 1) % data.length]; + } + + public boolean isEmpty() { + return size == 0; + } + + public boolean isFull() { + return size == data.length; + } +} diff --git a/src/main/java/g0601_0700/s0641_design_circular_deque/readme.md b/src/main/java/g0601_0700/s0641_design_circular_deque/readme.md new file mode 100644 index 000000000..31b5ade56 --- /dev/null +++ b/src/main/java/g0601_0700/s0641_design_circular_deque/readme.md @@ -0,0 +1,55 @@ +641\. Design Circular Deque + +Medium + +Design your implementation of the circular double-ended queue (deque). + +Implement the `MyCircularDeque` class: + +* `MyCircularDeque(int k)` Initializes the deque with a maximum size of `k`. +* `boolean insertFront()` Adds an item at the front of Deque. Returns `true` if the operation is successful, or `false` otherwise. +* `boolean insertLast()` Adds an item at the rear of Deque. Returns `true` if the operation is successful, or `false` otherwise. +* `boolean deleteFront()` Deletes an item from the front of Deque. Returns `true` if the operation is successful, or `false` otherwise. +* `boolean deleteLast()` Deletes an item from the rear of Deque. Returns `true` if the operation is successful, or `false` otherwise. +* `int getFront()` Returns the front item from the Deque. Returns `-1` if the deque is empty. +* `int getRear()` Returns the last item from Deque. Returns `-1` if the deque is empty. +* `boolean isEmpty()` Returns `true` if the deque is empty, or `false` otherwise. +* `boolean isFull()` Returns `true` if the deque is full, or `false` otherwise. + +**Example 1:** + +**Input** + +["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"] + +[[3], [1], [2], [3], [4], [], [], [], [4], []] + +**Output:** [null, true, true, true, false, 2, true, true, true, 4] + +**Explanation:** + +MyCircularDeque myCircularDeque = new MyCircularDeque(3); + +myCircularDeque.insertLast(1); // return True + +myCircularDeque.insertLast(2); // return True + +myCircularDeque.insertFront(3); // return True + +myCircularDeque.insertFront(4); // return False, the queue is full. + +myCircularDeque.getRear(); // return 2 + +myCircularDeque.isFull(); // return True + +myCircularDeque.deleteLast(); // return True + +myCircularDeque.insertFront(4); // return True + +myCircularDeque.getFront(); // return 4 + +**Constraints:** + +* `1 <= k <= 1000` +* `0 <= value <= 1000` +* At most `2000` calls will be made to `insertFront`, `insertLast`, `deleteFront`, `deleteLast`, `getFront`, `getRear`, `isEmpty`, `isFull`. \ No newline at end of file diff --git a/src/main/java/g0601_0700/s0643_maximum_average_subarray_i/Solution.java b/src/main/java/g0601_0700/s0643_maximum_average_subarray_i/Solution.java new file mode 100644 index 000000000..09dbe36bf --- /dev/null +++ b/src/main/java/g0601_0700/s0643_maximum_average_subarray_i/Solution.java @@ -0,0 +1,21 @@ +package g0601_0700.s0643_maximum_average_subarray_i; + +// #Easy #Array #Sliding_Window + +public class Solution { + public double findMaxAverage(int[] nums, int k) { + double windowSum = 0; + int windowStart = 0; + double max = Integer.MIN_VALUE; + for (int windowEnd = 0; windowEnd < nums.length; ++windowEnd) { + windowSum += nums[windowEnd]; + if (windowEnd>= k - 1) { + double candidate = windowSum / k; + max = Math.max(candidate, max); + windowSum -= nums[windowStart]; + windowStart++; + } + } + return max; + } +} diff --git a/src/main/java/g0601_0700/s0643_maximum_average_subarray_i/readme.md b/src/main/java/g0601_0700/s0643_maximum_average_subarray_i/readme.md new file mode 100644 index 000000000..8bdc13477 --- /dev/null +++ b/src/main/java/g0601_0700/s0643_maximum_average_subarray_i/readme.md @@ -0,0 +1,27 @@ +643\. Maximum Average Subarray I + +Easy + +You are given an integer array `nums` consisting of `n` elements, and an integer `k`. + +Find a contiguous subarray whose **length is equal to** `k` that has the maximum average value and return _this value_. Any answer with a calculation error less than 10-5 will be accepted. + +**Example 1:** + +**Input:** nums = [1,12,-5,-6,50,3], k = 4 + +**Output:** 12.75000 + +**Explanation:** Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75 + +**Example 2:** + +**Input:** nums = [5], k = 1 + +**Output:** 5.00000 + +**Constraints:** + +* `n == nums.length` +* 1 <= k <= n <= 105 +* -104 <= nums[i] <= 104 \ No newline at end of file diff --git a/src/test/java/g0601_0700/s0639_decode_ways_ii/SolutionTest.java b/src/test/java/g0601_0700/s0639_decode_ways_ii/SolutionTest.java new file mode 100644 index 000000000..0c0dc7f4f --- /dev/null +++ b/src/test/java/g0601_0700/s0639_decode_ways_ii/SolutionTest.java @@ -0,0 +1,23 @@ +package g0601_0700.s0639_decode_ways_ii; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void numDecodings() { + assertThat(new Solution().numDecodings("*"), equalTo(9)); + } + + @Test + void numDecodings2() { + assertThat(new Solution().numDecodings("1*"), equalTo(18)); + } + + @Test + void numDecodings3() { + assertThat(new Solution().numDecodings("2*"), equalTo(15)); + } +} diff --git a/src/test/java/g0601_0700/s0640_solve_the_equation/SolutionTest.java b/src/test/java/g0601_0700/s0640_solve_the_equation/SolutionTest.java new file mode 100644 index 000000000..f362985ed --- /dev/null +++ b/src/test/java/g0601_0700/s0640_solve_the_equation/SolutionTest.java @@ -0,0 +1,23 @@ +package g0601_0700.s0640_solve_the_equation; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void solveEquation() { + assertThat(new Solution().solveEquation("x+5-3+x=6+x-2"), equalTo("x=2")); + } + + @Test + void solveEquation2() { + assertThat(new Solution().solveEquation("x=x"), equalTo("Infinite solutions")); + } + + @Test + void solveEquation3() { + assertThat(new Solution().solveEquation("2x=x"), equalTo("x=0")); + } +} diff --git a/src/test/java/g0601_0700/s0641_design_circular_deque/MyCircularDequeTest.java b/src/test/java/g0601_0700/s0641_design_circular_deque/MyCircularDequeTest.java new file mode 100644 index 000000000..264096020 --- /dev/null +++ b/src/test/java/g0601_0700/s0641_design_circular_deque/MyCircularDequeTest.java @@ -0,0 +1,22 @@ +package g0601_0700.s0641_design_circular_deque; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class MyCircularDequeTest { + @Test + void myCircularDequeTest() { + MyCircularDeque myCircularDeque = new MyCircularDeque(3); + assertThat(myCircularDeque.insertLast(1), equalTo(true)); + assertThat(myCircularDeque.insertLast(2), equalTo(true)); + assertThat(myCircularDeque.insertFront(3), equalTo(true)); + assertThat(myCircularDeque.insertFront(4), equalTo(false)); + assertThat(myCircularDeque.getRear(), equalTo(2)); + assertThat(myCircularDeque.isFull(), equalTo(true)); + assertThat(myCircularDeque.deleteLast(), equalTo(true)); + assertThat(myCircularDeque.insertFront(4), equalTo(true)); + assertThat(myCircularDeque.getFront(), equalTo(4)); + } +} diff --git a/src/test/java/g0601_0700/s0643_maximum_average_subarray_i/SolutionTest.java b/src/test/java/g0601_0700/s0643_maximum_average_subarray_i/SolutionTest.java new file mode 100644 index 000000000..b38e88707 --- /dev/null +++ b/src/test/java/g0601_0700/s0643_maximum_average_subarray_i/SolutionTest.java @@ -0,0 +1,20 @@ +package g0601_0700.s0643_maximum_average_subarray_i; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void findMaxAverage() { + assertThat( + new Solution().findMaxAverage(new int[] {1, 12, -5, -6, 50, 3}, 4), + equalTo(12.75000)); + } + + @Test + void findMaxAverage2() { + assertThat(new Solution().findMaxAverage(new int[] {5}, 1), equalTo(5.00000)); + } +}

AltStyle によって変換されたページ (->オリジナル) /