Skip to content

Commit 06d2bca

Browse files
committedSep 17, 2012
Signed-off-by: yuduozhou <[email protected]>
1 parent 8597bd4 commit 06d2bca

9 files changed

+398
-0
lines changed
 

‎LargestRectangleArea.java

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// This is from heartfire.cc.
2+
import java.util.Stack;
3+
public class Solution {
4+
public int largestRectangleArea(int[] height) {
5+
// Start typing your Java solution below
6+
// DO NOT write main() function
7+
8+
//for every i, height[0...i] is a subproblem
9+
int i = 0, max = 0;
10+
Stack<Ele> st = new Stack<Ele>();
11+
while (i < height.length) {
12+
if (st.empty() || height[i] > st.peek().height) {
13+
st.push(new Ele(height[i], i));
14+
}
15+
else if (st.peek().height > height[i]){ //need to ignore the case where st.peek().height == height[i]
16+
int prev = 0;
17+
while (!st.empty() && st.peek().height > height[i]) {
18+
Ele e = st.pop();
19+
prev = e.index;
20+
max = Math.max(max, e.height * (i - e.index));
21+
}
22+
st.push(new Ele(height[i], prev));
23+
}
24+
i++;
25+
}
26+
27+
//stack may contain a series of ascending heights
28+
while (!st.empty()) {
29+
Ele e = st.pop();
30+
max = Math.max(max, e.height*(i - e.index));
31+
}
32+
33+
return max;
34+
}
35+
36+
private class Ele {
37+
int height;
38+
int index;
39+
40+
public Ele (int h, int i) {
41+
this.height = h;
42+
this.index = i;
43+
}
44+
}
45+
}
46+
47+
// My recursive version, but cannot pass large test.
48+
public class Solution {
49+
public int largestRectangleArea(int[] height) {
50+
// Start typing your Java solution below
51+
// DO NOT write main() function
52+
if (height.length < 1){
53+
return 0;
54+
}
55+
if (height.length == 1){
56+
return height[0];
57+
}
58+
int l = 0;
59+
int r = height.length - 1;
60+
return findLargestRectangle(height, l, r);
61+
}
62+
63+
public int findLargestRectangle(int[] height, int l, int r){
64+
if (l == r){
65+
return height[l];
66+
}
67+
int min = extractMin(height, l, r);
68+
int combine = (r - l + 1) * height[min];
69+
int left = (l < min) ? findLargestRectangle(height, l, min - 1) : height[l];
70+
int right = (min < r) ? findLargestRectangle(height, min + 1, r) : height[r];
71+
int bigger = (left > right)?left:right;
72+
return (combine > bigger)?combine : bigger;
73+
}
74+
75+
// return the index of the minimum element between l and r, inclusive.
76+
public int extractMin(int[] height, int l, int r){
77+
int min = l;
78+
for (int i = l + 1; i <= r; i++){
79+
if (height[i] < height[min]){
80+
min = i;
81+
}
82+
}
83+
return min;
84+
}
85+
}

‎LengthOfLastWord.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Solution {
2+
public int lengthOfLastWord(String s) {
3+
// Start typing your Java solution below
4+
// DO NOT write main() function
5+
if (s.length() < 1){
6+
return 0;
7+
}
8+
char[] chars = s.toCharArray();
9+
int len = 0;
10+
boolean flag = false;
11+
for (int i = chars.length - 1; i >=0; i--){
12+
if (chars[i] == ' '){
13+
if(flag){
14+
return len;
15+
}
16+
len = 0;
17+
}
18+
if (chars[i] != ' '){
19+
flag = true;
20+
len ++;
21+
}
22+
}
23+
return len;
24+
}
25+
}

