Skip to content

Commit 5558791

Browse files
committed
Merge pull request Hearen#29 from zhewen166/master
week two
2 parents 4ff9e3e + c0e099d commit 5558791

File tree

6 files changed

+355
-0
lines changed

6 files changed

+355
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.cz.algorithm.learn.two;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import java.util.Set;
7+
8+
public class FindRepeatDNASequence {
9+
10+
public static void main(String[] args) {
11+
String s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT";
12+
new FindRepeatDNASequence().findRepeatedDnaSequences(s);
13+
}
14+
15+
/**
16+
* during the process to compute the subString, we can use the hash algorithm to compute the hash value to identify weather two string is equal or not
17+
* this is a inspect and method to solve the class question.
18+
* */
19+
public List<String> findRepeatedDnaSequences(String s) {
20+
21+
List<String> result = new ArrayList<String>();
22+
if (s == null || s.length() < 10)
23+
return result;
24+
int len = s.length();
25+
Set<Integer> set = new HashSet<Integer>();
26+
Set<Integer> unique = new HashSet<Integer>();
27+
int hash = 0;
28+
for (int i = 0; i < len; i++) {
29+
if (i < 9) {
30+
hash = (hash << 2) + getVal(s.charAt(i));
31+
} else {
32+
hash = (hash << 2) + getVal(s.charAt(i));
33+
// in the subStirng it has 10 , so the 20bit is enough for the
34+
// subString.
35+
hash &= (1 << 20) - 1;
36+
if (set.contains(hash) && !unique.contains(hash)) {
37+
result.add(s.substring(i - 9, i + 1));
38+
unique.add(hash);
39+
System.out.println(s.substring(i - 9, i + 1));
40+
} else {
41+
set.add(hash);
42+
}
43+
}
44+
}
45+
return result;
46+
}
47+
48+
private int getVal(char ch) {
49+
if (ch == 'A')
50+
return 0;
51+
if (ch == 'C')
52+
return 1;
53+
if (ch == 'G')
54+
return 2;
55+
if (ch == 'T')
56+
return 3;
57+
return -1;
58+
}
59+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.cz.algorithm.learn.two;
2+
3+
import java.util.Stack;
4+
5+
public class LargestRectangleinHistogram {
6+
7+
// public static void main(String[] args) {
8+
// int[] height = new int[] {2,1,5,6,2,3};
9+
// new LargestRectangleinHistogram().largestRectangleArea(height);
10+
// }
11+
public int largestRectanglaArea2(int[] height) {
12+
return 0;
13+
}
14+
15+
/**
16+
* the methodology is to create a stack which store the height of the array,
17+
* if the height[i] is greater or equal to the top of the stack , then push
18+
* height[i] into the stack else compute the max area , on the other hand,
19+
* record the number of which pop from the stack and push itself to the
20+
* stack when the process ends namely reaching the end of the array , pop
21+
* the stack until empty ,computing the area. at last, return the max area.
22+
*
23+
* the time complexity is O(n2); the space complexity is O(n);
24+
*
25+
* */
26+
public int largestRectangleArea(int[] height) {
27+
if (height == null || height.length == 0)
28+
return 0;
29+
int len = height.length;
30+
if (len == 1)
31+
return height[0];
32+
int max = 0;
33+
Stack<Integer> stack = new Stack<Integer>();
34+
int i = 0;
35+
while (i < len) {
36+
if (stack.empty() || height[i] >= stack.peek()) {
37+
stack.push(height[i]);
38+
i++;
39+
} else {
40+
int count = 0;
41+
while (!stack.empty() && stack.peek() > height[i]) {
42+
count++;
43+
int top = stack.peek();
44+
stack.pop();
45+
max = Math.max(max, top * count);
46+
}
47+
for (int j = 0; j < count + 1; j++) {
48+
stack.push(height[i]);
49+
}
50+
i++;
51+
}
52+
}
53+
int count = 0;
54+
while (!stack.empty()) {
55+
count++;
56+
max = Math.max(max, stack.peek() * count);
57+
stack.pop();
58+
}
59+
return max;
60+
}
61+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.cz.algorithm.learn.two;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
7+
public class LongestSubstring {
8+
9+
public static void main(String[] args) {
10+
String s = "asdfgfsds";
11+
int n = findLongestSubString(s);
12+
System.out.println(n);
13+
}
14+
/**
15+
* 暴力破解这个解法,时间复杂度是 O(n2).
16+
* 不是最优解.不可取.
17+
*
18+
* 把字符串转化为字符数组,对字符数组排序,
19+
* 然后一遍扫描, 时间复杂度取决于排序算法 最好的是 O(nLogn)
20+
*
21+
* 应该存在时间复杂度是O(n)的情况
22+
* 使用map数据结构进行解决.
23+
*
24+
* 思路说明:寻找两个相同字母之间的长度,作为嫌疑长度进行考察.
25+
* 并且保存一个中间值进行标记.
26+
* */
27+
public static int findLongestSubString(String s) {
28+
if(s == null || s.length() == 0) return 0;
29+
int max = -1;
30+
int lastIndex = -1;
31+
Map <Character, Integer> letterMap = new HashMap<Character, Integer>();
32+
for(int i = 0 ; i < s.length(); i ++) {
33+
char temp = s.charAt(i);
34+
if(letterMap.containsKey(temp) && lastIndex < letterMap.get(temp)) {
35+
lastIndex = letterMap.get(temp);
36+
}
37+
if( i - lastIndex > max)
38+
max = i - lastIndex;
39+
letterMap.put(temp , i);
40+
}
41+
return max;
42+
}
43+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.cz.algorithm.learn.two;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class MaxPoints {
7+
8+
public int maxPoints(Point[] points) {
9+
if(points == null) return 0;
10+
int len = points.length;
11+
if (len < 3)
12+
return len;
13+
int result = 0;
14+
Map<Double, Integer> slopeMap = new HashMap<Double, Integer>();
15+
for (int i = 0; i < len; i++) {
16+
slopeMap.clear();
17+
int duplicate = 1;
18+
double slope = 0.0;
19+
for (int j = 0; j < len; j++) {
20+
if (i == j) continue;
21+
if (points[i].x == points[j].x && points[i].y == points[j].y) {
22+
duplicate++;
23+
continue;
24+
} else if (points[i].x == points[j].x) {
25+
slope = Integer.MAX_VALUE;
26+
} else {
27+
slope = 1.0 * (points[i].y - points[j].y) / (points[i].x - points[j].x);
28+
}
29+
slopeMap.put(slope, slopeMap.containsKey(slope) ? slopeMap.get(slope) + 1 : 1);
30+
}
31+
if(slopeMap.keySet().size() == 0) {
32+
result = duplicate > result ? duplicate : result;
33+
} else {
34+
for (double key : slopeMap.keySet()) {
35+
result = Math.max((duplicate + slopeMap.get(key)), result);
36+
}
37+
}
38+
}
39+
return result;
40+
}
41+
42+
class Point {
43+
int x;
44+
int y;
45+
46+
public Point() {
47+
x = 0;
48+
y = 0;
49+
}
50+
51+
public Point(int x, int y) {
52+
super();
53+
this.x = x;
54+
this.y = y;
55+
}
56+
}
57+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.cz.algorithm.learn.two;
2+
3+
import java.util.Stack;
4+
5+
public class MaximalRectangle {
6+
7+
public static void main(String[] args) {
8+
char[][] matrix = { { '0', '1', '1', '0', '1' },
9+
{ '1', '1', '0', '1', '0' }, { '0', '1', '1', '1', '0' },
10+
{ '1', '1', '1', '1', '0' }, { '1', '1', '1', '1', '1' },
11+
{ '0', '0', '0', '0', '0' } };
12+
System.out.println(new MaximalRectangle().maximalRectangle(matrix));
13+
}
14+
15+
/**
16+
* here we apply the methodology of the last topic and transform the
17+
* question to this circumstance but how we transform this topic. what i do
18+
* here is that the max continues sequence of the number 1 construct the
19+
* height of the histogram, next step is to use the method previous. so we
20+
* can solve the question through two step: (1) find the max continues
21+
* sequence of the number 1 (2) use the method previous (3) the proof of the
22+
* transformation is in the resources of the project. the file name is
23+
* MaximalRectangle.docx
24+
*
25+
* the time complexity of the method is O(N3) the space complexity is O(n);
26+
*
27+
* */
28+
public int maximalRectangle(char[][] matrix) {
29+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
30+
return 0;
31+
int m = matrix.length;
32+
int n = matrix[0].length;
33+
int max = 0;
34+
int[] height = new int[n];
35+
for (int i = 0; i < m; i++) {
36+
for (int j = 0; j < n; j++) {
37+
if (matrix[i][j] == '0')
38+
height[j] = 0;
39+
else
40+
height[j] += 1;
41+
}
42+
max = Math.max(largestRectangleArea(height), max);
43+
}
44+
return max;
45+
46+
}
47+
private int largestRectangleArea(int[] height) {
48+
if (height == null || height.length == 0)
49+
return 0;
50+
int len = height.length;
51+
if (len == 1)
52+
return height[0];
53+
int max = 0;
54+
Stack<Integer> stack = new Stack<Integer>();
55+
int i = 0;
56+
while (i < len) {
57+
if (stack.empty() || height[i] >= stack.peek()) {
58+
stack.push(height[i]);
59+
i++;
60+
} else {
61+
int count = 0;
62+
while (!stack.empty() && stack.peek() > height[i]) {
63+
count++;
64+
int top = stack.peek();
65+
stack.pop();
66+
max = Math.max(max, top * count);
67+
}
68+
for (int j = 0; j < count + 1; j++) {
69+
stack.push(height[i]);
70+
}
71+
i++;
72+
}
73+
}
74+
int count = 0;
75+
while (!stack.empty()) {
76+
count++;
77+
max = Math.max(max, stack.peek() * count);
78+
stack.pop();
79+
}
80+
return max;
81+
}
82+
83+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.cz.algorithm.learn.two;
2+
3+
public class SudokuSolver {
4+
public static void main(String[] args) {
5+
6+
}
7+
8+
public void solveSudoku(char[][] board) {
9+
solution(board);
10+
}
11+
12+
private boolean solution(char[][] board) {
13+
for (int i = 0; i < 9; i++) {
14+
for (int j = 0; j < 9; j++) {
15+
if (board[i][j] == '.') {
16+
for (char k = '1'; k <= '9'; k++) {
17+
board[i][j] = k;
18+
if (isValid(board, i, j) && solution(board)) {
19+
return true;
20+
}
21+
board[i][j] = '.';
22+
}
23+
return false;
24+
}
25+
}
26+
}
27+
return true;
28+
}
29+
30+
// check weather satisfy the condition
31+
private boolean isValid(char[][] board, int x, int y) {
32+
// TODO Auto-generated method stub
33+
for(int i = 0; i < 9 ; i ++) {
34+
if(y != i && board[x][y] == board[x][i] ) {
35+
return false;
36+
}
37+
}
38+
for(int i = 0 ; i < 9 ; i ++) {
39+
if(x != i && board[x][y] == board[i][y] ) {
40+
return false;
41+
}
42+
}
43+
for(int i = (x / 3) * 3 ; i < (x / 3) * 3 + 3 ; i ++) {
44+
for(int j = ( y / 3) * 3 ; j < ( y / 3) * 3 + 3 ; j ++ ) {
45+
if (x != i && y != j && board[i][j] == board[x][y]) {
46+
return false;
47+
}
48+
}
49+
}
50+
return true;
51+
}
52+
}

0 commit comments

Comments
 (0)