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
27 changes: 27 additions & 0 deletions Week_04/id_33/Leetcode_46_33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* @lc app=leetcode id=46 lang=java
*
* [46] Permutations
*/
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
backTrace(nums, result, new ArrayList<>());
return result;
}

public void backTrace(int[] nums, List<List<Integer>> result, List<Integer> tempList){
if(tempList.size() == nums.length){
result.add(new ArrayList<Integer>(tempList));
return;
}

for(int i = 0; i< nums.length; i++){
if(tempList.contains(nums[i])) continue;
tempList.add(nums[i]);
backTrace(nums, result, tempList);
tempList.remove(tempList.size()-1);
}
}
}

17 changes: 17 additions & 0 deletions Week_04/id_33/Leetcode_53_33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* @lc app=leetcode id=53 lang=java
*
* [53] Maximum Subarray
*/
class Solution {
public int maxSubArray(int[] nums) {
if(nums.length==0) return 0;
int maxSum = nums[0], sum = nums[0];
for (int i = 1; i < nums.length; i++){
sum = Math.max(nums[i], sum + nums[i]);
maxSum = Math.max(sum, maxSum);
}
return maxSum;
}
}

19 changes: 19 additions & 0 deletions Week_04/id_33/Leetcode_62_33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* @lc app=leetcode id=62 lang=java
*
* [62] Unique Paths
*/
class Solution {
public int uniquePaths(int m, int n) {
int[][] dp = new int[n][m];
for (int i = 0; i < n; i++) dp[i][0] = 1;
for (int i = 0; i < m; i++) dp[0][i] = 1;
for (int i = 1; i < n; i++){
for (int j = 1; j < m; j++){
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[n-1][m-1];
}
}

18 changes: 18 additions & 0 deletions Week_04/id_33/Leetcode_70_33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* @lc app=leetcode id=70 lang=java
*
* [70] Climbing Stairs
*/
class Solution {
public int climbStairs(int n) {
if (n == 1) return 1;
int[] dp = new int[n + 1];
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
}

21 changes: 21 additions & 0 deletions Week_04/id_33/Leetcode_714_33.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* @lc app=leetcode id=714 lang=java
*
* [714] Best Time to Buy and Sell Stock with Transaction Fee
*/
class Solution {
public int maxProfit(int[] prices, int fee) {
if(prices.length <= 1) return 0;
int profit = 0, min = prices[0];
for(int i = 0;i < prices.length;i++){
if(prices[i] < min){
min = prices[i];
}else if(prices[i] > min + fee){
profit += prices[i] - min - fee;
min = prices[i] - fee;
}
}
return profit;
}
}