‎LetterCombinations.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
public class Solution {
2+
public HashMap<Integer, String[]> Dict = new HashMap<Integer, String[]>();
3+
public ArrayList<String> letterCombinations(String digits) {
4+
// Start typing your Java solution below
5+
// DO NOT write main() function
6+
ArrayList<String> result = new ArrayList<String>();
7+
if (digits.equals("")){
8+
result.add("");
9+
return result;
10+
}
11+
initialDictionary();
12+
result = printAllCombination(digits);
13+
return result;
14+
}
15+
16+
public ArrayList<String> printAllCombination(String digits){
17+
ArrayList<String> result = new ArrayList<String>();
18+
if (digits.length() == 1){
19+
Integer number = Integer.parseInt(digits);
20+
String[] temp = Dict.get(number);
21+
for (String s : temp){
22+
result.add(s);
23+
}
24+
return result;
25+
}
26+
Integer pivot = Integer.parseInt(digits.substring(0, 1));
27+
String remains = digits.substring(1);
28+
String[] temp = Dict.get(pivot);
29+
for (String s : temp){
30+
for (String p : printAllCombination(remains)){
31+
result.add(s + p);
32+
}
33+
}
34+
return result;
35+
}
36+
37+
public void initialDictionary(){
38+
Dict.put(2, new String[]{"a", "b", "c"});
39+
Dict.put(3, new String[]{"d", "e", "f"});
40+
Dict.put(4, new String[]{"g", "h", "i"});
41+
Dict.put(5, new String[]{"j", "k", "l"});
42+
Dict.put(6, new String[]{"m", "n", "o"});
43+
Dict.put(7, new String[]{"p", "q", "r", "s"});
44+
Dict.put(8, new String[]{"t", "u", "v"});
45+
Dict.put(9, new String[]{"w", "x", "y", "z"});
46+
}
47+
}

‎LongestCommonPrefix.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class Solution {
2+
public String longestCommonPrefix(String[] strs) {
3+
// Start typing your Java solution below
4+
// DO NOT write main() function
5+
if (strs.length == 0){
6+
return "";
7+
}
8+
String prefix = strs[0];
9+
if (strs.length == 1){
10+
return prefix;
11+
}
12+
for (int i = 1; i < strs.length; i++){
13+
prefix = CommonPrefix(prefix, strs[i]);
14+
if (prefix == "")
15+
return prefix;
16+
}
17+
return prefix;
18+
}
19+
20+
public String CommonPrefix(String a, String b){
21+
int la= a.length();
22+
int lb = b.length();
23+
int limit = (la < lb)? la : lb;
24+
for (int i= 0; i < limit; i++){
25+
if(a.charAt(i) != b.charAt(i)){
26+
return a.substring(0, i);
27+
}
28+
}
29+
return a.substring(0, limit);
30+
}
31+
}

