Skip to content
24 changes: 15 additions & 9 deletions problems/0452.用最少数量的箭引爆气球.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public:
```

* 时间复杂度:O(nlog n),因为有一个快排
* 空间复杂度:O(1),有一个快排,最差情况(倒序)时,需要n次递归调用。因此确实需要O(n)的栈空间
* 空间复杂度:O(n),有一个快排,最差情况(倒序)时,需要n次递归调用。因此确实需要O(n)的栈空间

可以看出代码并不复杂。

Expand Down Expand Up @@ -180,19 +180,25 @@ class Solution:
```python
class Solution: # 不改变原数组
def findMinArrowShots(self, points: List[List[int]]) -> int:
if len(points) == 0:
return 0

points.sort(key = lambda x: x[0])
sl,sr = points[0][0],points[0][1]

# points已经按照第一个坐标正序排列,因此只需要设置一个变量,记录右侧坐标(阈值)
# 考虑一个气球范围包含两个不相交气球的情况:气球1: [1, 10], 气球2: [2, 5], 气球3: [6, 10]
curr_min_right = points[0][1]
count = 1

for i in points:
if i[0]>sr:
count+=1
sl,sr = i[0],i[1]
if i[0] > curr_min_right:
# 当气球左侧大于这个阈值,那么一定就需要在发射一只箭,并且将阈值更新为当前气球的右侧
count += 1
curr_min_right = i[1]
else:
sl = max(sl,i[0])
sr = min(sr,i[1])
# 否则的话,我们只需要求阈值和当前气球的右侧的较小值来更新阈值
curr_min_right = min(curr_min_right, i[1])
return count


```
### Go
```go
Expand Down
2 changes: 1 addition & 1 deletion problems/kamacoder/0098.所有可达路径.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ ACM格式大家在输出结果的时候,要关注看看格式问题,特别

有录友可能会想,ACM格式就是麻烦,有空格没有空格有什么影响,结果对了不就行了?

ACM模式相对于核心代码模式(力扣) 更考验大家对代码的掌控能力。 例如工程代码里,输出输出都是要自己控制的。这也是为什么大公司笔试,都是ACM模式。
ACM模式相对于核心代码模式(力扣) 更考验大家对代码的掌控能力。 例如工程代码里,输入输出都是要自己控制的。这也是为什么大公司笔试,都是ACM模式。

以上代码中,结果都存在了 result数组里(二维数组,每一行是一个结果),最后将其打印出来。(重点看注释)

Expand Down
174 changes: 90 additions & 84 deletions problems/kamacoder/0100.岛屿的最大面积.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,121 +222,127 @@ public:

## 其他语言版本

### Java
DFS
### Java

```java
//这里的实现为主函数处理每个岛屿的第一块陆地 方式
//所以是主函数直接置count为1,剩余的交给dfs来做。
import java.util.*;
import java.math.*;

/**
* DFS版
*/
public class Main{
static int[][] dir = {{0,-1}, {1,0}, {0,1}, {-1, 0}};//四个方向
static int count = 0;
public static void dfs(boolean[][] visited, int x, int y, int[][] grid){
for(int i = 0; i < 4; i++){
int nextX = x + dir[i][0];
int nextY = y + dir[i][1];
if(nextX < 0 || nextY < 0 || nextY >= grid[0].length || nextX >= grid.length){
continue;
}
if(!visited[nextX][nextY] && grid[nextX][nextY] == 1){
count++;
visited[nextX][nextY] = true;
dfs(visited, nextX, nextY, grid);
}
}
}

static final int[][] dir={{0,1},{1,0},{0,-1},{-1,0}};
static int result=0;
static int count=0;

public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int[][] grid = new int[n][m];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
grid[i][j] = in.nextInt();
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int[][] map = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
map[i][j]=scanner.nextInt();
}
}

int result = 0;
boolean[][] visited = new boolean[n][m];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(!visited[i][j] && grid[i][j] == 1){
visited[i][j] = true;
count = 1;
dfs(visited, i, j, grid);
//dfs遍历完了一座岛屿,就比较count和result,保留最大的
result = Math.max(result, count);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if(!visited[i][j]&&map[i][j]==1){
count=0;
dfs(map,visited,i,j);
result= Math.max(count, result);
}
}
}
System.out.println(result);
}

static void dfs(int[][] map,boolean[][] visited,int x,int y){
count++;
visited[x][y]=true;
for (int i = 0; i < 4; i++) {
int nextX=x+dir[i][0];
int nextY=y+dir[i][1];
//水或者已经访问过的跳过
if(nextX<0||nextY<0
||nextX>=map.length||nextY>=map[0].length
||visited[nextX][nextY]||map[nextX][nextY]==0)continue;

dfs(map,visited,nextX,nextY);
}
}
}
```
BFS

