diff --git "a/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/README.md" "b/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/README.md" index 098fe5fe8a34f..0400bc1804a0c 100644 --- "a/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/README.md" +++ "b/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/README.md" @@ -47,6 +47,8 @@ +**方法一:动态规划** + ### **Python3** @@ -54,7 +56,14 @@ ```python - +class Solution: + def numWays(self, n: int, relation: List[List[int]], k: int) -> int: + dp = [[0] * n for _ in range(k + 1)] + dp[0][0] = 1 + for i in range(1, k + 1): + for a, b in relation: + dp[i][b] += dp[i - 1][a] + return dp[-1][-1] ``` ### **Java** @@ -62,7 +71,18 @@ ```java - +class Solution { + public int numWays(int n, int[][] relation, int k) { + int[][] dp = new int[k + 1][n]; + dp[0][0] = 1; + for (int i = 1; i <= k; i++) { + for (int[] r : relation) { + dp[i][r[1]] += dp[i - 1][r[0]]; + } + } + return dp[k][n - 1]; + } +} ``` ### **...** diff --git "a/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/Solution.java" "b/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/Solution.java" new file mode 100644 index 0000000000000..bb4d430e55cd4 --- /dev/null +++ "b/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/Solution.java" @@ -0,0 +1,12 @@ +class Solution { + public int numWays(int n, int[][] relation, int k) { + int[][] dp = new int[k + 1][n]; + dp[0][0] = 1; + for (int i = 1; i <= k; i++) { + for (int[] r : relation) { + dp[i][r[1]] += dp[i - 1][r[0]]; + } + } + return dp[k][n - 1]; + } +} diff --git "a/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/Solution.py" "b/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/Solution.py" new file mode 100644 index 0000000000000..b6f7f1f054a3b --- /dev/null +++ "b/lcp/LCP 07. \344\274\240\351\200\222\344\277\241\346\201\257/Solution.py" @@ -0,0 +1,9 @@ +class Solution: + def numWays(self, n: int, relation: List[List[int]], k: int) -> int: + dp = [[0] * n for _ in range(k + 1)] + dp[0][0] = 1 + for i in range(1, k + 1): + for a, b in relation: + dp[i][b] += dp[i - 1][a] + return dp[-1][-1] + diff --git "a/lcp/LCP 08. \345\211\247\346\203\205\350\247\246\345\217\221\346\227\266\351\227\264/README.md" "b/lcp/LCP 08. \345\211\247\346\203\205\350\247\246\345\217\221\346\227\266\351\227\264/README.md" index faee7d08b57c5..c60bb332e50e6 100644 --- "a/lcp/LCP 08. \345\211\247\346\203\205\350\247\246\345\217\221\346\227\266\351\227\264/README.md" +++ "b/lcp/LCP 08. \345\211\247\346\203\205\350\247\246\345\217\221\346\227\266\351\227\264/README.md" @@ -61,6 +61,8 @@ +**方法一:前缀和 + 二分查找** + ### **Python3** @@ -68,7 +70,24 @@ ```python - +class Solution: + def getTriggerTime(self, increase: List[List[int]], requirements: List[List[int]]) -> List[int]: + increase.insert(0, [0, 0, 0]) + m, n = len(increase), len(requirements) + for i in range(1, m): + for j in range(3): + increase[i][j] += increase[i - 1][j] + ans = [-1] * n + for i, req in enumerate(requirements): + left, right = 0, m + while left < right: + mid = (left + right) >> 1 + if all(a >= b for a, b in zip(increase[mid], req)): + ans[i] = mid + right = mid + else: + left = mid + 1 + return ans ``` ### **Java** @@ -76,7 +95,42 @@ ```java - +class Solution { + public int[] getTriggerTime(int[][] increase, int[][] requirements) { + int m = increase.length, n = requirements.length; + int[][] s = new int[m + 1][3]; + for (int j = 0; j < 3; ++j) { + for (int i = 0; i < m; ++i) { + s[i + 1][j] = s[i][j] + increase[i][j]; + } + } + + int[] ans = new int[n]; + Arrays.fill(ans, -1); + for (int i = 0; i < n; ++i) { + int left = 0, right = m + 1; + while (left < right) { + int mid = (left + right) >> 1; + if (check(s[mid], requirements[i])) { + ans[i] = mid; + right = mid; + } else { + left = mid + 1; + } + } + } + return ans; + } + + private boolean check(int[] a, int[] b) { + for (int i = 0; i < 3; ++i) { + if (a[i] < b[i]) { + return false; + } + } + return true; + } +} ``` ### **...** diff --git "a/lcp/LCP 08. \345\211\247\346\203\205\350\247\246\345\217\221\346\227\266\351\227\264/Solution.java" "b/lcp/LCP 08. \345\211\247\346\203\205\350\247\246\345\217\221\346\227\266\351\227\264/Solution.java" new file mode 100644 index 0000000000000..912d07ba327e6 --- /dev/null +++ "b/lcp/LCP 08. \345\211\247\346\203\205\350\247\246\345\217\221\346\227\266\351\227\264/Solution.java" @@ -0,0 +1,36 @@ +class Solution { + public int[] getTriggerTime(int[][] increase, int[][] requirements) { + int m = increase.length, n = requirements.length; + int[][] s = new int[m + 1][3]; + for (int j = 0; j < 3; ++j) { + for (int i = 0; i < m; ++i) { + s[i + 1][j] = s[i][j] + increase[i][j]; + } + } + + int[] ans = new int[n]; + Arrays.fill(ans, -1); + for (int i = 0; i < n; ++i) { + int left = 0, right = m + 1; + while (left < right) { + int mid = (left + right) >> 1; + if (check(s[mid], requirements[i])) { + ans[i] = mid; + right = mid; + } else { + left = mid + 1; + } + } + } + return ans; + } + + private boolean check(int[] a, int[] b) { + for (int i = 0; i < 3; ++i) { + if (a[i] < b[i]) { + return false; + } + } + return true; + } +} diff --git "a/lcp/LCP 08. \345\211\247\346\203\205\350\247\246\345\217\221\346\227\266\351\227\264/Solution.py" "b/lcp/LCP 08. \345\211\247\346\203\205\350\247\246\345\217\221\346\227\266\351\227\264/Solution.py" new file mode 100644 index 0000000000000..82e23bc8e2365 --- /dev/null +++ "b/lcp/LCP 08. \345\211\247\346\203\205\350\247\246\345\217\221\346\227\266\351\227\264/Solution.py" @@ -0,0 +1,18 @@ +class Solution: + def getTriggerTime(self, increase: List[List[int]], requirements: List[List[int]]) -> List[int]: + increase.insert(0, [0, 0, 0]) + m, n = len(increase), len(requirements) + for i in range(1, m): + for j in range(3): + increase[i][j] += increase[i - 1][j] + ans = [-1] * n + for i, req in enumerate(requirements): + left, right = 0, m + while left < right: + mid = (left + right) >> 1 + if all(a >= b for a, b in zip(increase[mid], req)): + ans[i] = mid + right = mid + else: + left = mid + 1 + return ans diff --git "a/lcp/LCP 11. \346\234\237\346\234\233\344\270\252\346\225\260\347\273\237\350\256\241/Solution.java" "b/lcp/LCP 11. \346\234\237\346\234\233\344\270\252\346\225\260\347\273\237\350\256\241/Solution.java" new file mode 100644 index 0000000000000..3a0dfbb74eab2 --- /dev/null +++ "b/lcp/LCP 11. \346\234\237\346\234\233\344\270\252\346\225\260\347\273\237\350\256\241/Solution.java" @@ -0,0 +1,9 @@ +class Solution { + public int expectNumber(int[] scores) { + Set s = new HashSet<>(); + for (int v : scores) { + s.add(v); + } + return s.size(); + } +} diff --git "a/lcp/LCP 11. \346\234\237\346\234\233\344\270\252\346\225\260\347\273\237\350\256\241/Solution.py" "b/lcp/LCP 11. \346\234\237\346\234\233\344\270\252\346\225\260\347\273\237\350\256\241/Solution.py" new file mode 100644 index 0000000000000..366def3600dc4 --- /dev/null +++ "b/lcp/LCP 11. \346\234\237\346\234\233\344\270\252\346\225\260\347\273\237\350\256\241/Solution.py" @@ -0,0 +1,3 @@ +class Solution: + def expectNumber(self, scores: List[int]) -> int: + return len(set(scores)) diff --git "a/lcp/LCP 34. \344\272\214\345\217\211\346\240\221\346\237\223\350\211\262/README.md" "b/lcp/LCP 34. \344\272\214\345\217\211\346\240\221\346\237\223\350\211\262/README.md" index d6dd74dd084de..590835e07cef3 100644 --- "a/lcp/LCP 34. \344\272\214\345\217\211\346\240\221\346\237\223\350\211\262/README.md" +++ "b/lcp/LCP 34. \344\272\214\345\217\211\346\240\221\346\237\223\350\211\262/README.md" @@ -40,7 +40,29 @@ ```python - +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def maxValue(self, root: TreeNode, k: int) -> int: + def dfs(root): + ans = [0] * (k + 1) + if root is None: + return ans + l, r = dfs(root.left), dfs(root.right) + for i in range(k): + for j in range(k - i): + ans[i + j + 1] = max(ans[i + j + 1], l[i] + r[j] + root.val) + for i in range(k + 1): + for j in range(k + 1): + ans[0] = max(ans[0], l[i] + r[j]) + return ans + + return max(dfs(root)) ``` ### **Java** @@ -48,7 +70,45 @@ ```java - +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +class Solution { + public int maxValue(TreeNode root, int k) { + int[] t = dfs(root, k); + int ans = 0; + for (int v : t) { + ans = Math.max(ans, v); + } + return ans; + } + + private int[] dfs(TreeNode root, int k) { + int[] ans = new int[k + 1]; + if (root == null) { + return ans; + } + int[] l = dfs(root.left, k); + int[] r = dfs(root.right, k); + for (int i = 0; i < k; ++i) { + for (int j = 0; j < k - i; ++j) { + ans[i + j + 1] = Math.max(ans[i + j + 1], l[i] + r[j] + root.val); + } + } + for (int i = 0; i <= k; ++i) { + for (int j = 0; j <= k; ++j) { + ans[0] = Math.max(ans[0], l[i] + r[j]); + } + } + return ans; + } +} ``` ### **...** diff --git "a/lcp/LCP 34. \344\272\214\345\217\211\346\240\221\346\237\223\350\211\262/Solution.java" "b/lcp/LCP 34. \344\272\214\345\217\211\346\240\221\346\237\223\350\211\262/Solution.java" new file mode 100644 index 0000000000000..f44ffa0e431cd --- /dev/null +++ "b/lcp/LCP 34. \344\272\214\345\217\211\346\240\221\346\237\223\350\211\262/Solution.java" @@ -0,0 +1,39 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +class Solution { + public int maxValue(TreeNode root, int k) { + int[] t = dfs(root, k); + int ans = 0; + for (int v : t) { + ans = Math.max(ans, v); + } + return ans; + } + + private int[] dfs(TreeNode root, int k) { + int[] ans = new int[k + 1]; + if (root == null) { + return ans; + } + int[] l = dfs(root.left, k); + int[] r = dfs(root.right, k); + for (int i = 0; i < k; ++i) { + for (int j = 0; j < k - i; ++j) { + ans[i + j + 1] = Math.max(ans[i + j + 1], l[i] + r[j] + root.val); + } + } + for (int i = 0; i <= k; ++i) { + for (int j = 0; j <= k; ++j) { + ans[0] = Math.max(ans[0], l[i] + r[j]); + } + } + return ans; + } +} diff --git "a/lcp/LCP 34. \344\272\214\345\217\211\346\240\221\346\237\223\350\211\262/Solution.py" "b/lcp/LCP 34. \344\272\214\345\217\211\346\240\221\346\237\223\350\211\262/Solution.py" new file mode 100644 index 0000000000000..674bd0e39a735 --- /dev/null +++ "b/lcp/LCP 34. \344\272\214\345\217\211\346\240\221\346\237\223\350\211\262/Solution.py" @@ -0,0 +1,23 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def maxValue(self, root: TreeNode, k: int) -> int: + def dfs(root): + ans = [0] * (k + 1) + if root is None: + return ans + l, r = dfs(root.left), dfs(root.right) + for i in range(k): + for j in range(k - i): + ans[i + j + 1] = max(ans[i + j + 1], l[i] + r[j] + root.val) + for i in range(k + 1): + for j in range(k + 1): + ans[0] = max(ans[0], l[i] + r[j]) + return ans + + return max(dfs(root))