-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq10p9.java
More file actions
44 lines (41 loc) · 1.33 KB
/
Copy pathq10p9.java
File metadata and controls
44 lines (41 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public class q10p9{
public static void main(String[] args){
int[][] matrix = {{15,20,40,85},
{20,35,80,95},
{30,55,95,105},
{40,80,100,120}};
boolean[][] mem = new boolean[matrix.length][matrix[0].length];
System.out.println(search(matrix, 80, 0, 0, mem));
System.out.println(search2(matrix, 80));
}
//suck..
public static boolean search(int[][] matrix, int target, int pX, int pY, boolean[][] mem){
if(pX >= matrix.length || pY >= matrix[0].length)
return false;
if(mem[pX][pY] == true)
return false;
else
mem[pX][pY] = true;
if(matrix[pX][pY] == target)
return true;
boolean tempA = search(matrix, target, pX+1, pY, mem);
boolean tempB = search(matrix, target, pX, pY+1, mem);
return tempA || tempB;
}
// this way is better
// T: O(row+col)
public static boolean search2(int[][] matrix, int target){
int row = 0;
int col = matrix[0].length-1;
while(row < matrix.length && col >=0){
if(matrix[row][col]==target)
return true;
else if(matrix[row][col] > target)
col--;
else
row++;
}
return false;
}
// BST can be use in way 3.
}