Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added tasks 639, 640, 641, 643 #255

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

Merged
javadev merged 3 commits into javadev:main from ThanhNIT:tasks-639-640-641-643
Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/main/java/g0601_0700/s0639_decode_ways_ii/Solution.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package g0601_0700.s0639_decode_ways_ii;

// #Hard #String #Dynamic_Programming

public class Solution {
Copy link
Owner

Choose a reason for hiding this comment

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

Please check unit tests. Code coverage is too low.

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()];
}
}
49 changes: 49 additions & 0 deletions src/main/java/g0601_0700/s0639_decode_ways_ii/readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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** <code>10<sup>9</sup> + 7</code>.

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

* <code>1 <= s.length <= 10<sup>5</sup></code>
* `s[i]` is a digit or `'*'`.
71 changes: 71 additions & 0 deletions src/main/java/g0601_0700/s0640_solve_the_equation/Solution.java
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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;
}
}
31 changes: 31 additions & 0 deletions src/main/java/g0601_0700/s0640_solve_the_equation/readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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'`.
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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;
}
}
55 changes: 55 additions & 0 deletions src/main/java/g0601_0700/s0641_design_circular_deque/readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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`.
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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;
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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 <code>10<sup>-5</sup></code> 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`
* <code>1 <= k <= n <= 10<sup>5</sup></code>
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
Loading

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