Skip to content

Commit 2f1de3e

Browse files
topic wise folders
1 parent b2c35f1 commit 2f1de3e

File tree

264 files changed

+818
-805
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

264 files changed

+818
-805
lines changed
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
For those who don�t like regular images, ASCII Maps Inc. has created maps that are fully printable ASCII characters. Each map is a rectangular grid of lowercase English letters, where each letter stands for various locations. In particular, �w� stands for water and the other 25 letters represent various different land locations. For this problem, we are interested in counting the number of bodies of water on a given ASCII map. A body of water is a maximal set of contiguous grid squares on the ASCII map where each square in the body of water shares a boundary with at least one other square in the body of water. Thus, for two grid squares to be part of the same body of water, one must be above, below, to the left, or to the right of the other grid square.
2-
Input
3-
The first line of input consists of two space separated integers, r (1=r=50) and c (1=c=50), the number of rows and columns, respectively for the input map. The next r lines will each contain c lowercase English letters, representing the corresponding row of the input map.
4-
5-
Output
6-
On a line by itself, output the number of bodies of water in the input map.
7-
8-
Sample test case 1:
9-
10-
Input:
11-
5 6
12-
waaaww
13-
wawawc
14-
bbbbwc
15-
wwwwww
16-
dddddd
17-
18-
Output:
19-
3
20-
21-
Sample test case 2:
22-
Input:
23-
2 8
24-
wxwxwxwx
25-
xwxwxwxw
26-
27-
Output:
28-
8
1+
For those who don�t like regular images, ASCII Maps Inc. has created maps that are fully printable ASCII characters. Each map is a rectangular grid of lowercase English letters, where each letter stands for various locations. In particular, �w� stands for water and the other 25 letters represent various different land locations. For this problem, we are interested in counting the number of bodies of water on a given ASCII map. A body of water is a maximal set of contiguous grid squares on the ASCII map where each square in the body of water shares a boundary with at least one other square in the body of water. Thus, for two grid squares to be part of the same body of water, one must be above, below, to the left, or to the right of the other grid square.
2+
Input
3+
The first line of input consists of two space separated integers, r (1=r=50) and c (1=c=50), the number of rows and columns, respectively for the input map. The next r lines will each contain c lowercase English letters, representing the corresponding row of the input map.
4+
5+
Output
6+
On a line by itself, output the number of bodies of water in the input map.
7+
8+
Sample test case 1:
9+
10+
Input:
11+
5 6
12+
waaaww
13+
wawawc
14+
bbbbwc
15+
wwwwww
16+
dddddd
17+
18+
Output:
19+
3
20+
21+
Sample test case 2:
22+
Input:
23+
2 8
24+
wxwxwxwx
25+
xwxwxwxw
26+
27+
Output:
28+
8
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
1-
import java.util.Arrays;
2-
import java.util.Scanner;
3-
4-
public class water {
5-
public static void main(String[] args){
6-
Scanner kb=new Scanner(System.in);
7-
System.out.println("Enter the dimensions of the map:");
8-
int n=kb.nextInt();
9-
int m=kb.nextInt();
10-
System.out.println("Enter the map:");
11-
char[][] x=new char[n][m];
12-
for(int i=0;i<n;i++)
13-
x[i]=kb.next().toCharArray();
14-
String str="";
15-
int count=0;
16-
for(int i=0;i<n;i++){
17-
for(int k=0;k<m;k++){
18-
if(x[i][k]!='#'&&x[i][k]=='w'){
19-
fillGrid(x, i, k);
20-
display(x);
21-
count++;
22-
System.out.println(count);
23-
}
24-
}
25-
}
26-
if(count==1)
27-
System.out.println("\nThere is "+count+" body of water.");
28-
else
29-
System.out.println("\nThere are "+count+" bodies of water.");
30-
}
31-
public static void fillGrid(char[][] arr, int r, int c) {
32-
if (inBounds(arr,r,c)&&arr[r][c] == 'w'){
33-
arr[r][c] = '#';
34-
fillGrid(arr,r + 1, c);
35-
fillGrid(arr,r - 1, c);
36-
fillGrid(arr,r, c + 1);
37-
fillGrid(arr,r, c - 1);
38-
}
39-
}
40-
41-
public static boolean inBounds(char[][] arr, int r, int c) {
42-
return r>=0&&r<arr.length&&c>=0&&c<arr[0].length;
43-
}
44-
45-
public static void display(char[][] arr){
46-
System.out.println("\nGrid: ");
47-
for (int i = 0; i < arr.length; i++){
48-
for (int j = 0; j < arr[i].length; j++)
49-
System.out.print(arr[i][j]);
50-
System.out.println();
51-
}
52-
}
53-
}
1+
import java.util.Arrays;
2+
import java.util.Scanner;
3+
4+
public class water {
5+
public static void main(String[] args){
6+
Scanner kb=new Scanner(System.in);
7+
System.out.println("Enter the dimensions of the map:");
8+
int n=kb.nextInt();
9+
int m=kb.nextInt();
10+
System.out.println("Enter the map:");
11+
char[][] x=new char[n][m];
12+
for(int i=0;i<n;i++)
13+
x[i]=kb.next().toCharArray();
14+
String str="";
15+
int count=0;
16+
for(int i=0;i<n;i++){
17+
for(int k=0;k<m;k++){
18+
if(x[i][k]!='#'&&x[i][k]=='w'){
19+
fillGrid(x, i, k);
20+
display(x);
21+
count++;
22+
System.out.println(count);
23+
}
24+
}
25+
}
26+
if(count==1)
27+
System.out.println("\nThere is "+count+" body of water.");
28+
else
29+
System.out.println("\nThere are "+count+" bodies of water.");
30+
}
31+
public static void fillGrid(char[][] arr, int r, int c) {
32+
if (inBounds(arr,r,c)&&arr[r][c] == 'w'){
33+
arr[r][c] = '#';
34+
fillGrid(arr,r + 1, c);
35+
fillGrid(arr,r - 1, c);
36+
fillGrid(arr,r, c + 1);
37+
fillGrid(arr,r, c - 1);
38+
}
39+
}
40+
41+
public static boolean inBounds(char[][] arr, int r, int c) {
42+
return r>=0&&r<arr.length&&c>=0&&c<arr[0].length;
43+
}
44+
45+
public static void display(char[][] arr){
46+
System.out.println("\nGrid: ");
47+
for (int i = 0; i < arr.length; i++){
48+
for (int j = 0; j < arr[i].length; j++)
49+
System.out.print(arr[i][j]);
50+
System.out.println();
51+
}
52+
}
53+
}
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1-
#include <stdio.h>
2-
#include <limits.h>
3-
4-
int maxSubarraySum(int a[], int l) {
5-
int max = INT_MIN, max_here = 0;
6-
7-
for (int i = 0; i < l; i++) {
8-
max_here = max_here + a[i];
9-
if (max < max_here) max = max_here;
10-
if (max_here < 0) max_here = 0;
11-
}
12-
13-
return max;
14-
}
15-
16-
// int maxSubarraySum(int a[], int l) {
17-
// int max = a[0], max_here = a[0];
18-
19-
// for (int i = 0; i < l; i++) {
20-
// if (max_here > 0) max_here += a[i];
21-
// else max_here = a[i];
22-
// if (max_here > max) max = max_here;
23-
// }
24-
25-
// return max;
26-
// }
27-
28-
int main(int argc, char *argv[]) {
29-
int arr[] = {-2, -3, 4, -1, -2, 1, 5, -3};
30-
int l = sizeof(arr) / sizeof(int);
31-
32-
int m = maxSubarraySum(arr, l);
33-
34-
printf("%d is the max subarray sum\n", m);
35-
36-
return 0;
1+
#include <stdio.h>
2+
#include <limits.h>
3+
4+
int maxSubarraySum(int a[], int l) {
5+
int max = INT_MIN, max_here = 0;
6+
7+
for (int i = 0; i < l; i++) {
8+
max_here = max_here + a[i];
9+
if (max < max_here) max = max_here;
10+
if (max_here < 0) max_here = 0;
11+
}
12+
13+
return max;
14+
}
15+
16+
// int maxSubarraySum(int a[], int l) {
17+
// int max = a[0], max_here = a[0];
18+
19+
// for (int i = 0; i < l; i++) {
20+
// if (max_here > 0) max_here += a[i];
21+
// else max_here = a[i];
22+
// if (max_here > max) max = max_here;
23+
// }
24+
25+
// return max;
26+
// }
27+
28+
int main(int argc, char *argv[]) {
29+
int arr[] = {-2, -3, 4, -1, -2, 1, 5, -3};
30+
int l = sizeof(arr) / sizeof(int);
31+
32+
int m = maxSubarraySum(arr, l);
33+
34+
printf("%d is the max subarray sum\n", m);
35+
36+
return 0;
3737
}
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Problem:
2-
Find out the maximum continuous subarray sum in linear time
3-
4-
Solution:
5-
Kadane's algorithm can calculate the maximum subarray sum in linear time
1+
Problem:
2+
Find out the maximum continuous subarray sum in linear time
3+
4+
Solution:
5+
Kadane's algorithm can calculate the maximum subarray sum in linear time
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)