Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5b27a31

Browse files
author
C5141506
committed
Leetcode problems
1 parent f40ce40 commit 5b27a31

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package java_problem.dynamic_programming;
2+
3+
public class EggBreaks {
4+
public static void main(String[] args) {
5+
System.out.println(twoEggDrop(7));
6+
}
7+
8+
public static int twoEggDrop(int floor) {
9+
int egg = 2; // hard coded to 2 eggs for this problem
10+
int[][] dp = new int[floor + 1][egg + 1];
11+
return eggDrop(floor, egg, dp);
12+
}
13+
14+
public static int eggDrop(int floor, int egg, int[][] dp) {
15+
if (floor <= 2 || egg == 1) return floor;
16+
if (dp[floor][egg] != 0) return dp[floor][egg];
17+
int min = floor; // when you drop at each floor starting from 1
18+
for (int flr = 1; flr < floor; flr++) {
19+
int eggBreak = 1 + eggDrop(flr - 1, egg - 1, dp); // drops needed if egg breaks at this floor
20+
int noEggBreak = 1 + eggDrop(floor - flr, egg, dp); // drops needed if egg does not break at this floor
21+
int moves = Math.max(eggBreak, noEggBreak); // since we want certain moves for floor floor take max
22+
min = Math.min(min, moves);
23+
}
24+
dp[floor][egg] = min;
25+
return min;
26+
}
27+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package java_problem.dynamic_programming;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class StringPermutation {
7+
public static void main(String[] args) {
8+
System.out.println(checkInclusion("ab", "eidbaooo"));
9+
}
10+
11+
//return true if s2 contains permutation of s1
12+
public static boolean checkInclusion(String s1, String s2) {
13+
int n2 = s2.length();
14+
int totalPermutation = fact(n2);
15+
List<String> permutations = findPermutation(totalPermutation, s2);
16+
return permutations.contains(s1);
17+
}
18+
19+
public static List<String> findPermutation(int totalPermutation, String s) {
20+
List<String> permutations = new ArrayList<>();
21+
for (int i = 0; i < totalPermutation; i++) {
22+
StringBuilder sb = new StringBuilder(s);
23+
int dividend = i;
24+
StringBuilder permutation = new StringBuilder();
25+
for (int divisor = s.length(); divisor >= 1; divisor--) {
26+
int q = dividend / divisor;
27+
int r = dividend % divisor;
28+
permutation.append(sb.charAt(r));
29+
sb.deleteCharAt(r);
30+
dividend = q;
31+
}
32+
permutations.add(String.valueOf(permutation));
33+
}
34+
return permutations;
35+
}
36+
37+
public static int fact(int n) {
38+
if (n == 1) return n;
39+
return n * fact(n - 1);
40+
}
41+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /