Skip to content

Commit 68b5db7

Browse files
committed
update
1 parent 6846702 commit 68b5db7

File tree

48 files changed

+1083
-178
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1083
-178
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// AC: Runtime: 2 ms, faster than 100.00% of Java online submissions for Concatenation of Array.
2+
// Memory Usage: 47.7 MB, less than 100.00% of Java online submissions for Concatenation of Array.
3+
// .
4+
// T:O(n), S:O(1)
5+
//
6+
class Solution {
7+
public int[] getConcatenation(int[] nums) {
8+
int size = nums.length;
9+
int[] ret = new int[2 * size];
10+
for (int i = 0; i < 2 * size; i++) {
11+
ret[i] = nums[i % size];
12+
}
13+
14+
return ret;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// AC: Runtime: 74 ms, faster than 100.00% of Java online submissions for Unique Length-3 Palindromic Subsequences.
2+
// Memory Usage: 51.5 MB, less than 50.00% of Java online submissions for Unique Length-3 Palindromic Subsequences.
3+
// thought: record the start-index and end-index of every char, and count the distinct char between (start-index, end-index)
4+
// T:O(n^2), S:O(1)
5+
//
6+
class Solution {
7+
public int countPalindromicSubsequence(String s) {
8+
int size = s.length(), ret = 0;
9+
HashMap<Character, List<Integer>> charIndex = new HashMap<>();
10+
for (int i = 0; i < size; i++) {
11+
char c = s.charAt(i);
12+
if (charIndex.get(c) != null) {
13+
if (charIndex.get(c).size() == 1) {
14+
charIndex.get(c).add(i);
15+
} else {
16+
charIndex.get(c).set(1, i);
17+
}
18+
} else {
19+
List<Integer> tempList = new LinkedList<>();
20+
tempList.add(i);
21+
charIndex.put(c, tempList);
22+
}
23+
}
24+
for (int i = 0; i < 26; i++) {
25+
char c = (char) (i + 'a');
26+
if (charIndex.get(c) != null && charIndex.get(c).size() == 2) {
27+
HashSet<Character> tempRecord = new HashSet<>();
28+
int startIndex = charIndex.get(c).get(0), endIndex = charIndex.get(c).get(1);
29+
if (endIndex - startIndex == 1) {
30+
continue;
31+
}
32+
for (int j = startIndex + 1; j <= endIndex - 1; j++) {
33+
tempRecord.add(s.charAt(j));
34+
// reach max possible value, break
35+
if (tempRecord.size() == 26) {
36+
break;
37+
}
38+
}
39+
ret += tempRecord.size();
40+
}
41+
}
42+
43+
return ret;
44+
}
45+
}

Diff for: leetcode_solved/[editing]leetcode_0042_Trapping_Rain_Water.cpp

-41
This file was deleted.

Diff for: leetcode_solved/[editing]leetcode_0162_Find_Peak_Element.cpp

Whitespace-only changes.

Diff for: leetcode_solved/[editing]leetcode_0413_Arithmetic_Slices.cpp

-6
This file was deleted.

Diff for: leetcode_solved/[editing]leetcode_0535_Encode_and_Decode_TinyURL.cpp

-17
This file was deleted.

Diff for: leetcode_solved/[editing]leetcode_0611_Valid_Triangle_Number.cpp

-6
This file was deleted.

Diff for: leetcode_solved/[editing]leetcode_0735_Asteroid_Collision.cpp

-6
This file was deleted.

Diff for: leetcode_solved/[editing]leetcode_0791_Custom_Sort_String.cpp

-11
This file was deleted.

Diff for: leetcode_solved/[editing]leetcode_0841_Keys_and_Rooms.cpp

-6
This file was deleted.

Diff for: leetcode_solved/[editing]leetcode_0861_Score_After_Flipping_Matrix.cpp

-6
This file was deleted.

Diff for: leetcode_solved/[editing]leetcode_0890_Find_and_Replace_Pattern.cpp

-6
This file was deleted.

Diff for: leetcode_solved/[editing]leetcode_0895_Maximum_Frequency_Stack.cpp

-21
This file was deleted.

Diff for: leetcode_solved/[editing]leetcode_0905_Sort_Array_By_Parity.cpp

-6
This file was deleted.

Diff for: leetcode_solved/[editing]leetcode_0950_Reveal_Cards_In_Increasing_Order.cpp

-6
This file was deleted.

Diff for: leetcode_solved/[editing]leetcode_1261_Find_Elements_in_a_Contaminated_Binary_Tree.cpp

-25
This file was deleted.

Diff for: leetcode_solved/[editing]leetcode_1325_Delete_Leaves_With_a_Given_Value.cpp

-15
This file was deleted.
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Solution1: for every element(except first and last), get the left max and right max,
2+
// and if min(leftMax, rightMax) > cur, then can trap water: min(leftMax, rightMax) - cur
3+
// AC: Runtime: 70 ms, faster than 5.29% of Java online submissions for Trapping Rain Water.
4+
// Memory Usage: 38 MB, less than 99.03% of Java online submissions for Trapping Rain Water.
5+
// .
6+
// T:O(n^2), S:O(1)
7+
//
8+
class Solution {
9+
public int trap(int[] height) {
10+
int size = height.length, ret = 0;
11+
if (size >= 3) {
12+
// the first element and last element cannot trap water.
13+
for (int i = 1; i < size - 1; i++) {
14+
int leftMax = 0, rightMax = 0;
15+
for (int j = 0; j < i; j++) {
16+
leftMax = Math.max(leftMax, height[j]);
17+
}
18+
for (int j = i + 1; j < size; j++) {
19+
rightMax = Math.max(rightMax, height[j]);
20+
}
21+
22+
int temp = Math.min(leftMax, rightMax);
23+
ret += temp > height[i] ? (temp - height[i]) : 0;
24+
}
25+
}
26+
27+
return ret;
28+
}
29+
}
30+
31+
// Solution2: DP, we store every element's left max element, and right max element, then
32+
// the complexity we reduce to O(n)
33+
// AC: Runtime: 1 ms, faster than 85.19% of Java online submissions for Trapping Rain Water.
34+
// Memory Usage: 38.6 MB, less than 47.90% of Java online submissions for Trapping Rain Water.
35+
// dp: to store the left max and right max
36+
// T:O(n), S:O(n)
37+
//
38+
class Solution {
39+
public int trap(int[] height) {
40+
int size = height.length, ret = 0;
41+
if (size < 3) {
42+
return ret;
43+
}
44+
int[] leftMax = new int[size], rightMax = new int[size];
45+
leftMax[0] = 0;
46+
for (int i = 1; i < size; i++) {
47+
leftMax[i] = Math.max(leftMax[i - 1], height[i - 1]);
48+
}
49+
rightMax[size - 1] = 0;
50+
for (int i = size - 2; i >= 0; i--) {
51+
rightMax[i] = Math.max(rightMax[i + 1], height[i + 1]);
52+
}
53+
54+
for (int i = 1; i < size - 1; i++) {
55+
int temp = Math.min(leftMax[i], rightMax[i]);
56+
ret += temp > height[i] ? (temp - height[i]) : 0;
57+
}
58+
59+
return ret;
60+
}
61+
}
62+
63+
64+
// Solution3: todo- 单调栈解法

Diff for: leetcode_solved/leetcode_0162_Find_Peak_Element.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// AC: Runtime: 0 ms, faster than 100.00% of Java online submissions for Find Peak Element.
2+
// Memory Usage: 38.4 MB, less than 77.63% of Java online submissions for Find Peak Element.
3+
// binary search, before next iteration we can judge element's neighbors and may return in advance.
4+
// T:O(logn), S:O(1)
5+
//
6+
class Solution {
7+
public int findPeakElement(int[] nums) {
8+
int start = 0, end = nums.length - 1;
9+
return binarySearch(nums, start, end);
10+
}
11+
12+
private int binarySearch(int[] arr, int start, int end) {
13+
if (start == end) {
14+
return start;
15+
}
16+
int mid = (start + end) / 2, mid2 = mid + 1;
17+
if (arr[mid] > arr[mid2]) {
18+
if (mid - 1 > 0 && arr[mid - 1] < arr[mid]) {
19+
return mid;
20+
}
21+
return binarySearch(arr, start, mid);
22+
} else {
23+
if (mid2 + 1 <= end && arr[mid2 + 1] < arr[mid2]) {
24+
return mid2;
25+
}
26+
return binarySearch(arr, mid2, end);
27+
}
28+
}
29+
}

Diff for: leetcode_solved/leetcode_0413_Arithmetic_Slices.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// AC: Runtime: 0 ms, faster than 100.00% of Java online submissions for Arithmetic Slices.
2+
// Memory Usage: 36.7 MB, less than 80.00% of Java online submissions for Arithmetic Slices.
3+
// thoughts: find the consecutive subarray, then it could contribute (len - 1) * (len - 2) / 2 slices,
4+
// then start from the end of last consecutive +1, and go ahead to find the next consecutive.
5+
// T:O(n), S:O(1)
6+
//
7+
class Solution {
8+
public int numberOfArithmeticSlices(int[] nums) {
9+
int size = nums.length, ret = 0;
10+
if (size >= 3) {
11+
for (int i = 0; i < size - 2;) {
12+
int diff1 = nums[i + 1] - nums[i], diff2 = nums[i + 2] - nums[i + 1];
13+
if (diff1 == diff2) {
14+
int end = i + 2;
15+
while (end < size && nums[end] - nums[end - 1] == diff1) {
16+
end++;
17+
}
18+
// length of the consecutive subarray that has same adjacent diff.
19+
int len = end - i;
20+
// may produce such amount amount of `arithmetic slices`.
21+
ret += (len - 1) * (len - 2) / 2;
22+
23+
// forwarding to the sequence's end + 1
24+
i = end - 2;
25+
} else {
26+
i++;
27+
}
28+
}
29+
}
30+
31+
return ret;
32+
}
33+
}

0 commit comments

Comments
 (0)