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

Commit 1111d0b

Browse files
Added tasks 639, 640, 641, 643.
1 parent 966beaa commit 1111d0b

File tree

12 files changed

+471
-0
lines changed

12 files changed

+471
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package g0601_0700.s0639_decode_ways_ii;
2+
3+
// #Hard #String #Dynamic_Programming
4+
5+
public class Solution {
6+
public int numDecodings(String s) {
7+
if (s.charAt(0) == '0') {
8+
return 0;
9+
}
10+
long[] dp = new long[s.length() + 1];
11+
dp[0] = 1;
12+
dp[1] = s.charAt(0) == '*' ? 9 : 1;
13+
char[] ch = s.toCharArray();
14+
15+
for (int i = 2; i <= ch.length; i++) {
16+
if (ch[i - 1] == '0') {
17+
if (ch[i - 2] == '1' || ch[i - 2] == '2') {
18+
dp[i] = dp[i - 2];
19+
} else if (ch[i - 2] == '*') {
20+
dp[i] = 2 * dp[i - 2];
21+
} else {
22+
return 0;
23+
}
24+
} else if (ch[i - 1] >= '1' && ch[i - 1] <= '6') {
25+
dp[i] = dp[i - 1];
26+
if (ch[i - 2] == '1' || ch[i - 2] == '2') {
27+
dp[i] += dp[i - 2];
28+
} else if (ch[i - 2] == '*') {
29+
dp[i] += 2 * dp[i - 2];
30+
}
31+
} else if (ch[i - 1] >= '7' && ch[i - 1] <= '9') {
32+
dp[i] = dp[i - 1];
33+
if (ch[i - 2] == '1' || ch[i - 2] == '*') {
34+
dp[i] += dp[i - 2];
35+
}
36+
} else if (ch[i - 1] == '*') {
37+
dp[i] = 9 * dp[i - 1];
38+
if (ch[i - 2] == '1') {
39+
dp[i] += 9 * dp[i - 2];
40+
} else if (ch[i - 2] == '2') {
41+
dp[i] += 6 * dp[i - 2];
42+
} else if (ch[i - 2] == '*') {
43+
dp[i] += 15 * dp[i - 2];
44+
}
45+
}
46+
dp[i] %= 1000000007;
47+
}
48+
49+
return (int) dp[s.length()];
50+
}
51+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
639\. Decode Ways II
2+
3+
Hard
4+
5+
A message containing letters from `A-Z` can be **encoded** into numbers using the following mapping:
6+
7+
'A' -> "1" 'B' -> "2" ... 'Z' -> "26"
8+
9+
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:
10+
11+
* `"AAJF"` with the grouping `(1 1 10 6)`
12+
* `"KJF"` with the grouping `(11 10 6)`
13+
14+
Note that the grouping `(1 11 06)` is invalid because `"06"` cannot be mapped into `'F'` since `"6"` is different from `"06"`.
15+
16+
**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.
17+
18+
Given a string `s` consisting of digits and `'*'` characters, return _the **number** of ways to **decode** it_.
19+
20+
Since the answer may be very large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
21+
22+
**Example 1:**
23+
24+
**Input:** s = "\*"
25+
26+
**Output:** 9
27+
28+
**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 "\*".
29+
30+
**Example 2:**
31+
32+
**Input:** s = "1\*"
33+
34+
**Output:** 18
35+
36+
**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\*".
37+
38+
**Example 3:**
39+
40+
**Input:** s = "2\*"
41+
42+
**Output:** 15
43+
44+
**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\*".
45+
46+
**Constraints:**
47+
48+
* <code>1 <= s.length <= 10<sup>5</sup></code>
49+
* `s[i]` is a digit or `'*'`.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package g0601_0700.s0640_solve_the_equation;
2+
3+
// #Medium #String #Math #Simulation
4+
5+
public class Solution {
6+
public String solveEquation(String equation) {
7+
String[] eqs = equation.split("=");
8+
9+
int[] arr1 = evaluate(eqs[0]);
10+
int[] arr2 = evaluate(eqs[1]);
11+
12+
if (arr1[0] == arr2[0] && arr1[1] == arr2[1]) {
13+
return "Infinite solutions";
14+
} else if (arr1[0] == arr2[0]) {
15+
return "No solution";
16+
} else {
17+
return "x=" + (arr2[1] - arr1[1]) / (arr1[0] - arr2[0]);
18+
}
19+
}
20+
21+
private int[] evaluate(String eq) {
22+
char[] arr = eq.toCharArray();
23+
boolean f = false;
24+
int a = 0;
25+
int b = 0;
26+
27+
int i = 0;
28+
if (arr[0] == '-') {
29+
f = true;
30+
i++;
31+
}
32+
while (i < arr.length) {
33+
if (arr[i] == '-') {
34+
f = true;
35+
i++;
36+
} else if (arr[i] == '+') {
37+
i++;
38+
}
39+
StringBuilder sb = new StringBuilder();
40+
while (i < arr.length && Character.isDigit(arr[i])) {
41+
sb.append(arr[i]);
42+
i++;
43+
}
44+
String n = sb.toString();
45+
if (i < arr.length && arr[i] == 'x') {
46+
int number;
47+
if (n.equals("")) {
48+
number = 1;
49+
} else {
50+
number = Integer.parseInt(n);
51+
}
52+
if (f) {
53+
number = -number;
54+
}
55+
a += number;
56+
i++;
57+
} else {
58+
int number = Integer.parseInt(n);
59+
if (f) {
60+
number = -number;
61+
}
62+
b += number;
63+
}
64+
f = false;
65+
}
66+
int[] op = new int[2];
67+
op[0] = a;
68+
op[1] = b;
69+
return op;
70+
}
71+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
640\. Solve the Equation
2+
3+
Medium
4+
5+
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.
6+
7+
If there is exactly one solution for the equation, we ensure that the value of `'x'` is an integer.
8+
9+
**Example 1:**
10+
11+
**Input:** equation = "x+5-3+x=6+x-2"
12+
13+
**Output:** "x=2"
14+
15+
**Example 2:**
16+
17+
**Input:** equation = "x=x"
18+
19+
**Output:** "Infinite solutions"
20+
21+
**Example 3:**
22+
23+
**Input:** equation = "2x=x"
24+
25+
**Output:** "x=0"
26+
27+
**Constraints:**
28+
29+
* `3 <= equation.length <= 1000`
30+
* `equation` has exactly one `'='`.
31+
* `equation` consists of integers with an absolute value in the range `[0, 100]` without any leading zeros, and the variable `'x'`.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package g0601_0700.s0641_design_circular_deque;
2+
3+
// #Medium #Array #Design #Linked_List #Queue
4+
5+
public class MyCircularDeque {
6+
private final int[] data;
7+
private int front;
8+
private int rear;
9+
private int size;
10+
11+
public MyCircularDeque(int k) {
12+
data = new int[k];
13+
front = 0;
14+
rear = k - 1;
15+
size = 0;
16+
}
17+
18+
public boolean insertFront(int value) {
19+
if (size == data.length) {
20+
return false;
21+
}
22+
data[front] = value;
23+
front = (front + 1) % data.length;
24+
size++;
25+
return true;
26+
}
27+
28+
public boolean insertLast(int value) {
29+
if (size == data.length) {
30+
return false;
31+
}
32+
data[rear] = value;
33+
rear = (rear - 1 + data.length) % data.length;
34+
size++;
35+
return true;
36+
}
37+
38+
public boolean deleteFront() {
39+
if (size == 0) {
40+
return false;
41+
}
42+
front = (front - 1 + data.length) % data.length;
43+
size--;
44+
return true;
45+
}
46+
47+
public boolean deleteLast() {
48+
if (size == 0) {
49+
return false;
50+
}
51+
rear = (rear + 1) % data.length;
52+
size--;
53+
return true;
54+
}
55+
56+
public int getFront() {
57+
if (size == 0) {
58+
return -1;
59+
}
60+
61+
return data[(front - 1 + data.length) % data.length];
62+
}
63+
64+
public int getRear() {
65+
if (size == 0) {
66+
return -1;
67+
}
68+
return data[(rear + 1) % data.length];
69+
}
70+
71+
public boolean isEmpty() {
72+
return size == 0;
73+
}
74+
75+
public boolean isFull() {
76+
return size == data.length;
77+
}
78+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
641\. Design Circular Deque
2+
3+
Medium
4+
5+
Design your implementation of the circular double-ended queue (deque).
6+
7+
Implement the `MyCircularDeque` class:
8+
9+
* `MyCircularDeque(int k)` Initializes the deque with a maximum size of `k`.
10+
* `boolean insertFront()` Adds an item at the front of Deque. Returns `true` if the operation is successful, or `false` otherwise.
11+
* `boolean insertLast()` Adds an item at the rear of Deque. Returns `true` if the operation is successful, or `false` otherwise.
12+
* `boolean deleteFront()` Deletes an item from the front of Deque. Returns `true` if the operation is successful, or `false` otherwise.
13+
* `boolean deleteLast()` Deletes an item from the rear of Deque. Returns `true` if the operation is successful, or `false` otherwise.
14+
* `int getFront()` Returns the front item from the Deque. Returns `-1` if the deque is empty.
15+
* `int getRear()` Returns the last item from Deque. Returns `-1` if the deque is empty.
16+
* `boolean isEmpty()` Returns `true` if the deque is empty, or `false` otherwise.
17+
* `boolean isFull()` Returns `true` if the deque is full, or `false` otherwise.
18+
19+
**Example 1:**
20+
21+
**Input**
22+
23+
["MyCircularDeque", "insertLast", "insertLast", "insertFront", "insertFront", "getRear", "isFull", "deleteLast", "insertFront", "getFront"]
24+
25+
[[3], [1], [2], [3], [4], [], [], [], [4], []]
26+
27+
**Output:** [null, true, true, true, false, 2, true, true, true, 4]
28+
29+
**Explanation:**
30+
31+
MyCircularDeque myCircularDeque = new MyCircularDeque(3);
32+
33+
myCircularDeque.insertLast(1); // return True
34+
35+
myCircularDeque.insertLast(2); // return True
36+
37+
myCircularDeque.insertFront(3); // return True
38+
39+
myCircularDeque.insertFront(4); // return False, the queue is full.
40+
41+
myCircularDeque.getRear(); // return 2
42+
43+
myCircularDeque.isFull(); // return True
44+
45+
myCircularDeque.deleteLast(); // return True
46+
47+
myCircularDeque.insertFront(4); // return True
48+
49+
myCircularDeque.getFront(); // return 4
50+
51+
**Constraints:**
52+
53+
* `1 <= k <= 1000`
54+
* `0 <= value <= 1000`
55+
* At most `2000` calls will be made to `insertFront`, `insertLast`, `deleteFront`, `deleteLast`, `getFront`, `getRear`, `isEmpty`, `isFull`.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g0601_0700.s0643_maximum_average_subarray_i;
2+
3+
// #Easy #Array #Sliding_Window
4+
5+
public class Solution {
6+
public double findMaxAverage(int[] nums, int k) {
7+
double windowSum = 0;
8+
int windowStart = 0;
9+
double max = Integer.MIN_VALUE;
10+
for (int windowEnd = 0; windowEnd < nums.length; ++windowEnd) {
11+
windowSum += nums[windowEnd];
12+
if (windowEnd >= k - 1) {
13+
double candidate = windowSum / k;
14+
max = Math.max(candidate, max);
15+
windowSum -= nums[windowStart];
16+
windowStart++;
17+
}
18+
}
19+
return max;
20+
}
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
643\. Maximum Average Subarray I
2+
3+
Easy
4+
5+
You are given an integer array `nums` consisting of `n` elements, and an integer `k`.
6+
7+
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.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,12,-5,-6,50,3], k = 4
12+
13+
**Output:** 12.75000
14+
15+
**Explanation:** Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [5], k = 1
20+
21+
**Output:** 5.00000
22+
23+
**Constraints:**
24+
25+
* `n == nums.length`
26+
* <code>1 <= k <= n <= 10<sup>5</sup></code>
27+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>

0 commit comments

Comments
(0)

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