Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions 4th/homework_1/id_80/MinimumPathSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

class Solution {
public int minPathSum(int[][] grid) {
// 比较典型的dp, 找最小。
// 应该是需要dfs?全盘dfs吗?好像是需要额。
// 方式一是dfs,全盘遍历,记录最小值。
// 方式二是dp. 从最底层元素往回找,更新每个元素的值。最小值则是对于起点元素,求出其右边的值和下边的值,哪个更小,然后加上自己,就是返回值。
// dp的优势在于,减少了重复计算。 其实可以从头开始遍历, O(m*n)


if(grid == null || grid.length == 0) {
return -1; //应该是不会出现这种情况。
}

int m = grid.length;
int n = grid[0].length;

int [][] memo = new int [m][n];
memo[m-1][n-1] = grid[m-1][n-1];

for (int i=m-1; i>= 0;i--) {
for(int j=n-1;j>=0; j--) {
if (j == n-1 && i == m-1) {
continue;
}
memo[i][j] = getMin(grid, memo, i, j);
}
}

return memo[0][0];

}

private int getMin(int[][] grid,int[][] memo, int i, int j) {


int ret = 0;
if(i<grid.length-1&&j<grid[0].length -1) {
ret = Math.min(memo[i+1][j],memo[i][j+1]) + grid[i][j];
} else if (i<grid.length - 1 && j == grid[0].length-1) { //在最右边一列
ret = memo[i+1][j] + grid[i][j];
} else { //在最下面一行
ret = memo[i][j+1] + grid[i][j];
}

return ret;


}




}
22 changes: 22 additions & 0 deletions 4th/homework_2/id_80/BestTimeToSellStock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

class Solution {
// 这个算dp?
public int maxProfit(int[] prices) {

if(prices == null || prices.length == 0) {
return 0;
}

int min = prices[0];
int ret = 0;

for(int i=1; i<prices.length; i++) {

ret = Math.max(ret, prices[i] - min);
min = Math.min(min, prices[i]);

}
return ret;

}
}
20 changes: 20 additions & 0 deletions 4th/homework_3/id_80/Stock2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

class Solution {
public int maxProfit(int[] prices) {
// 没思路 啊?
// 多次买入卖出。

// 只能dp吧?贪心估计算出来不是全局最优。
//可以考虑,以每个节点为买入点,一直遍历到结尾,求出其所有可能的卖出点。但是这样是单次的吧,第二次买入点了?
// 想不出来,看答案了。

// 艹尼玛!!!看了答案,好简单啊!!!

int total = 0;
for (int i=0; i< prices.length-1; i++) {
if (prices[i+1]>prices[i]) total += prices[i+1]-prices[i];
}

return total;
}
}
37 changes: 37 additions & 0 deletions 4th/homework_4/id_80/ClimbStairs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@


class Solution {


public int climbStairs(int n) {

// 一共n阶台阶
// 每次一阶或者两阶
// 共有几种方式完成攀登。

// 感觉和生成括号有点类似。
// dfs? --> 超时了。
// 二分搜索后相乘? --》应该是这个思路。 --->操蛋了。

//根据test发现,其实是个fib数列。我曹了。 --> 原理是,到达最后一个节点的方法,只有两种,即从倒数第二个到达,和从倒数第三个到达

if (n ==1) {
return 1;
} if(n == 2) {
return 2;
}
int [] result = new int [n];
result[0] =1;
result[1]=2;

for (int i=2; i<n ; i++) {
result[i] = result[i-1]+result[i-2];
}
return result[n-1];

}




}
37 changes: 37 additions & 0 deletions 4th/homework_5/id_80/Robber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

