# 303. Range Sum Query - Immutable

``````class NumArray {
int[] sumnums;
public NumArray(int[] nums) {

if(nums.length==0)
return;
sumnums = new int[nums.length];
sumnums[0] = nums[0];
for(int i=1;i<nums.length;i++){
sumnums[i] = sumnums[i-1] + nums[I];
}
}

public int sumRange(int i, int j) {
if(i==0)
return sumnums[j];
return sumnums[j] - sumnums[i-1];
}
}

/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* int param_1 = obj.sumRange(i,j);
*/
``````

# 304. Range Sum Query 2D - Immutable

``````class NumMatrix {
int[][] summatrix;
public NumMatrix(int[][] matrix) {
if(matrix==null || matrix.length==0)
return;
summatrix = new int[matrix.length][matrix[0].length];
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[i].length;j++){
if(i==0 && j==0)
summatrix[i][j] = matrix[i][j];
else if(i==0)
summatrix[i][j] = summatrix[i][j-1] + matrix[i][j];
else if(j==0)
summatrix[i][j] = summatrix[i-1][j] + matrix[i][j];
else
summatrix[i][j] = summatrix[i-1][j] + summatrix[i][j-1] - summatrix[i-1][j-1] + matrix[i][j];
}
}
}

public int sumRegion(int row1, int col1, int row2, int col2) {
if(row1==0 && col1==0)
return summatrix[row2][col2];
else if(row1==0)
return summatrix[row2][col2] - summatrix[row2][col1-1];
else if(col1==0)
return summatrix[row2][col2] - summatrix[row1-1][col2];
else
return summatrix[row2][col2] - summatrix[row1-1][col2] - summatrix[row2][col1-1] + summatrix[row1-1][col1-1];
}
}

/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
*/
``````

# 307. Range Sum Query - Mutable

``````class NumArray {
int[] sumnums;
int[] orgnums;
public NumArray(int[] nums) {
if(nums==null || nums.length==0)
return;
sumnums = new int[nums.length];
sumnums[0] = nums[0];
orgnums = new int[nums.length];
orgnums[0] = nums[0];
for(int i=1;i<nums.length;i++){
sumnums[i] = sumnums[i-1] + nums[I];
orgnums[i] = nums[I];
}
}

public void update(int i, int val) {
for(int j=i;j<sumnums.length;j++){
sumnums[j] += (val - orgnums[I]);
}
orgnums[i] = val;
}

public int sumRange(int i, int j) {
if(i==0)
return sumnums[j];
return sumnums[j] - sumnums[i-1];
}
}

/**
* Your NumArray object will be instantiated and called as such:
* NumArray obj = new NumArray(nums);
* obj.update(i,val);
* int param_2 = obj.sumRange(i,j);
*/
``````

# 309. Best Time to Buy and Sell Stock with Cooldown

``````class Solution {
int maxProfit = 0;
public int maxProfit(int[] prices) {
backprop(prices,0,0,0,0);
return maxProfit;
}
public void backprop(int[] prices,int start,int profit,int buyPrice,int type){
if(start >= prices.length){
maxProfit = Math.max(maxProfit,profit);
}
if(type==0){
for(int i=start;i<prices.length;i++){
backprop(prices,i+1,profit,prices[i],1);
}
}
else if(type==1){
for(int i=start;i<prices.length;i++){
}
}
}
}
``````

sell[i]表示在第i天之前最后一个操作是卖，此时的最大收益。
rest[i]表示在第i天之前最后一个操作是冷冻期，此时的最大收益。

``````buy[i]  = max(rest[i-1] - price, buy[i-1])
sell[i] = max(buy[i-1] + price, sell[i-1])
rest[i] = max(sell[i-1], rest[i-1])
``````

``````buy[i]  = max(sell[i-2] - price, buy[i-1])
sell[i] = max(buy[i-1] + price, sell[i-1])
``````

``````class Solution {
public int maxProfit(int[] prices) {
if(prices==null || prices.length <2)
return 0;
int[] sell = new int[prices.length];
sell[0] = 0;
sell[1] = Math.max(0,prices[1]-prices[0]);
for(int i=2;i<prices.length;i++){
}
return sell[prices.length-1];
}
}
``````

# 310. Minimum Height Trees

``````class Solution {
public List<Integer> findMinHeightTrees(int n, int[][] edges) {
List<Integer> leaves = new ArrayList<>();
if(n <= 1) {
return Collections.singletonList(0);
}
Map<Integer, Set<Integer>> graph = new HashMap<>();     // list of edges to  Ajacency Lists

for(int i = 0; i < n; i++) {
graph.put(i, new HashSet<Integer>());
}
for(int[] edge : edges) {
}

for(int i = 0; i < n; i++) {
if(graph.get(i).size() == 1) {
}
}

while(n > 2) {
n -= leaves.size();
List<Integer> newLeaves = new ArrayList<>();
for(int leaf : leaves) {
for(int newLeaf : graph.get(leaf)) {
graph.get(leaf).remove(newLeaf);
graph.get(newLeaf).remove(leaf);
if(graph.get(newLeaf).size() == 1) {