Skip to content

Commit 3a85491

Browse files
committed
update
1 parent 18d6d54 commit 3a85491

27 files changed

+630
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// AC: Runtime: 0 ms, faster than 100.00% of Java online submissions for Find the Middle Index in Array.
2+
// Memory Usage: 38.2 MB, less than 91.66% of Java online submissions for Find the Middle Index in Array.
3+
// .
4+
// T:O(n), S:O(1)
5+
//
6+
class Solution {
7+
public int findMiddleIndex(int[] nums) {
8+
int size = nums.length, sum = 0, tempSum = 0;
9+
for (int i: nums) {
10+
sum += i;
11+
}
12+
for (int i = 0; i < size; i++) {
13+
tempSum += nums[i];
14+
int leftSum = tempSum - nums[i], rightSum = sum - tempSum;
15+
if (leftSum == rightSum) {
16+
return i;
17+
}
18+
}
19+
20+
return -1;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// AC: Runtime: 287 ms, faster than 32.35% of Java online submissions for Operations on Tree.
2+
// Memory Usage: 47.5 MB, less than 68.04% of Java online submissions for Operations on Tree.
3+
// .
4+
// T: lock:O(n), upgrade:O(n^2)
5+
//
6+
class LockingTree {
7+
public HashMap<Integer, Integer> relation;
8+
public HashMap<Integer, List<Integer>> parentToChildRelation;
9+
public HashMap<Integer, Integer> lockUserRecord;
10+
11+
public LockingTree(int[] parent) {
12+
int size = parent.length;
13+
relation = new HashMap<>();
14+
parentToChildRelation = new HashMap<>();
15+
lockUserRecord = new HashMap<>();
16+
for (int i = 0; i < size; i++) {
17+
relation.put(i, parent[i]);
18+
if (parentToChildRelation.containsKey(parent[i])) {
19+
parentToChildRelation.get(parent[i]).add(i);
20+
} else {
21+
List<Integer> tempList = new LinkedList<>();
22+
tempList.add(i);
23+
parentToChildRelation.put(parent[i], tempList);
24+
}
25+
}
26+
}
27+
28+
public boolean lock(int num, int user) {
29+
if (lockUserRecord.containsKey(num)) {
30+
return false;
31+
} else {
32+
lockUserRecord.put(num, user);
33+
return true;
34+
}
35+
}
36+
37+
public boolean unlock(int num, int user) {
38+
if (lockUserRecord.containsKey(num) && lockUserRecord.get(num) == user) {
39+
lockUserRecord.remove(num);
40+
return true;
41+
}
42+
43+
return false;
44+
}
45+
46+
public boolean upgrade(int num, int user) {
47+
if (!lockUserRecord.containsKey(num)) {
48+
// ancester has no lock.
49+
boolean ancestorHasLock = false;
50+
int pos = num;
51+
while (relation.get(pos) != -1) {
52+
int parent = relation.get(pos);
53+
if (lockUserRecord.containsKey(parent)) {
54+
ancestorHasLock = true;
55+
break;
56+
}
57+
pos = parent;
58+
}
59+
if (ancestorHasLock) {
60+
return false;
61+
}
62+
63+
// check child has lock
64+
// leaf node
65+
if (!parentToChildRelation.containsKey(num)) {
66+
return false;
67+
}
68+
boolean childHasLock = false;
69+
List<Integer> searchChild = new LinkedList<>(parentToChildRelation.get(num));
70+
while (!searchChild.isEmpty()) {
71+
List<Integer> newSearchList = new LinkedList<>();
72+
for (int i: searchChild) {
73+
if (lockUserRecord.containsKey(i)) {
74+
childHasLock = true;
75+
lockUserRecord.remove(i);
76+
}
77+
78+
newSearchList.addAll(parentToChildRelation.getOrDefault(i, new LinkedList<>()));
79+
}
80+
searchChild = newSearchList;
81+
}
82+
if (!childHasLock) {
83+
return false;
84+
}
85+
lockUserRecord.put(num, user);
86+
87+
return true;
88+
}
89+
90+
return false;
91+
}
92+
}

leetcode_solved/[editing]leetcode_0092_Reverse_Linked_List_II.cpp

Whitespace-only changes.

leetcode_solved/[editing]leetcode_0096_Unique_Binary_Search_Trees.cpp

Whitespace-only changes.

leetcode_solved/[editing]leetcode_0139_Word_Break.cpp

Whitespace-only changes.

leetcode_solved/[editing]leetcode_0207_Course_Schedule.cpp

Whitespace-only changes.

leetcode_solved/[editing]leetcode_0208_Implement_Trie_(Prefix_Tree).cpp

Whitespace-only changes.

leetcode_solved/[editing]leetcode_0210_Course_Schedule_II.cpp

Whitespace-only changes.

leetcode_solved/[editing]leetcode_0279_Perfect_Squares.cpp

Whitespace-only changes.

leetcode_solved/[editing]leetcode_0300_Longest_Increasing_Subsequence.cpp

-6
This file was deleted.

leetcode_solved/[editing]leetcode_0322_Coin_Change.cpp

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// AC: Runtime: 0 ms, faster than 100.00% of Java online submissions for Reverse Linked List II.
2+
// Memory Usage: 36.5 MB, less than 82.11% of Java online submissions for Reverse Linked List II.
3+
// find the start position and end position of range to be reversed. and record the range values. then assign it to list nodes in reverse order.
4+
// T:O(n), S:O(n)
5+
//
6+
class Solution {
7+
public ListNode reverseBetween(ListNode head, int left, int right) {
8+
if (head == null || left == right) {
9+
return head;
10+
}
11+
12+
ListNode fast = head, slow = head;
13+
int length = right - left;
14+
while (length-- > 0) {
15+
fast = fast.next;
16+
}
17+
18+
// find the start position and end position.
19+
int leftCopy = left - 1;
20+
while (leftCopy-- > 0) {
21+
slow = slow.next;
22+
fast = fast.next;
23+
}
24+
25+
// record the range values.
26+
ListNode temp = slow;
27+
List<Integer> record = new ArrayList<>();
28+
record.add(slow.val);
29+
while (temp != fast) {
30+
temp = temp.next;
31+
record.add(temp.val);
32+
}
33+
34+
// assign values in reverse order.
35+
int pos = record.size() - 1;
36+
slow.val = record.get(pos--);
37+
while (slow != fast) {
38+
slow = slow.next;
39+
slow.val = record.get(pos--);
40+
}
41+
42+
return head;
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// AC: Runtime: 0 ms, faster than 100.00% of Java online submissions for Unique Binary Search Trees.
2+
// Memory Usage: 37.5 MB, less than 18.78% of Java online submissions for Unique Binary Search Trees.
3+
// DP.
4+
// T:O(n ^ 2), S:O(n)
5+
//
6+
class Solution {
7+
public int numTrees(int n) {
8+
int[] dp = new int[n + 1];
9+
dp[0] = 1;
10+
for (int i = 1; i <= n; i++) {
11+
for (int j = 1; j <= i; j++) {
12+
dp[i] += dp[j - 1] * dp[i - j];
13+
}
14+
}
15+
16+
return dp[n];
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// AC: Runtime: 10 ms, faster than 25.12% of Java online submissions for Word Break.
2+
// Memory Usage: 44.1 MB, less than 7.54% of Java online submissions for Word Break.
3+
// DP.
4+
// T:O(n ^ 2), S:O(n)
5+
//
6+
class Solution {
7+
public boolean wordBreak(String s, List<String> wordDict) {
8+
int size = s.length();
9+
boolean[] dp = new boolean[size + 1];
10+
HashSet<String> record = new HashSet<>(wordDict);
11+
dp[0] = true;
12+
for (int i = 0; i < size; i++) {
13+
for (int j = 0; j <= i; j++) {
14+
if (dp[j] && record.contains(s.substring(j, i + 1))) {
15+
dp[i + 1] = true;
16+
break;
17+
}
18+
}
19+
}
20+
21+
return dp[size];
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// dfs + topo sorting
2+
// AC: Runtime: 28 ms, faster than 8.34% of Java online submissions for Course Schedule.
3+
// Memory Usage: 45 MB, less than 16.13% of Java online submissions for Course Schedule.
4+
// 确定有向图中无环即为 true,否则为 false,课程成环相互依赖无法全部修完
5+
// T:O(m + n), O(m + n), m 为点数,n 为边数
6+
//
7+
class Solution {
8+
List<List<Integer>> edges;
9+
int[] visited;
10+
boolean flag = true;
11+
public boolean canFinish(int numCourses, int[][] prerequisites) {
12+
edges = new LinkedList<>();
13+
visited = new int[numCourses];
14+
for (int i = 0; i < numCourses; i++) {
15+
edges.add(new LinkedList<>());
16+
}
17+
for (int[] arr: prerequisites) {
18+
edges.get(arr[1]).add(arr[0]);
19+
}
20+
21+
for (int i = 0; i < numCourses && flag; i++) {
22+
if (visited[i] == 0) {
23+
dfs(i);
24+
if (!flag) {
25+
return flag;
26+
}
27+
}
28+
}
29+
30+
return flag;
31+
}
32+
33+
private void dfs(int i) {
34+
visited[i] = 1;
35+
for (int v: edges.get(i)) {
36+
if (visited[v] == 0) {
37+
dfs(v);
38+
if (!flag) {
39+
return;
40+
}
41+
} else if (visited[v] == 1) { // 形成环
42+
flag = false;
43+
return;
44+
}
45+
}
46+
47+
visited[i] = 2;
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// AC: Runtime: 28 ms, faster than 99.62% of Java online submissions for Implement Trie (Prefix Tree).
2+
// Memory Usage: 48.3 MB, less than 77.69% of Java online submissions for Implement Trie (Prefix Tree).
3+
// understand the definition of trie tree. using a isWord boolean and children[26] to record its existence of a word prefix,
4+
// and to enrich feature, we can add wordCount to record the number words.
5+
// T: insert: O(n), search: O(n), startsWith: O(n), S:O(m * n), n is the length of string, m is the number of strings.
6+
//
7+
class TrieNode {
8+
boolean isWord;
9+
TrieNode[] children;
10+
11+
public TrieNode() {
12+
isWord = false;
13+
children = new TrieNode[26];
14+
}
15+
}
16+
17+
class Trie {
18+
private TrieNode root;
19+
20+
/** Initialize your data structure here. */
21+
public Trie() {
22+
root = new TrieNode();
23+
}
24+
25+
/** Inserts a word into the trie. */
26+
public void insert(String word) {
27+
TrieNode parent = root;
28+
for (int i = 0; i < word.length(); i++) {
29+
int charI = word.charAt(i) - 'a';
30+
if (parent.children[charI] == null) {
31+
parent.children[charI] = new TrieNode();
32+
}
33+
parent = parent.children[charI];
34+
}
35+
parent.isWord = true;
36+
}
37+
38+
/** Returns if the word is in the trie. */
39+
public boolean search(String word) {
40+
TrieNode target = find(word);
41+
return target != null && target.isWord;
42+
}
43+
44+
public TrieNode find(String word) {
45+
TrieNode parent = root;
46+
for (int i = 0; i < word.length(); i++) {
47+
int charI = word.charAt(i) - 'a';
48+
if (parent.children[charI] == null) {
49+
return null;
50+
}
51+
parent = parent.children[charI];
52+
}
53+
54+
return parent;
55+
}
56+
57+
/** Returns if there is any word in the trie that starts with the given prefix. */
58+
public boolean startsWith(String prefix) {
59+
TrieNode parent = root;
60+
for (int i = 0; i < prefix.length(); i++) {
61+
int charI = prefix.charAt(i) - 'a';
62+
if (parent.children[charI] == null) {
63+
return false;
64+
}
65+
parent = parent.children[charI];
66+
}
67+
68+
return true;
69+
}
70+
}

0 commit comments

Comments
 (0)