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 ee65772

Browse files
Added tasks 213-222.
1 parent d3794c0 commit ee65772

File tree

30 files changed

+820
-0
lines changed

30 files changed

+820
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package g0201_0300.s0213_house_robber_ii;
2+
3+
public class Solution {
4+
public int rob(int[] nums) {
5+
int n = nums.length;
6+
if (n == 0) {
7+
return 0;
8+
}
9+
if (n == 1) {
10+
return nums[0];
11+
}
12+
if (n == 2) {
13+
return Math.max(nums[0], nums[1]);
14+
}
15+
if (n == 3) {
16+
return Math.max(nums[0], Math.max(nums[1], nums[2]));
17+
}
18+
int max = Integer.MIN_VALUE;
19+
int[] inc = new int[n];
20+
int[] exc = new int[n];
21+
inc[0] = nums[0];
22+
exc[0] = 0;
23+
inc[1] = nums[0];
24+
exc[1] = nums[1];
25+
inc[2] = nums[2] + nums[0];
26+
exc[2] = nums[2];
27+
for (int i = 3; i < n - 1; i++) {
28+
inc[i] = Math.max(inc[i - 2], inc[i - 3]) + nums[i];
29+
exc[i] = Math.max(exc[i - 2], exc[i - 3]) + nums[i];
30+
}
31+
inc[n - 1] = inc[n - 2];
32+
exc[n - 1] = Math.max(exc[n - 3], exc[n - 4]) + nums[n - 1];
33+
for (int i = 0; i < n; i++) {
34+
max = Math.max(max, Math.max(inc[i], exc[i]));
35+
}
36+
return max;
37+
}
38+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
213\. House Robber II
2+
3+
Medium
4+
5+
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are **arranged in a circle.** That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and **it will automatically contact the police if two adjacent houses were broken into on the same night**.
6+
7+
Given an integer array `nums` representing the amount of money of each house, return _the maximum amount of money you can rob tonight **without alerting the police**_.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = \[2,3,2\]
12+
13+
**Output:** 3
14+
15+
**Explanation:** You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = \[1,2,3,1\]
20+
21+
**Output:** 4
22+
23+
**Explanation:** Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4.
24+
25+
**Example 3:**
26+
27+
**Input:** nums = \[1,2,3\]
28+
29+
**Output:** 3
30+
31+
**Constraints:**
32+
33+
* `1 <= nums.length <= 100`
34+
* `0 <= nums[i] <= 1000`
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g0201_0300.s0214_shortest_palindrome;
2+
3+
public class Solution {
4+
public String shortestPalindrome(String s) {
5+
int i;
6+
int x;
7+
int diff;
8+
int n = s.length();
9+
int m = (n << 1) + 1;
10+
char[] letters = new char[m];
11+
12+
for (i = 0; i < n; i++) {
13+
letters[i] = letters[m - 1 - i] = s.charAt(i);
14+
}
15+
16+
letters[i] = '#';
17+
18+
int[] lps = new int[m];
19+
lps[0] = 0;
20+
21+
for (i = 1; i < m; i++) {
22+
x = lps[i - 1];
23+
24+
while (letters[i] != letters[x]) {
25+
if (x == 0) {
26+
x = -1;
27+
break;
28+
}
29+
30+
x = lps[x - 1];
31+
}
32+
33+
lps[i] = x + 1;
34+
}
35+
36+
diff = n - lps[m - 1];
37+
if (diff == 0) {
38+
return s;
39+
} else {
40+
StringBuilder builder = new StringBuilder();
41+
for (i = n - 1; i >= n - diff; i--) {
42+
builder.append(s.charAt(i));
43+
}
44+
45+
builder.append(s);
46+
47+
return builder.toString();
48+
}
49+
}
50+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
214\. Shortest Palindrome
2+
3+
Hard
4+
5+
You are given a string `s`. You can convert `s` to a palindrome by adding characters in front of it.
6+
7+
Return _the shortest palindrome you can find by performing this transformation_.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "aacecaaa"
12+
13+
**Output:** "aaacecaaa"
14+
15+
**Example 2:**
16+
17+
**Input:** s = "abcd"
18+
19+
**Output:** "dcbabcd"
20+
21+
**Constraints:**
22+
23+
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code>
24+
* `s` consists of lowercase English letters only.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package g0201_0300.s0215_kth_largest_element_in_an_array;
2+
3+
import java.util.Arrays;
4+
5+
public class Solution {
6+
public int findKthLargest(int[] nums, int k) {
7+
int n = nums.length;
8+
Arrays.sort(nums);
9+
return nums[n - k];
10+
}
11+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
215\. Kth Largest Element in an Array
2+
3+
Medium
4+
5+
Given an integer array `nums` and an integer `k`, return _the_ <code>k<sup>th</sup></code> _largest element in the array_.
6+
7+
Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = \[3,2,1,5,6,4\], k = 2
12+
13+
**Output:** 5
14+
15+
**Example 2:**
16+
17+
**Input:** nums = \[3,2,3,1,2,4,5,5,6\], k = 4
18+
19+
**Output:** 4
20+
21+
**Constraints:**
22+
23+
* <code>1 <= k <= nums.length <= 10<sup>4</sup></code>
24+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package g0201_0300.s0216_combination_sum_iii;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
@SuppressWarnings("java:S5413")
7+
public class Solution {
8+
public List<List<Integer>> combinationSum3(int k, int n) {
9+
List<List<Integer>> res = new ArrayList<>();
10+
solve(k, n, new ArrayList<>(), res, 0, 1);
11+
return res;
12+
}
13+
14+
private void solve(
15+
int k, int target, List<Integer> temp, List<List<Integer>> res, int sum, int start) {
16+
if (sum == target && temp.size() == k) {
17+
res.add(new ArrayList<>(temp));
18+
return;
19+
}
20+
21+
if (temp.size() >= k) {
22+
return;
23+
}
24+
25+
if (sum > target) {
26+
return;
27+
}
28+
29+
for (int i = start; i <= 9; i++) {
30+
temp.add(i);
31+
solve(k, target, temp, res, sum + i, i + 1);
32+
temp.remove(temp.size() - 1);
33+
}
34+
}
35+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
216\. Combination Sum III
2+
3+
Medium
4+
5+
Find all valid combinations of `k` numbers that sum up to `n` such that the following conditions are true:
6+
7+
* Only numbers `1` through `9` are used.
8+
* Each number is used **at most once**.
9+
10+
Return _a list of all possible valid combinations_. The list must not contain the same combination twice, and the combinations may be returned in any order.
11+
12+
**Example 1:**
13+
14+
**Input:** k = 3, n = 7
15+
16+
**Output:** \[\[1,2,4\]\]
17+
18+
**Explanation:** 1 + 2 + 4 = 7 There are no other valid combinations.
19+
20+
**Example 2:**
21+
22+
**Input:** k = 3, n = 9
23+
24+
**Output:** \[\[1,2,6\],\[1,3,5\],\[2,3,4\]\]
25+
26+
**Explanation:** 1 +たす 2 +たす 6 = 9 1 +たす 3 +たす 5 = 9 2 +たす 3 +たす 4 = 9 There are no other valid combinations.
27+
28+
**Example 3:**
29+
30+
**Input:** k = 4, n = 1
31+
32+
**Output:** \[\]
33+
34+
**Explanation:** There are no valid combinations. Using 4 different numbers in the range \[1,9\], the smallest sum we can get is 1+2+3+4 = 10 and since 10 > 1, there are no valid combination.
35+
36+
**Example 4:**
37+
38+
**Input:** k = 3, n = 2
39+
40+
**Output:** \[\]
41+
42+
**Explanation:** There are no valid combinations.
43+
44+
**Example 5:**
45+
46+
**Input:** k = 9, n = 45
47+
48+
**Output:** \[\[1,2,3,4,5,6,7,8,9\]\]
49+
50+
**Explanation:** 1 +たす 2 +たす 3 +たす 4 +たす 5 +たす 6 +たす 7 +たす 8 +たす 9 = 45 There are no other valid combinations.
51+
52+
**Constraints:**
53+
54+
* `2 <= k <= 9`
55+
* `1 <= n <= 60`
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g0201_0300.s0217_contains_duplicate;
2+
3+
import java.util.Arrays;
4+
5+
public class Solution {
6+
public boolean containsDuplicate(int[] nums) {
7+
Arrays.sort(nums);
8+
for (int i = 0; i < nums.length - 1; i++) {
9+
if (nums[i] == nums[i + 1]) {
10+
return true;
11+
}
12+
}
13+
14+
return false;
15+
}
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
217\. Contains Duplicate
2+
3+
Easy
4+
5+
Given an integer array `nums`, return `true` if any value appears **at least twice** in the array, and return `false` if every element is distinct.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = \[1,2,3,1\]
10+
11+
**Output:** true
12+
13+
**Example 2:**
14+
15+
**Input:** nums = \[1,2,3,4\]
16+
17+
**Output:** false
18+
19+
**Example 3:**
20+
21+
**Input:** nums = \[1,1,1,3,3,4,3,2,4,2\]
22+
23+
**Output:** true
24+
25+
**Constraints:**
26+
27+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
28+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>

0 commit comments

Comments
(0)

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