class Solution {
public int rob(int[] nums) {

// 题意是还算简单易懂,但是怎么个做法了。

// 求max。

// 直接遍历,按单双数是有问题的。

//其实是不是只有四种情况,以1或2为起点,然后3/4和 4/5变成了可以选择的选项?
//求出最大值,一直更新最大值即可。
//这个算dfs吧

// 那dp了?想不出来dp有什么招啊! dfs肯定又超时。

//最后两家肯定抢一家。
// 好了,只想出了low的dfs.
if(nums == null || nums.length ==0) {
return 0;
}

// 有个关系在这。
for(int i=0; i<nums.length; i++) {
if (i==0) {
continue;
}
if(i ==1) {
nums[1] = Math.max(nums[0], nums[1]);
continue;
}
nums[i] = Math.max(nums[i-2] +nums[i], nums[i-1]);
}

return nums[nums.length -1];
}
}
31 changes: 31 additions & 0 deletions 4th/homework_6/id_80/Triangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

class Solution {

public int minimumTotal(List<List<Integer>> triangle) {

// dp
// 从下至上,修改没一个值。
if(triangle == null ||triangle.size() == 0) {
return 0;
}

int len = triangle.size();

for(int i=len - 2; i>=0; i--) {
List<Integer> list = triangle.get(i);
List<Integer> list2 = triangle.get(i+1);

for(int j =0;j< list.size(); j++) {
list.set(j, Math.min(list2.get(j), list2.get(j+1)) + list.get(j));
}

}
return triangle.get(0).get(0);



}

}


42 changes: 42 additions & 0 deletions 4th/homework_7/id_80/MaximumProductSubarray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

class Solution {
public int maxProduct(int[] nums) {

//1. 求乘积. 0也可以考虑进去的,关键要考虑负数的问题。 --》 忘了。还要是相邻的。
//2. dfs怎么搞?
// dp又怎么搞? 如何找到要递推的东西?

//想不出来。 负数的位置让我找不出任何规律。

//看答案。


int r = nums[0];
int n = nums.length;
// imax/imin stores the max/min product of
// subarray that ends with the current number A[i]
for (int i = 1, imax = r, imin = r; i < n; i++) {
// multiplied by a negative makes big number smaller, small number bigger
// so we redefine the extremums by swapping them
if (nums[i] < 0) {
int temp = imin;
imin = imax;
imax = temp;
}


// max/min product for the current number is either the current number itself
// or the max/min by the previous number times the current one
imax = Math.max(nums[i], imax * nums[i]);
imin = Math.min(nums[i], imin * nums[i]);

// the newly computed max value is a candidate for our global result
r = Math.max(r, imax);
}
return r;




}
}
54 changes: 54 additions & 0 deletions 4th/homework_8/id_80/Robber2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

class Solution {
public int rob(int[] nums) {

// 直接拿1 的答案过来套, 因为有了环,所以要么终点往前移一个,要么起点往后移一个。
//

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



int max[] = new int[nums.length-1];
int max2[] = new int[nums.length -1];

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

for(int i=1; i<nums.length ; i++) {
if (i==1) {
max2[0] = nums[1];
continue;
}
if(i ==2) {
max2[1] = Math.max(nums[1], nums[2]);
continue;
}
max2[i-1] = Math.max(max2[i-3] +nums[i], max2[i-2]);
}

return Math.max(max[max.length -1], max2[max.length-1]);





}
}
48 changes: 48 additions & 0 deletions 4th/homework_9/id_80/CoinChange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@


class Solution {

private HashMap<Integer, Integer> map = new HashMap<>();
public int coinChange(int[] coins, int amount) {

// 刷硬币的问题
//用多少次硬币能解决问题。
// 解决问题的标志是什么?--》 关键得找出这个状态转移方程来。

// 想不出来,超时了,看答案。

//递归往回找。 StackOverFlow了。。


// 说实话想不通。
// 看代码都有点看不懂。。

// 多看了几遍答案,现在来自己手写。
if(amount == 0) {
return 0;
}
if(map.get(amount) != null){
return map.get(amount);
}

int min = Integer.MAX_VALUE; // 其实只要比amount大就可以了。
for (int coin : coins) {
int current = 0;
if (amount >= coin) {
int next = coinChange(coins, amount - coin);
if(next >=0) {
current = next + 1;
}
}

if(current > 0) {
//说明至少找到了。要去找最小的了。
min = Math.min(min, current);
}
}

int finalCount = (min==Integer.MAX_VALUE) ? -1 : min;
map.put(amount,finalCount);
return finalCount;
}
}