Skip to content

Commit f8bf19a

Browse files
committed
update
1 parent 5539d92 commit f8bf19a

6 files changed

+127
-0
lines changed

leetcode_solved/[editing]leetcode_0076_Minimum_Window_Substring.cpp

Whitespace-only changes.

leetcode_solved/[editing]leetcode_0143_Reorder_List.cpp

Whitespace-only changes.

leetcode_solved/[editing]leetcode_0146_LRU_Cache.cpp

Whitespace-only changes.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// AC: Runtime: 122 ms, faster than 5.02% of Java online submissions for Minimum Window Substring.
2+
// Memory Usage: 39.9 MB, less than 32.04% of Java online submissions for Minimum Window Substring.
3+
// sliding window
4+
// T:O(26 * n + m) ~ O(m + n), S:O(26 * 2) ~ O(1)
5+
//
6+
class Solution {
7+
public String minWindow(String s, String t) {
8+
int sizeS = s.length(), sizeT = t.length(), minSize = Integer.MAX_VALUE, left = -1, right = -1;
9+
if (sizeS < sizeT) {
10+
return "";
11+
}
12+
13+
HashMap<Character, Integer> recordS = new HashMap<>(), recordT = new HashMap<>();
14+
for (char c : t.toCharArray()) {
15+
recordT.merge(c, 1, Integer::sum);
16+
}
17+
for (char c : s.toCharArray()) {
18+
recordS.merge(c, 1, Integer::sum);
19+
}
20+
21+
for (char c : recordT.keySet()) {
22+
if (!recordS.containsKey(c) || recordS.get(c) < recordT.get(c)) {
23+
return "";
24+
}
25+
}
26+
27+
int leftPos = 0, rightPos = 0;
28+
HashMap<Character, Integer> tempRecord = new HashMap<>();
29+
tempRecord.merge(s.charAt(rightPos), 1, Integer::sum);
30+
31+
while (leftPos <= sizeS - 1) {
32+
// right pointer forwarding
33+
while (rightPos < sizeS - 1 && !check(tempRecord, recordT)) {
34+
rightPos++;
35+
tempRecord.merge(s.charAt(rightPos), 1, Integer::sum);
36+
}
37+
if (check(tempRecord, recordT)) {
38+
if (rightPos - leftPos + 1 < minSize) {
39+
minSize = rightPos - leftPos + 1;
40+
left = leftPos;
41+
right = rightPos;
42+
}
43+
} else {
44+
break;
45+
}
46+
47+
// left pointer forwarding
48+
while (leftPos <= sizeS - 1 && check(tempRecord, recordT)) {
49+
if (rightPos - leftPos + 1 < minSize) {
50+
minSize = rightPos - leftPos + 1;
51+
left = leftPos;
52+
right = rightPos;
53+
}
54+
tempRecord.merge(s.charAt(leftPos), -1, Integer::sum);
55+
leftPos++;
56+
}
57+
}
58+
59+
return left == -1 ? "" :s.substring(left, right + 1);
60+
}
61+
62+
private boolean check(HashMap<Character, Integer> h1, HashMap<Character, Integer> h2) {
63+
for (char c : h2.keySet()) {
64+
if (!h1.containsKey(c) || h2.get(c) > h1.get(c)) {
65+
return false;
66+
}
67+
}
68+
69+
return true;
70+
}
71+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Solution: space O(n) method
2+
// AC: Runtime: 2 ms, faster than 38.26% of Java online submissions for Reorder List.
3+
// Memory Usage: 41.5 MB, less than 81.90% of Java online submissions for Reorder List.
4+
// .
5+
// T:O(n), S:O(n)
6+
//
7+
class Solution {
8+
public void reorderList(ListNode head) {
9+
List<ListNode> record = new ArrayList<>();
10+
while (head != null) {
11+
record.add(head);
12+
head = head.next;
13+
}
14+
int size = record.size(), left = 1, right = size - 1;
15+
ListNode temp = record.get(0);
16+
ListNode ret = temp;
17+
while (left <= right) {
18+
temp.next = record.get(right);
19+
temp = temp.next;
20+
right--;
21+
if (left < right) {
22+
temp.next = record.get(left);
23+
temp = temp.next;
24+
left++;
25+
}
26+
}
27+
temp.next = null;
28+
29+
head = ret;
30+
}
31+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// AC: Runtime: 46 ms, faster than 89.97% of Java online submissions for LRU Cache.
2+
// Memory Usage: 109.2 MB, less than 87.04% of Java online submissions for LRU Cache.
3+
// implement with java's native LinkedHashMap container.
4+
// T:O(1), S:O(n)
5+
//
6+
class LRUCache {
7+
private HashMap<Integer, Integer> map;
8+
9+
public LRUCache(int capacity) {
10+
map = new LinkedHashMap<Integer, Integer>(capacity, 0.75f, true) {
11+
@Override
12+
protected boolean removeEldestEntry(Map.Entry eldest) {
13+
return size() > capacity;
14+
}
15+
};
16+
}
17+
18+
public int get(int key) {
19+
return map.getOrDefault(key, -1);
20+
}
21+
22+
public void put(int key, int value) {
23+
map.put(key, value);
24+
}
25+
}

0 commit comments

Comments
 (0)