Skip to content

Commit 099259e

Browse files
committed
binary search
1 parent cede7b1 commit 099259e

File tree

7 files changed

+129
-37
lines changed

7 files changed

+129
-37
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.leetcode.binarysearch;
2+
3+
public class BinarySearch {
4+
5+
6+
public int searchInsert(int[] arr , int target){
7+
int left = 0 , right = arr.length -1 ;
8+
while(left <= right){
9+
10+
int mid = ( left + right ) / 2;
11+
if(arr[mid] == target){
12+
return mid;
13+
}else if(target < arr[mid]){
14+
right = mid - 1;
15+
}else{
16+
left = mid + 1;
17+
}
18+
}
19+
return left;
20+
}
21+
22+
public static void main(String[] args) {
23+
24+
int[] nums = {1,3,5,7 ,9};
25+
int target = 6;
26+
27+
int index = new BinarySearch().searchInsert(nums , target);
28+
System.out.println(index);
29+
30+
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.leetcode.binarysearch;
2+
3+
4+
/**
5+
* Search a 2D Matrix
6+
*/
7+
public class SearchMatrix {
8+
9+
public boolean searchMatrix(int[][] matrix, int target) {
10+
int rows = matrix.length;
11+
int columns = matrix[0].length;
12+
int targetRow = searchRow(matrix, target, rows, columns);
13+
if(targetRow == -1 ){
14+
return true;
15+
}else if(targetRow >= rows){
16+
return false;
17+
} else{
18+
return searchTarget(matrix[targetRow] , target);
19+
}
20+
}
21+
22+
private boolean searchTarget(int[] arr, int target) {
23+
int left = 0 , right = arr.length -1 ;
24+
while(left <= right){
25+
26+
int mid = ( left + right ) / 2;
27+
if(arr[mid] == target){
28+
return true;
29+
}else if(target < arr[mid]){
30+
right = mid - 1;
31+
}else{
32+
left = mid + 1;
33+
}
34+
}
35+
return false;
36+
}
37+
38+
private int searchRow(int[][] matrix, int target, int rows, int columns) {
39+
int left = 0 , right = rows -1;
40+
while(left <= right){
41+
int mid = (left + right) / 2;
42+
// if can search directly in the row end number, pass -1 to identify it can be searched.
43+
if(target == matrix[mid][columns - 1] ){
44+
return -1;
45+
}
46+
else if(target > matrix[mid][columns - 1] ){
47+
left = mid + 1;
48+
}else{
49+
right = mid -1;
50+
}
51+
}
52+
return left;
53+
}
54+
55+
public static void main(String[] args) {
56+
int[][] matrix = {{1,3,5,7},{10,11,16,20},{23,30,34,60}};
57+
int target = 2;
58+
boolean b = new SearchMatrix().searchMatrix(matrix, target);
59+
System.out.println(b);
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.leetcode.dynamicplan;
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.Comparator;
6+
import java.util.Optional;
7+
8+
/**
9+
*
10+
* Best Time to Buy and Sell Stock
11+
*/
12+
13+
public class BestTIme {
14+
15+
private int minPrice = Integer.MAX_VALUE;
16+
private int maxprofit = Integer.MIN_VALUE;
17+
18+
public int maxProfit(int[] prices) {
19+
for(int price : prices){
20+
minPrice = Math.min(minPrice , price);
21+
maxprofit = Math.max(maxprofit , price - minPrice);
22+
}
23+
return maxprofit;
24+
}
25+
26+
public static void main(String[] args) {
27+
BestTIme bestTIme = new BestTIme();
28+
int[] prices = {7,1,5,3,6,4};
29+
int maxProfit = bestTIme.maxProfit(prices);
30+
System.out.println(maxProfit);
31+
}
32+
}

src/main/java/com/leetcode/recursion/RecursionOne.java renamed to src/main/java/com/leetcode/recursion/Permutate.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* permutation or arrangement problem: [1,2,3] => [1,2,3] [1,3,2] ,[2,1,3] ,[2,3,1],[3,1,2],[3,2,1]
99
*/
10-
public class RecursionOne {
10+
public class Permutate {
1111

1212
private int count;
1313
private ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
@@ -57,7 +57,7 @@ public int[] restOfArr(int ele, int[] arr) {
5757

5858
public static void main(String[] args) {
5959
int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
60-
RecursionOne arrangement = new RecursionOne();
60+
Permutate arrangement = new Permutate();
6161
arrangement.arrange(arr);
6262
arrangement.result.stream().forEach(new Consumer<ArrayList<Integer>>() {
6363
@Override

src/main/java/com/leetcode/recursion/RecursionOneS.java renamed to src/main/java/com/leetcode/recursion/PermutateS.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import java.util.ArrayList;
44
import java.util.List;
55

6-
public class RecursionOneS {
6+
public class PermutateS {
77

88

99
public static void main(String[] args) {
1010
String s = "abc";
11-
RecursionOneS recursionOneS = new RecursionOneS();
11+
PermutateS recursionOneS = new PermutateS();
1212
List<String> result = new ArrayList<>();
1313
recursionOneS.permutate(s, "", result);
1414
for (String ele : result) {

src/test/java/com/leetcode/recursion/RecursionOneTest.java

-17
This file was deleted.

src/test/java/com/leetcode/stack/HeapOneTest.java

-16
This file was deleted.

0 commit comments

Comments
 (0)