```java
import java.util.*;
public class Main{
static int[][] dir = {{0,-1}, {1,0}, {0,1}, {-1, 0}};//下右上左的顺序
static int count = 0;
public static void bfs(boolean[][] visited, int x, int y, int[][] grid){
Queue<pair> queue = new LinkedList<pair>();
queue.add(new pair(x,y));
count = 1; //该岛屿的第一块陆地被visit了

//对这个岛屿的所有都入队,除非上下左右都没有未访问的陆地
while(!queue.isEmpty()){
int curX = queue.peek().x;
int curY = queue.poll().y;
//对每块陆地都进行上下左右的入队和计算(遍历),自然就是按广度优先了
for(int i = 0; i < 4; i++){
int nextX = curX + dir[i][0];
int nextY = curY + dir[i][1];
if(nextX < 0 || nextY < 0 || nextX >= grid.length || nextY >= grid[0].length){
continue;
}
if(!visited[nextX][nextY] && grid[nextX][nextY] == 1){
count++;
queue.add(new pair(nextX, nextY));
visited[nextX][nextY] = true;
}
}
}
}

static class pair{
import java.math.*;

/**
* BFS版
*/
public class Main {
static class Node {
int x;
int y;
pair(int x, int y){

public Node(int x, int y) {
this.x = x;
this.y = y;
}
}

public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int[][] grid = new int[n][m];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
grid[i][j] = in.nextInt();

static final int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
static int result = 0;
static int count = 0;

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int[][] map = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
map[i][j] = scanner.nextInt();
}
}
int result = 0;
boolean[][] visited = new boolean[n][m];
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(!visited[i][j] && grid[i][j] == 1){
visited[i][j] = true;
bfs(visited, i, j, grid);
result = Math.max(result, count);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!visited[i][j] && map[i][j] == 1) {
count = 0;
bfs(map, visited, i, j);
result = Math.max(count, result);

}
}
}
System.out.println(result);
}

static void bfs(int[][] map, boolean[][] visited, int x, int y) {
Queue<Node> q = new LinkedList<>();
q.add(new Node(x, y));
visited[x][y] = true;
count++;
while (!q.isEmpty()) {
Node node = q.remove();
for (int i = 0; i < 4; i++) {
int nextX = node.x + dir[i][0];
int nextY = node.y + dir[i][1];
if (nextX < 0 || nextY < 0 || nextX >= map.length || nextY >= map[0].length || visited[nextX][nextY] || map[nextX][nextY] == 0)
continue;
q.add(new Node(nextX, nextY));
visited[nextX][nextY] = true;
count++;
}
}
}
}

```
### Python

Expand Down
108 changes: 56 additions & 52 deletions problems/kamacoder/0110.字符串接龙.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,66 +152,70 @@ int main() {

## 其他语言版本

### Java
### Java

```Java
import java.util.*;

public class Main {
// BFS方法
public static int ladderLength(String beginWord, String endWord, List<String> wordList) {
// 使用set作为查询容器,效率更高
HashSet<String> set = new HashSet<>(wordList);

// 声明一个queue存储每次变更一个字符得到的且存在于容器中的新字符串
Queue<String> queue = new LinkedList<>();

// 声明一个hashMap存储遍历到的字符串以及所走过的路径path
HashMap<String, Integer> visitMap = new HashMap<>();
queue.offer(beginWord);
visitMap.put(beginWord, 1);

while (!queue.isEmpty()) {
String curWord = queue.poll();
int path = visitMap.get(curWord);

for (int i = 0; i < curWord.length(); i++) {
char[] ch = curWord.toCharArray();
// 每个位置尝试26个字母
for (char k = 'a'; k <= 'z'; k++) {
ch[i] = k;

String newWord = new String(ch);
if (newWord.equals(endWord)) return path + 1;

// 如果这个新字符串存在于容器且之前未被访问到
if (set.contains(newWord) && !visitMap.containsKey(newWord)) {
visitMap.put(newWord, path + 1);
queue.offer(newWord);
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine();
String beginStr = scanner.next();
String endStr = scanner.next();
scanner.nextLine();
List<String> wordList = new ArrayList<>();
wordList.add(beginStr);
wordList.add(endStr);
for (int i = 0; i < n; i++) {
wordList.add(scanner.nextLine());
}
int count = bfs(beginStr, endStr, wordList);
System.out.println(count);
}

/**
* 广度优先搜索-寻找最短路径
*/
public static int bfs(String beginStr, String endStr, List<String> wordList) {
int len = 1;
Set<String> set = new HashSet<>(wordList);
Set<String> visited = new HashSet<>();
Queue<String> q = new LinkedList<>();
visited.add(beginStr);
q.add(beginStr);
q.add(null);
while (!q.isEmpty()) {
String node = q.remove();
//上一层结束,若下一层还有节点进入下一层
if (node == null) {
if (!q.isEmpty()) {
len++;
q.add(null);
}
continue;
}
char[] charArray = node.toCharArray();
//寻找邻接节点
for (int i = 0; i < charArray.length; i++) {
//记录旧值,用于回滚修改
char old = charArray[i];
for (char j = 'a'; j <= 'z'; j++) {
charArray[i] = j;
String newWord = new String(charArray);
if (set.contains(newWord) && !visited.contains(newWord)) {
q.add(newWord);
visited.add(newWord);
//找到结尾
if (newWord.equals(endStr)) return len + 1;
}
}
charArray[i] = old;
}
}

return 0;
}

public static void main (String[] args) {
/* code */
// 接收输入
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
sc.nextLine();
String[] strs = sc.nextLine().split(" ");

List<String> wordList = new ArrayList<>();
for (int i = 0; i < N; i++) {
wordList.add(sc.nextLine());
}

// wordList.add(strs[1]);

// 打印结果
int result = ladderLength(strs[0], strs[1], wordList);
System.out.println(result);
}
}

```
Expand Down
Loading