diff --git a/problems/1049.最后一块石头的重量II.md b/problems/1049.最后一块石头的重量II.md index 4f2cc9e3..b40ed114 100644 --- a/problems/1049.最后一块石头的重量II.md +++ b/problems/1049.最后一块石头的重量II.md @@ -313,6 +313,8 @@ class Solution: ``` ### Go: + +一维dp ```go func lastStoneWeightII(stones []int) int { // 15001 = 30 * 1000 /2 +1 @@ -341,6 +343,43 @@ func max(a, b int) int { } ``` +二维dp +```go +func lastStoneWeightII(stones []int) int { + sum := 0 + for _, val := range stones { + sum += val + } + target := sum / 2 + + dp := make([][]int, len(stones)) + for i := range dp { + dp[i] = make([]int, target + 1) + } + for j := stones[0]; j <= target; j++ { + dp[0][j] = stones[0] + } + + for i := 1; i < len(stones); i++ { + for j := 0; j <= target; j++ { + if stones[i] > j { + dp[i][j] = dp[i-1][j] + } else { + dp[i][j] = max(dp[i-1][j], dp[i-1][j-stones[i]] + stones[i]) + } + } + } + return (sum - dp[len(stones)-1][target]) - dp[len(stones)-1][target] +} + +func max(x, y int) int { + if x > y { + return x + } + return y +} +``` + ### JavaScript: ```javascript diff --git a/problems/kamacoder/0105.有向图的完全可达性.md b/problems/kamacoder/0105.有向图的完全可达性.md index cd880a83..765f9a27 100644 --- a/problems/kamacoder/0105.有向图的完全可达性.md +++ b/problems/kamacoder/0105.有向图的完全可达性.md @@ -289,6 +289,55 @@ int main() { ### Java +```java + +import java.util.*; + +public class Main { + + public static void dfs(List> graph, int key, boolean[] visited) { + for (int neighbor : graph.get(key)) { + if (!visited[neighbor]) { // Check if the next node is not visited + visited[neighbor] = true; + dfs(graph, neighbor, visited); + } + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + + List> graph = new ArrayList<>(); + for (int i = 0; i <= n; i++) { + graph.add(new ArrayList<>()); + } + + for (int i = 0; i < m; i++) { + int s = scanner.nextInt(); + int t = scanner.nextInt(); + graph.get(s).add(t); + } + + boolean[] visited = new boolean[n + 1]; + visited[1] = true; // Process node 1 beforehand + dfs(graph, 1, visited); + + for (int i = 1; i <= n; i++) { + if (!visited[i]) { + System.out.println(-1); + return; + } + } + System.out.println(1); + } +} + + +``` + + ### Python BFS算法 ```Python @@ -327,8 +376,103 @@ if __name__ == "__main__": ``` +``` python + +def dfs(graph, key, visited): + for neighbor in graph[key]: + if not visited[neighbor]: # Check if the next node is not visited + visited[neighbor] = True + dfs(graph, neighbor, visited) + +def main(): + import sys + input = sys.stdin.read + data = input().split() + + n = int(data[0]) + m = int(data[1]) + + graph = [[] for _ in range(n + 1)] + index = 2 + for _ in range(m): + s = int(data[index]) + t = int(data[index + 1]) + graph[s].append(t) + index += 2 + + visited = [False] * (n + 1) + visited[1] = True # Process node 1 beforehand + dfs(graph, 1, visited) + + for i in range(1, n + 1): + if not visited[i]: + print(-1) + return + + print(1) + +if __name__ == "__main__": + main() + + +``` + ### Go +```go + +package main + +import ( + "bufio" + "fmt" + "os" +) + +func dfs(graph [][]int, key int, visited []bool) { + visited[key] = true + for _, neighbor := range graph[key] { + if !visited[neighbor] { + dfs(graph, neighbor, visited) + } + } +} + +func main() { + scanner := bufio.NewScanner(os.Stdin) + scanner.Scan() + var n, m int + fmt.Sscanf(scanner.Text(), "%d %d", &n, &m) + + graph := make([][]int, n+1) + for i := 0; i <= n; i++ { + graph[i] = make([]int, 0) + } + + for i := 0; i < m; i++ { + scanner.Scan() + var s, t int + fmt.Sscanf(scanner.Text(), "%d %d", &s, &t) + graph[s] = append(graph[s], t) + } + + visited := make([]bool, n+1) + + dfs(graph, 1, visited) + + for i := 1; i <= n; i++ { + if !visited[i] { + fmt.Println(-1) + return + } + } + fmt.Println(1) +} + + +``` + + ### Rust ### Javascript