-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
517 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_813.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.hit.basmath.learn.others; | ||
|
||
/** | ||
* 813. Largest Sum of Averages | ||
* <p> | ||
* We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve? | ||
* <p> | ||
* Note that our partition must use every number in A, and that scores are not necessarily integers. | ||
* <p> | ||
* Example: | ||
* <p> | ||
* Input: | ||
* A = [9,1,2,3,9] | ||
* K = 3 | ||
* Output: 20 | ||
* Explanation: | ||
* The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20. | ||
* We could have also partitioned A into [9, 1], [2], [3, 9], for example. | ||
* That partition would lead to a score of 5 + 2 + 6 = 13, which is worse. | ||
* <p> | ||
* Note: | ||
* <p> | ||
* 1 <= A.length <= 100. | ||
* 1 <= A[i] <= 10000. | ||
* 1 <= K <= A.length. | ||
* Answers within 10^-6 of the correct answer will be accepted as correct. | ||
*/ | ||
public class _813 { | ||
public double largestSumOfAverages(int[] A, int K) { | ||
if (K == 0 || A.length == 0) { | ||
return 0; | ||
} | ||
int l = A.length; | ||
double[][] f = new double[l][K + 1]; | ||
double[] s = new double[l + 1]; | ||
for (int i = 1; i <= l; i++) { | ||
s[i] = s[i - 1] + A[i - 1]; | ||
f[i - 1][1] = s[i] / i; | ||
} | ||
for (int j = 2; j <= K; j++) { | ||
for (int i = 0; i < l; i++) { | ||
double max = Double.MIN_VALUE; | ||
for (int p = 0; p < i; p++) { | ||
double sum = f[p][j - 1] + (s[i + 1] - s[p + 1]) / (i - p); | ||
max = Double.max(sum, max); | ||
} | ||
f[i][j] = max; | ||
} | ||
} | ||
return f[l - 1][K]; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_814.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.hit.basmath.learn.others; | ||
|
||
import com.hit.common.TreeNode; | ||
|
||
/** | ||
* 814. Binary Tree Pruning | ||
* <p> | ||
* We are given the head node root of a binary tree, where additionally every node's value is either a 0 or a 1. | ||
* <p> | ||
* Return the same tree where every subtree (of the given tree) not containing a 1 has been removed. | ||
* <p> | ||
* (Recall that the subtree of a node X is X, plus every node that is a descendant of X.) | ||
* <p> | ||
* Example 1: | ||
* Input: [1,null,0,0,1] | ||
* Output: [1,null,0,null,1] | ||
* Explanation: | ||
* Only the red nodes satisfy the property "every subtree not containing a 1". | ||
* The diagram on the right represents the answer. | ||
* <p> | ||
* Example 2: | ||
* Input: [1,0,1,0,0,0,1] | ||
* Output: [1,null,1,null,1] | ||
* <p> | ||
* Example 3: | ||
* Input: [1,1,0,1,1,0,1,0] | ||
* Output: [1,1,0,1,1,null,1] | ||
* <p> | ||
* Note: | ||
* <p> | ||
* The binary tree will have at most 100 nodes. | ||
* The value of each node will only be 0 or 1. | ||
*/ | ||
public class _814 { | ||
public TreeNode pruneTree(TreeNode root) { | ||
if (root == null) return null; | ||
if (pruneTree(root.left) == null) root.left = null; | ||
if (pruneTree(root.right) == null) root.right = null; | ||
return (root.right == null && root.left == null && root.val == 0) ? null : root; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_815.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.hit.basmath.learn.others; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* 815. Bus Routes | ||
* <p> | ||
* We have a list of bus routes. Each routes[i] is a bus route that the i-th bus repeats forever. For example if routes[0] = [1, 5, 7], this means that the first bus (0-th indexed) travels in the sequence 1->5->7->1->5->7->1->... forever. | ||
* <p> | ||
* We start at bus stop S (initially not on a bus), and we want to go to bus stop T. Travelling by buses only, what is the least number of buses we must take to reach our destination? Return -1 if it is not possible. | ||
* <p> | ||
* Example: | ||
* <p> | ||
* Input: | ||
* routes = [[1, 2, 7], [3, 6, 7]] | ||
* S = 1 | ||
* T = 6 | ||
* Output: 2 | ||
* Explanation: | ||
* The best strategy is take the first bus to the bus stop 7, then take the second bus to the bus stop 6. | ||
* <p> | ||
* Note: | ||
* <p> | ||
* 1 <= routes.length <= 500. | ||
* 1 <= routes[i].length <= 500. | ||
* 0 <= routes[i][j] < 10 ^ 6. | ||
*/ | ||
public class _815 { | ||
public int numBusesToDestination(int[][] routes, int S, int T) { | ||
HashSet<Integer> visited = new HashSet<>(); | ||
Queue<Integer> q = new LinkedList<>(); | ||
HashMap<Integer, ArrayList<Integer>> map = new HashMap<>(); | ||
int ret = 0; | ||
|
||
if (S == T) return 0; | ||
|
||
for (int i = 0; i < routes.length; i++) { | ||
for (int j = 0; j < routes[i].length; j++) { | ||
ArrayList<Integer> buses = map.getOrDefault(routes[i][j], new ArrayList<>()); | ||
buses.add(i); | ||
map.put(routes[i][j], buses); | ||
} | ||
} | ||
|
||
q.offer(S); | ||
while (!q.isEmpty()) { | ||
int len = q.size(); | ||
ret++; | ||
for (int i = 0; i < len; i++) { | ||
int cur = q.poll(); | ||
ArrayList<Integer> buses = map.get(cur); | ||
for (int bus : buses) { | ||
if (visited.contains(bus)) continue; | ||
visited.add(bus); | ||
for (int j = 0; j < routes[bus].length; j++) { | ||
if (routes[bus][j] == T) return ret; | ||
q.offer(routes[bus][j]); | ||
} | ||
} | ||
} | ||
} | ||
return -1; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_816.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.hit.basmath.learn.others; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* 816. Ambiguous Coordinates | ||
* <p> | ||
* We had some 2-dimensional coordinates, like "(1, 3)" or "(2, 0.5)". Then, we removed all commas, decimal points, and spaces, and ended up with the string S. Return a list of strings representing all possibilities for what our original coordinates could have been. | ||
* <p> | ||
* Our original representation never had extraneous zeroes, so we never started with numbers like "00", "0.0", "0.00", "1.0", "001", "00.01", or any other number that can be represented with less digits. Also, a decimal point within a number never occurs without at least one digit occuring before it, so we never started with numbers like ".1". | ||
* <p> | ||
* The final answer list can be returned in any order. Also note that all coordinates in the final answer have exactly one space between them (occurring after the comma.) | ||
* <p> | ||
* Example 1: | ||
* <p> | ||
* Input: "(123)" | ||
* Output: ["(1, 23)", "(12, 3)", "(1.2, 3)", "(1, 2.3)"] | ||
* <p> | ||
* Example 2: | ||
* <p> | ||
* Input: "(00011)" | ||
* Output: ["(0.001, 1)", "(0, 0.011)"] | ||
* Explanation: | ||
* 0.0, 00, 0001 or 00.01 are not allowed. | ||
* <p> | ||
* Example 3: | ||
* <p> | ||
* Input: "(0123)" | ||
* Output: ["(0, 123)", "(0, 12.3)", "(0, 1.23)", "(0.1, 23)", "(0.1, 2.3)", "(0.12, 3)"] | ||
* <p> | ||
* Example 4: | ||
* <p> | ||
* Input: "(100)" | ||
* Output: [(10, 0)] | ||
* Explanation: | ||
* 1.0 is not allowed. | ||
* <p> | ||
* Note: | ||
* <p> | ||
* 4 <= S.length <= 12. | ||
* S[0] = "(", S[S.length - 1] = ")", and the other elements in S are digits. | ||
*/ | ||
public class _816 { | ||
public List<String> ambiguousCoordinates(String S) { | ||
int n = S.length(); | ||
List<String> res = new ArrayList<>(); | ||
for (int i = 1; i < n - 2; ++i) { | ||
List<String> A = f(S.substring(1, i + 1)), B = f(S.substring(i + 1, n - 1)); | ||
for (String a : A) for (String b : B) res.add("(" + a + ", " + b + ")"); | ||
} | ||
return res; | ||
} | ||
|
||
private List<String> f(String S) { | ||
int n = S.length(); | ||
List<String> res = new ArrayList<>(); | ||
if (n == 0 || (n > 1 && S.charAt(0) == '0' && S.charAt(n - 1) == '0')) return res; | ||
if (n > 1 && S.charAt(0) == '0') { | ||
res.add("0." + S.substring(1)); | ||
return res; | ||
} | ||
res.add(S); | ||
if (n == 1 || S.charAt(n - 1) == '0') return res; | ||
for (int i = 1; i < n; ++i) res.add(S.substring(0, i) + '.' + S.substring(i)); | ||
return res; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_817.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.hit.basmath.learn.others; | ||
|
||
import com.hit.common.ListNode; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* 817. Linked List Components | ||
* <p> | ||
* We are given head, the head node of a linked list containing unique integer values. | ||
* <p> | ||
* We are also given the list G, a subset of the values in the linked list. | ||
* <p> | ||
* Return the number of connected components in G, where two values are connected if they appear consecutively in the linked list. | ||
* <p> | ||
* Example 1: | ||
* <p> | ||
* Input: | ||
* <p> | ||
* head: 0->1->2->3 | ||
* G = [0, 1, 3] | ||
* Output: 2 | ||
* Explanation: | ||
* 0 and 1 are connected, so [0, 1] and [3] are the two connected components. | ||
* <p> | ||
* Example 2: | ||
* <p> | ||
* Input: | ||
* head: 0->1->2->3->4 | ||
* G = [0, 3, 1, 4] | ||
* Output: 2 | ||
* Explanation: | ||
* 0 and 1 are connected, 3 and 4 are connected, so [0, 1] and [3, 4] are the two connected components. | ||
* <p> | ||
* Note: | ||
* <p> | ||
* If N is the length of the linked list given by head, 1 <= N <= 10000. | ||
* The value of each node in the linked list will be in the range [0, N - 1]. | ||
* 1 <= G.length <= 10000. | ||
* G is a subset of all values in the linked list. | ||
*/ | ||
public class _817 { | ||
public int numComponents(ListNode head, int[] G) { | ||
Set<Integer> setG = new HashSet<>(); | ||
for (int i : G) setG.add(i); | ||
int res = 0; | ||
while (head != null) { | ||
if (setG.contains(head.val) && (head.next == null || !setG.contains(head.next.val))) res++; | ||
head = head.next; | ||
} | ||
return res; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_818.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.hit.basmath.learn.others; | ||
|
||
/** | ||
* 818. Race Car | ||
* <p> | ||
* Your car starts at position 0 and speed +1 on an infinite number line. (Your car can go into negative positions.) | ||
* <p> | ||
* Your car drives automatically according to a sequence of instructions A (accelerate) and R (reverse). | ||
* <p> | ||
* When you get an instruction "A", your car does the following: position += speed, speed *= 2. | ||
* <p> | ||
* When you get an instruction "R", your car does the following: if your speed is positive then speed = -1 , otherwise speed = 1. (Your position stays the same.) | ||
* <p> | ||
* For example, after commands "AAR", your car goes to positions 0->1->3->3, and your speed goes to 1->2->4->-1. | ||
* <p> | ||
* Now for some target position, say the length of the shortest sequence of instructions to get there. | ||
* <p> | ||
* Example 1: | ||
* <p> | ||
* Input: | ||
* target = 3 | ||
* Output: 2 | ||
* Explanation: | ||
* The shortest instruction sequence is "AA". | ||
* Your position goes from 0->1->3. | ||
* <p> | ||
* Example 2: | ||
* <p> | ||
* Input: | ||
* target = 6 | ||
* Output: 5 | ||
* Explanation: | ||
* The shortest instruction sequence is "AAARA". | ||
* Your position goes from 0->1->3->7->7->6. | ||
* <p> | ||
* Note: | ||
* <p> | ||
* 1 <= target <= 10000. | ||
*/ | ||
public class _818 { | ||
private int[] dp = new int[10001]; | ||
|
||
public int racecar(int t) { | ||
if (dp[t] > 0) return dp[t]; | ||
int n = (int) (Math.log(t) / Math.log(2)) + 1; | ||
if (1 << n == t + 1) dp[t] = n; | ||
else { | ||
dp[t] = racecar((1 << n) - 1 - t) + n + 1; | ||
for (int m = 0; m < n - 1; ++m) | ||
dp[t] = Math.min(dp[t], racecar(t - (1 << (n - 1)) + (1 << m)) + n + m + 1); | ||
} | ||
return dp[t]; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
codes/java/leetcodes/src/main/java/com/hit/basmath/learn/others/_819.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.hit.basmath.learn.others; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* 819. Most Common Word | ||
* <p> | ||
* Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words. It is guaranteed there is at least one word that isn't banned, and that the answer is unique. | ||
* <p> | ||
* Words in the list of banned words are given in lowercase, and free of punctuation. Words in the paragraph are not case sensitive. The answer is in lowercase. | ||
* <p> | ||
* Example: | ||
* <p> | ||
* Input: | ||
* paragraph = "Bob hit a ball, the hit BALL flew far after it was hit." | ||
* banned = ["hit"] | ||
* Output: "ball" | ||
* Explanation: | ||
* "hit" occurs 3 times, but it is a banned word. | ||
* "ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph. | ||
* Note that words in the paragraph are not case sensitive, | ||
* that punctuation is ignored (even if adjacent to words, such as "ball,"), | ||
* and that "hit" isn't the answer even though it occurs more because it is banned. | ||
* <p> | ||
* Note: | ||
* <p> | ||
* 1 <= paragraph.length <= 1000. | ||
* 0 <= banned.length <= 100. | ||
* 1 <= banned[i].length <= 10. | ||
* The answer is unique, and written in lowercase (even if its occurrences in paragraph may have uppercase symbols, and even if it is a proper noun.) | ||
* paragraph only consists of letters, spaces, or the punctuation symbols !?',;. | ||
* There are no hyphens or hyphenated words. | ||
* Words only consist of letters, never apostrophes or other punctuation symbols. | ||
*/ | ||
public class _819 { | ||
public String mostCommonWord(String p, String[] banned) { | ||
HashSet<String> ban = new HashSet<>(Arrays.asList(banned)); | ||
Map<String, Integer> count = new HashMap<>(); | ||
String[] words = p.replaceAll("\\W+", " ").toLowerCase().split("\\s+"); | ||
for (String w : words) if (!ban.contains(w)) count.put(w, count.getOrDefault(w, 0) + 1); | ||
return Collections.max(count.entrySet(), Map.Entry.comparingByValue()).getKey(); | ||
} | ||
} |
Oops, something went wrong.