Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mitcc committed Nov 26, 2013
1 parent 137c941 commit fcac6d0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 29 deletions.
17 changes: 10 additions & 7 deletions leetcode/MaximumSubarray.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
* the contiguous subarray [4,−1,2,1] has the largest sum = 6.
*/
public class MaximumSubarray {
/********************** updated 2013/11/26 *********************/
public int maxSubArray(int[] A) {
int max = A[0], sum = 0;
int max = Integer.MIN_VALUE, tempMax = 0;
for(int i = 0; i < A.length; i++) {
sum += A[i];
max = Math.max(max, sum);
sum = sum < 0 ? 0 : sum;
tempMax += A[i];
if(max < tempMax)
max = tempMax;
if(tempMax < 0)
tempMax = 0;
}
return max;
}

/** Below is from wikipedia */
/* public int maxSubArray(int[] A) {
/****************** Below is from wikipedia ******************/
public int maxSubArray(int[] A) {
int max_so_far = A[0], max_ending_here = A[0];
for(int i = 1; i < A.length; i++) {//recursion starts from 1.
if(max_ending_here < 0)
Expand All @@ -26,5 +29,5 @@ public int maxSubArray(int[] A) {
max_so_far = max_ending_here;
}
return max_so_far;
}*/
}
}
58 changes: 36 additions & 22 deletions leetcode/PascalTriangleII.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
package info.mitcc.leetcode;
/*
* Given an index k, return the kth row of the Pascal's triangle.
import java.util.ArrayList;
import java.util.Scanner;
* For example, given k = 3,
* Return [1,3,3,1].
* Note:
* Could you optimize your algorithm to use only O(k) extra space?
*/
public class PascalTriangleII {
public ArrayList<Integer> getRow(int rowIndex) {
if(rowIndex < 0)
throw new IllegalArgumentException();
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
if(rowIndex == 0)
return list;
else {
ArrayList<Integer> tempList = this.getRow(rowIndex - 1);
for(int i = 1; i < rowIndex; i++)
list.add(tempList.get(i - 1) + tempList.get(i));
list.add(1);
return list;
}
/**************************** updated 2013/11/26 *********************/
public ArrayList<Integer> getRow(int rowIndex) {
ArrayList<Integer> res = new ArrayList<Integer>(), temp = new ArrayList<Integer>();
res.add(1);
temp.add(1);
temp.add(1);
for(int i = 1; i <= rowIndex; i++) {
res = new ArrayList<Integer>();
res.add(1);
for(int j = 0; j <= i - 2; j++)
res.add(temp.get(j) + temp.get(j + 1));
res.add(1);
temp = res;
}
return res;
}

/*********************************************************************/

public ArrayList<Integer> getRow(int rowIndex) {
ArrayList<Integer> res = new ArrayList<Integer>();
res.add(1);
if(rowIndex > 0) {
ArrayList<Integer> temp = getRow(rowIndex - 1);
for(int i = 0; i <= rowIndex - 2; i++)
res.add(temp.get(i) + temp.get(i + 1));
res.add(1);
}
return res;
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println(new PascalTriangleII().getRow(in.nextInt()));
}
}

0 comments on commit fcac6d0

Please sign in to comment.