Skip to content

Commit

Permalink
added DynamicProgramming
Browse files Browse the repository at this point in the history
  • Loading branch information
kdn251 committed Feb 16, 2017
1 parent da711d5 commit 1e04f74
Show file tree
Hide file tree
Showing 12 changed files with 619 additions and 0 deletions.
87 changes: 87 additions & 0 deletions DynamicProgramming/bombEnemy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return the maximum enemies you can kill using one bomb.
// The bomb kills all the enemies in the same row and column from the planted point until it hits the wall since the wall is too strong to be destroyed.
// Note that you can only put the bomb at an empty cell.

// Example:
// For the given grid

// 0 E 0 0
// E 0 W E
// 0 E 0 0

// return 3. (Placing a bomb at (1,1) kills 3 enemies)

public class Solution {

public int maxKilledEnemies(char[][] grid) {

if(grid == null || grid.length == 0 || grid[0].length == 0) return 0;

int max = 0;
int row = 0;
int[] col = new int[grid[0].length];

for(int i = 0; i<grid.length; i++) {

for(int j = 0; j<grid[0].length;j++) {

if(grid[i][j] == 'W') continue;
if(j == 0 || grid[i][j-1] == 'W') {

row = killedEnemiesRow(grid, i, j);

}

if(i == 0 || grid[i-1][j] == 'W') {

col[j] = killedEnemiesCol(grid,i,j);

}

if(grid[i][j] == '0') {

max = (row + col[j] > max) ? row + col[j] : max;

}

}

}

return max;

}

//calculate killed enemies for row i from column j
private int killedEnemiesRow(char[][] grid, int i, int j) {

int num = 0;

while(j <= grid[0].length-1 && grid[i][j] != 'W') {

if(grid[i][j] == 'E') num++;
j++;

}

return num;

}

//calculate killed enemies for column j from row i
private int killedEnemiesCol(char[][] grid, int i, int j) {

int num = 0;

while(i <= grid.length -1 && grid[i][j] != 'W'){

if(grid[i][j] == 'E') num++;
i++;

}

return num;

}

}
26 changes: 26 additions & 0 deletions DynamicProgramming/climbingStairs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// You are climbing a stair case. It takes n steps to reach to the top.

// Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

// Note: Given n will be a positive integer.

public class Solution {

public int climbStairs(int n) {

int[] dp = new int[n + 1];

dp[0] = 1;
dp[1] = 1;

for(int i = 2; i < dp.length; i++) {

dp[i] = dp[i - 1] + dp[i - 2];

}

return dp[dp.length - 1];

}

}
50 changes: 50 additions & 0 deletions DynamicProgramming/combinationSumIV.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

// Example:

// nums = [1, 2, 3]
// target = 4

// The possible combination ways are:
// (1, 1, 1, 1)
// (1, 1, 2)
// (1, 2, 1)
// (1, 3)
// (2, 1, 1)
// (2, 2)
// (3, 1)

// Note that different sequences are counted as different combinations.

// Therefore the output is 7.

// Follow up:
// What if negative numbers are allowed in the given array?
// How does it change the problem?
// What limitation we need to add to the question to allow negative numbers?

public class Solution {

public int combinationSum4(int[] nums, int target) {

int[] dp = new int[target + 1];
dp[0] = 1;

for(int i = 1; i < dp.length; i++) {
for(int j = 0; j < nums.length; j++) {

if(i - nums[j] >= 0) {

dp[i] += dp[i - nums[j]];

}

}

}

return dp[target];

}

}
29 changes: 29 additions & 0 deletions DynamicProgramming/countingBits.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.

// Example:
// For num = 5 you should return [0,1,1,2,1,2].

// Follow up:
// It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
// Space complexity should be O(n).
// Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.

public class Solution {

public int[] countBits(int num) {

int[] bits = new int[num + 1];

bits[0] = 0;

for(int i = 1; i <= num; i++) {

bits[i] = bits[i >> 1] + (i & 1);

}

return bits;

}

}
59 changes: 59 additions & 0 deletions DynamicProgramming/editDistance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)

// You have the following 3 operations permitted on a word:

// a) Insert a character
// b) Delete a character
// c) Replace a character

public class Solution {

public int minDistance(String word1, String word2) {

int m = word1.length();
int n = word2.length();

int[][] dp = new int[m + 1][n + 1];

for(int i = 0; i <= m; i++) {

dp[i][0] = i;

}

for(int i = 0; i <= n; i++) {

dp[0][i] = i;

}

for(int i = 0; i < m; i++) {

for(int j = 0; j < n; j++) {

if(word1.charAt(i) == word2.charAt(j)) {

dp[i + 1][j + 1] = dp[i][j];

}

else {

int a = dp[i][j];
int b = dp[i][j + 1];
int c = dp[i + 1][j];

dp[i + 1][j + 1] = Math.min(a, Math.min(b, c));
dp[i + 1][j + 1]++;

}

}

}

return dp[m][n];

}

}
33 changes: 33 additions & 0 deletions DynamicProgramming/houseRobber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

// Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

public class Solution {

public int rob(int[] nums) {

if(nums.length == 0) return 0;
if(nums.length == 1) return nums[0];

int[] dp = new int[nums.length];

dp[0] = nums[0];
dp[1] = nums[0] > nums[1] ? nums[0] : nums[1];

for(int i = 2; i < nums.length; i++) {

dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);

}

for(int i = 0; i < dp.length; i++) {

System.out.print(dp[i] + " ");

}

return dp[dp.length - 1];

}

}
35 changes: 35 additions & 0 deletions DynamicProgramming/paintFence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// There is a fence with n posts, each post can be painted with one of the k colors.

// You have to paint all the posts such that no more than two adjacent fence posts have the same color.

// Return the total number of ways you can paint the fence.

// Note:
// n and k are non-negative integers.

public class Solution {

public int numWays(int n, int k) {

if(n <= 0) {

return 0;

}

int sameColorCounts = 0;
int differentColorCounts = k;

for(int i = 2; i <= n; i++) {

int temp = differentColorCounts;
differentColorCounts = (sameColorCounts + differentColorCounts) * (k - 1);
sameColorCounts = temp;

}

return sameColorCounts + differentColorCounts;

}

}
65 changes: 65 additions & 0 deletions DynamicProgramming/paintHouseII.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.

// The cost of painting each house with a certain color is represented by a n x k cost matrix. For example, costs[0][0] is the cost of painting house 0 with color 0; costs[1][2] is the cost of painting house 1 with color 2, and so on... Find the minimum cost to paint all houses.

// Note:
// All costs are positive integers.

// Follow up:
// Could you solve it in O(nk) runtime?

public class Solution {

public int minCostII(int[][] costs) {

if(costs == null|| costs.length == 0) return 0;

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

int min1 = -1;
int min2 = -1;

for(int i = 0; i < m; i++) {

int last1 = min1;
int last2 = min2;
min1 = -1;
min2 = -1;

for(int j = 0; j < n; j++) {

if(j != last1) {

costs[i][j] += last1 < 0 ? 0 : costs[i - 1][last1];

}

else {

costs[i][j] += last2 < 0 ? 0 : costs[i - 1][last2];

}

if(min1 < 0 || costs[i][j] < costs[i][min1]) {

min2 = min1;
min1 = j;

}

else if(min2 < 0 || costs[i][j] < costs[i][min2]) {

min2 = j;

}

}

}

return costs[m - 1][min1];

}

}
Loading

0 comments on commit 1e04f74

Please sign in to comment.