Skip to content

Commit 1e31412

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents 2c52c3c + af5ce7d commit 1e31412

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

problems/1049.最后一块石头的重量II.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ class Solution:
313313

314314
```
315315
### Go:
316+
317+
一维dp
316318
```go
317319
func lastStoneWeightII(stones []int) int {
318320
// 15001 = 30 * 1000 /2 +1
@@ -341,6 +343,43 @@ func max(a, b int) int {
341343
}
342344
```
343345

346+
二维dp
347+
```go
348+
func lastStoneWeightII(stones []int) int {
349+
sum := 0
350+
for _, val := range stones {
351+
sum += val
352+
}
353+
target := sum / 2
354+
355+
dp := make([][]int, len(stones))
356+
for i := range dp {
357+
dp[i] = make([]int, target + 1)
358+
}
359+
for j := stones[0]; j <= target; j++ {
360+
dp[0][j] = stones[0]
361+
}
362+
363+
for i := 1; i < len(stones); i++ {
364+
for j := 0; j <= target; j++ {
365+
if stones[i] > j {
366+
dp[i][j] = dp[i-1][j]
367+
} else {
368+
dp[i][j] = max(dp[i-1][j], dp[i-1][j-stones[i]] + stones[i])
369+
}
370+
}
371+
}
372+
return (sum - dp[len(stones)-1][target]) - dp[len(stones)-1][target]
373+
}
374+
375+
func max(x, y int) int {
376+
if x > y {
377+
return x
378+
}
379+
return y
380+
}
381+
```
382+
344383
### JavaScript:
345384

346385
```javascript

problems/kamacoder/0105.有向图的完全可达性.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,55 @@ int main() {
289289

290290
### Java
291291

292+
```java
293+
294+
import java.util.*;
295+
296+
public class Main {
297+
298+
public static void dfs(List<List<Integer>> graph, int key, boolean[] visited) {
299+
for (int neighbor : graph.get(key)) {
300+
if (!visited[neighbor]) { // Check if the next node is not visited
301+
visited[neighbor] = true;
302+
dfs(graph, neighbor, visited);
303+
}
304+
}
305+
}
306+
307+
public static void main(String[] args) {
308+
Scanner scanner = new Scanner(System.in);
309+
int n = scanner.nextInt();
310+
int m = scanner.nextInt();
311+
312+
List<List<Integer>> graph = new ArrayList<>();
313+
for (int i = 0; i <= n; i++) {
314+
graph.add(new ArrayList<>());
315+
}
316+
317+
for (int i = 0; i < m; i++) {
318+
int s = scanner.nextInt();
319+
int t = scanner.nextInt();
320+
graph.get(s).add(t);
321+
}
322+
323+
boolean[] visited = new boolean[n + 1];
324+
visited[1] = true; // Process node 1 beforehand
325+
dfs(graph, 1, visited);
326+
327+
for (int i = 1; i <= n; i++) {
328+
if (!visited[i]) {
329+
System.out.println(-1);
330+
return;
331+
}
332+
}
333+
System.out.println(1);
334+
}
335+
}
336+
337+
338+
```
339+
340+
292341
### Python
293342
BFS算法
294343
```Python
@@ -327,8 +376,103 @@ if __name__ == "__main__":
327376

328377
```
329378

379+
``` python
380+
381+
def dfs(graph, key, visited):
382+
for neighbor in graph[key]:
383+
if not visited[neighbor]: # Check if the next node is not visited
384+
visited[neighbor] = True
385+
dfs(graph, neighbor, visited)
386+
387+
def main():
388+
import sys
389+
input = sys.stdin.read
390+
data = input().split()
391+
392+
n = int(data[0])
393+
m = int(data[1])
394+
395+
graph = [[] for _ in range(n + 1)]
396+
index = 2
397+
for _ in range(m):
398+
s = int(data[index])
399+
t = int(data[index + 1])
400+
graph[s].append(t)
401+
index += 2
402+
403+
visited = [False] * (n + 1)
404+
visited[1] = True # Process node 1 beforehand
405+
dfs(graph, 1, visited)
406+
407+
for i in range(1, n + 1):
408+
if not visited[i]:
409+
print(-1)
410+
return
411+
412+
print(1)
413+
414+
if __name__ == "__main__":
415+
main()
416+
417+
418+
```
419+
330420
### Go
331421

422+
```go
423+
424+
package main
425+
426+
import (
427+
"bufio"
428+
"fmt"
429+
"os"
430+
)
431+
432+
func dfs(graph [][]int, key int, visited []bool) {
433+
visited[key] = true
434+
for _, neighbor := range graph[key] {
435+
if !visited[neighbor] {
436+
dfs(graph, neighbor, visited)
437+
}
438+
}
439+
}
440+
441+
func main() {
442+
scanner := bufio.NewScanner(os.Stdin)
443+
scanner.Scan()
444+
var n, m int
445+
fmt.Sscanf(scanner.Text(), "%d %d", &n, &m)
446+
447+
graph := make([][]int, n+1)
448+
for i := 0; i <= n; i++ {
449+
graph[i] = make([]int, 0)
450+
}
451+
452+
for i := 0; i < m; i++ {
453+
scanner.Scan()
454+
var s, t int
455+
fmt.Sscanf(scanner.Text(), "%d %d", &s, &t)
456+
graph[s] = append(graph[s], t)
457+
}
458+
459+
visited := make([]bool, n+1)
460+
461+
dfs(graph, 1, visited)
462+
463+
for i := 1; i <= n; i++ {
464+
if !visited[i] {
465+
fmt.Println(-1)
466+
return
467+
}
468+
}
469+
fmt.Println(1)
470+
}
471+
472+
473+
```
474+
475+
332476
### Rust
333477

334478
### Javascript

0 commit comments

Comments
 (0)