‎LongestPlaindrome.java

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Thanks to heartfire.cc
2+
public class Solution {
3+
public String longestPalindrome(String s) {
4+
// Start typing your Java solution below
5+
// DO NOT write main() function
6+
//the following is an O(n2) solution, which is not optimal
7+
int[] max = new int[s.length()];
8+
//special case s.length() == 0
9+
if (s.length() == 0) {
10+
return new String();
11+
}
12+
//init to 0s
13+
for (int i = 0; i < s.length(); i++) {
14+
max[i] = 0;
15+
}
16+
//try for each possible center.
17+
for (int c = 0; c < s.length(); c++) {
18+
//the center is at c
19+
int l = c, r = c;
20+
while (l >= 0 && r < s.length()) {
21+
if (s.charAt(l) == s.charAt(r)) {
22+
max[c] = r - l + 1;
23+
}
24+
else {
25+
break;
26+
}
27+
l--;
28+
r++;
29+
}
30+
//the center is between c and c+1
31+
l = c;
32+
r = c+1;
33+
while (l >= 0 && r < s.length()) {
34+
if (s.charAt(l) == s.charAt(r)) {
35+
if (max[c] < r - l + 1) {
36+
max[c] = r - l + 1;
37+
}
38+
}
39+
else {
40+
break;
41+
}
42+
l--;
43+
r++;
44+
}
45+
}
46+
//find the max
47+
int m = 0, index = 0;
48+
for (int i = 0 ; i < s.length(); i++) {
49+
if (m < max[i]) {
50+
m = max[i];
51+
index = i;
52+
}
53+
}
54+
return s.substring(index-(max[index] - 1)/2, index + max[index]/2 + 1);
55+
}
56+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class Solution {
2+
public int lengthOfLongestSubstring(String s) {
3+
// Start typing your Java solution below
4+
// DO NOT write main() function
5+
HashMap<String, Integer> visited = new HashMap<String, Integer>();
6+
int l = 0;
7+
int r = 0;
8+
int n = s.length();
9+
int max = 0;
10+
while (r < n){
11+
String curr = s.substring(r, r + 1);
12+
if(visited.containsKey(curr)){
13+
l = visited.get(curr) + 1;
14+
r = l;
15+
visited.clear();
16+
continue;
17+
}
18+
else{
19+
visited.put(curr, r);
20+
r ++;
21+
}
22+
max = ((r - l) > max) ? (r - l) : max;
23+
}
24+
return max;
25+
}
26+
}

‎LongestValidParentheses.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
public class Solution {
2+
public int longestValidParentheses(String s) {
3+
// Start typing your Java solution below
4+
// DO NOT write main() function
5+
Stack<Integer> left = new Stack<Integer>();
6+
ArrayList<Integer> couples = new ArrayList<Integer>();
7+
8+
for (int i = 0; i < s.length(); i++){
9+
char c = s.charAt(i);
10+
if (c == '('){
11+
left.push(i);
12+
}
13+
else if (c == ')'){
14+
if (!left.isEmpty()){
15+
int l = left.pop();
16+
couples.add(l);
17+
couples.add(i);
18+
}
19+
}
20+
}
21+
if (couples.size() < 2){
22+
return 0;
23+
}
24+
Collections.sort(couples);
25+
int max = 0;
26+
int i = 0;
27+
int j = 0;
28+
while (j < couples.size()){
29+
if(couples.get(j) - couples.get(i) > j - i){
30+
max = (max > (j - i))? max : j-i;
31+
i = j;
32+
}
33+
else{
34+
j ++;
35+
}
36+
}
37+
max = (max > (j - i))? max : j-i;
38+
return max;
39+
}
40+
}

‎MaximalRectangle.java

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
Thanks to heartfire.cc.
3+
Use the ¡°max area under a histogram¡± algorithm as a building block. For each row in
4+
the 2D matrix, we suppose the potential max rectangle ends at this row, and call the
5+
¡° max area under a histogram¡± algorithm. The heights for each column should be the
6+
number of ¡¯1¡äs after the last seen ¡¯0¡ä for that column.
7+
8+
This is the optimal solution. Time complexity O(n^2).
9+
*/
10+
11+
public class Solution {
12+
public int maximalRectangle(char[][] matrix) {
13+
// Start typing your Java solution below
14+
// DO NOT write main() function
15+
16+
if (matrix.length == 0) {
17+
return 0;
18+
}
19+
int m = matrix.length, n = matrix[0].length, max = 0;
20+
int[] heights = new int[n];
21+
for (int i = 0; i < m; i++) {
22+
for (int j = 0; j < n; j++) {
23+
if (matrix[i][j] == '1') {
24+
heights[j]++;
25+
}
26+
else {
27+
heights[j] = 0;
28+
}
29+
}
30+
31+
max = Math.max(max, maxHistoArea(heights));
32+
}
33+
34+
return max;
35+
}
36+
37+
public int maxHistoArea(int[] heights) {
38+
class Ele {
39+
int h;
40+
int i;
41+
public Ele (int h, int i) {
42+
this.h = h;
43+
this.i = i;
44+
}
45+
}
46+
47+
Stack<Ele> st = new Stack<Ele>();
48+
int max = 0, i = 0;
49+
50+
for (i = 0; i < heights.length; i++) {
51+
if (st.empty() || heights[i] > st.peek().h) {
52+
st.push(new Ele(heights[i], i));
53+
}
54+
else if (heights[i] < st.peek().h) { //ignore the equal case
55+
int prev = 0;
56+
while (!st.empty() && heights[i] < st.peek().h) {
57+
Ele e = st.pop();
58+
prev = e.i;
59+
max = Math.max(max, e.h * (i - e.i));
60+
}
61+
st.push(new Ele(heights[i], prev));
62+
}
63+
}
64+
65+
while (!st.empty()) {
66+
Ele e = st.pop();
67+
max = Math.max(max, e.h * (i - e.i));
68+
}
69+
70+
return max;
71+
}
72+
}

‎MaximumSubarray.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class Solution {
2+
public int maxSubArray(int[] A) {
3+
// Start typing your Java solution below
4+
// DO NOT write main() function
5+
int sum = 0;
6+
int max = Integer.MIN_VALUE;
7+
for (int i = 0; i < A.length; i++){
8+
sum += A[i];
9+
max = (max < sum)? sum : max;
10+
if (sum <= 0){
11+
sum = 0;
12+
}
13+
}
14+
return max;
15+
}
16+
}

0 commit comments

Comments
 (0)
Please sign in to comment.