Skip to content

Commit

Permalink
add dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
FS1360472174 committed Jul 15, 2018
1 parent 61e5bd2 commit c1827de
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
55 changes: 55 additions & 0 deletions alg/src/main/java/greedy/Parse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package greedy;

/**
* @author [email protected]
*/
public class Parse {
public static void main(String[] args) {
int[] nums = new int[] {2,3,1,1,4};
int [] nums2 = new int[] {3,2,1,0,4};
int[] nums3 = new int[] {2,0};
System.out.println(canJump(nums));
System.out.println(canJump(nums2));
System.out.println(canJump(nums3));
}


/**
* 通配符匹配,
* https://leetcode-cn.com/problems/wildcard-matching/description/
* 这题简单的做法应该就是迭代判断
* @param s
* @param p
* @return
*/
public static boolean isMatch(String s, String p) {
return true;
}


/**
* 跳跃游戏,就是图
* @param nums
* @return
*/
public static boolean canJump(int[] nums) {
if (nums.length <= 1) {
return true;
}
int i;
int right ;
int left;
for (i =0; i< nums.length && nums[i] !=0;) {
right = i + nums[i];
left = i + 1;
for (int j = i+1; j< nums.length && j<= right; j++) {
if (j + nums[j] >= right) {
right = j + nums[j];
left = j;
}
}
i = left;
}
return i == nums.length;
}
}
102 changes: 102 additions & 0 deletions alg/src/main/java/plan/Climb.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package plan;

import java.util.Arrays;

/**
* @author [email protected]
*/
public class Climb {
public static void main(String[] args) {
//System.out.println(climbStairs(4));
// int[] rob1 = new int[] {2,7,9,3,1};
//System.out.println(rob(rob1));
//int[] rob2 = new int[] {1,2,3,1};
// System.out.println(rob(rob2));
int[] array = new int[] {-2,1,-3,4,-1,2,1,-5,4};
System.out.println(maxSubArray(array));
}

/**
* 爬梯子https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/23/dynamic-programming/54/
* dp[n] = dp[n-2] + dp[n-1]
* @param n
* @return
*/
public static int climbStairs(int n) {
if(n < 1) {
return 0;
}

if (n == 1) {
return 1;
}

if (n == 2) {
return 2;
}
int pre[] = new int [] {1,2};
int result = pre[0] + pre[1];
for (int i = 3; i <= n; i++) {
result = pre[0] + pre[1];
pre[0] = pre[1];
pre[1] = result;
}
return result;
}

/**
* https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/23/dynamic-programming/57/
* dp[i] = max(dp[i-2] + nums[i], dp[i-1])
* @param nums
* @return
*/
public static int rob(int[] nums) {
if (nums.length ==0) {
return 0;
}
if (nums.length == 1) {
return nums[0];
}

if (nums.length ==2) {
return Math.max(nums[0],nums[1]);
}

int dp[] = new int[] {nums[0], Math.max(nums[0], nums[1])};
int result=0;
for (int i = 2; i < nums.length; i++) {
result = Math.max(dp[0] + nums[i], dp[1]);
dp[0] = dp[1];
dp[1] = result;
}
return result;
}

/**
* dp[n] = max[dp[n-1],dp[n-1]+n,n]
* @param nums
* @return
*/
public static int maxSubArray(int[] nums) {
if (nums.length == 0) {
return 0;
}

if (nums.length == 1) {
return nums[0];
}
int result = 0;
if (nums.length ==2) {
result = Math.max(nums[0],nums[1]);
result = Math.max(result, nums[0] + nums[1]);
}
int dp = result;
for (int i = 2; i< nums.length; i++) {
result = Math.max(dp, dp+nums[i]);
result = Math.max(result, nums[i]);
dp = result;
}
return result;
}

}

0 comments on commit c1827de

Please sign in to comment.