Skip to content

Commit cd3a03d

Browse files
committed
Update
1 parent 46adbba commit cd3a03d

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package dynamic_programming;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/domino-and-tromino-tiling
5+
* Difficulty: Medium
6+
* Time complexity: O(n)
7+
* Space complexity: O(n)
8+
*/
9+
public class DominoAndTrominoTiling {
10+
11+
// since the answer may be too large, return it modulo 10^9 + 7
12+
private static final int MOD = 1_000_000_007;
13+
14+
public int numTilings(int n) {
15+
if (n <= 2) return n;
16+
17+
// number of ways to fully cover a 2xN board
18+
long[] fullCoverage = new long[n + 1];
19+
fullCoverage[1] = 1L;
20+
fullCoverage[2] = 2L;
21+
22+
// number of ways to partially cover (one cell not being covered) a 2xN board
23+
long[] partialCoverage = new long[n + 1];
24+
partialCoverage[2] = 1L;
25+
26+
for (int i = 3; i <= n; i++) {
27+
fullCoverage[i] = (fullCoverage[i - 1] + fullCoverage[i - 2] + 2 * partialCoverage[i - 1]) % MOD;
28+
partialCoverage[i] = (partialCoverage[i - 1] + fullCoverage[i - 2]) % MOD;
29+
}
30+
31+
return (int) fullCoverage[n];
32+
}
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package dynamic_programming;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/longest-arithmetic-subsequence
8+
* Difficulty: Medium
9+
* Time complexity: O(n^2)
10+
* Space complexity: O(n^2)
11+
*/
12+
public class LongestArithmeticSubsequence {
13+
14+
public int longestArithSeqLength(int[] nums) {
15+
Map<Integer, Integer>[] dp = new HashMap[nums.length];
16+
int longest = 0;
17+
for (int right = 0; right < nums.length; right++) {
18+
dp[right] = new HashMap<>();
19+
for (int left = 0; left < right; left++) {
20+
int diff = nums[right] - nums[left];
21+
dp[right].put(diff, dp[left].getOrDefault(diff, 1) + 1);
22+
longest = Math.max(longest, dp[right].get(diff));
23+
}
24+
}
25+
26+
return longest;
27+
}
28+
}

src/math/FactorialTrailingZeroes.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package math;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/factorial-trailing-zeroes
5+
* Difficulty: Medium
6+
* Time complexity: O(log n)
7+
* Space complexity: O(1)
8+
*/
9+
public class FactorialTrailingZeroes {
10+
11+
public int trailingZeroes(int n) {
12+
int zeroes = 0;
13+
while (n > 0) {
14+
// every 5 results in a zero because there will always be a corresponding 2
15+
// -> we need to count the number of factors of 5
16+
// e.g. 1 * 2 * 3 * (4) * [5] * 6 * 7 * 9 * 9 * [10] * 11 * 12 * 13 * 14 * [15] * (16) * 17 * 18 * 19 * [20] * 21 * 22 * 23 * 24 * [25]
17+
// 4 * 16 = 2 * 2 * 2 * 2 * 2 * 2
18+
// 5 * 10 * 15 * 20 * 25 = 5 * 5 * 5 * 5 * 5 * 5 * (2 * 3 * 4)
19+
// -> 25! = X000000
20+
n /= 5;
21+
zeroes += n;
22+
}
23+
24+
return zeroes;
25+
}
26+
}

src/math/IntegerToRoman.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package math;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/integer-to-roman
5+
* Difficulty: Medium
6+
* Time complexity: O(1)
7+
* Space complexity: O(1)
8+
*/
9+
public class IntegerToRoman {
10+
11+
private static final int[] VALUES =
12+
new int[]{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
13+
private static final String[] SYMBOLS =
14+
new String[]{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
15+
16+
public String intToRomanViaLoop(int num) {
17+
StringBuilder roman = new StringBuilder();
18+
for (int i = 0; i < VALUES.length; i++) {
19+
while (VALUES[i] <= num) {
20+
roman.append(SYMBOLS[i]);
21+
num -= VALUES[i];
22+
}
23+
}
24+
25+
return roman.toString();
26+
}
27+
28+
private static final String[] THOUSANDS = new String[]{"", "M", "MM", "MMM"};
29+
private static final String[] HUNDREDS = new String[]{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
30+
private static final String[] TENS = new String[]{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
31+
private static final String[] ONES = new String[]{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
32+
33+
public String intToRomanViaMod(int num) {
34+
return THOUSANDS[num / 1000]
35+
+ HUNDREDS[(num % 1000) / 100]
36+
+ TENS[(num % 100) / 10]
37+
+ ONES[num % 10];
38+
}
39+
}

src/math/SqrtX.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package math;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/sqrtx
5+
* Difficulty: Easy
6+
* Time complexity: O(log n)
7+
* Space complexity: O(1)
8+
*/
9+
public class SqrtX {
10+
11+
public int mySqrt(int x) {
12+
if (x < 2) return x;
13+
14+
// 0 < sqrt(x) < x / 2
15+
int left = 2;
16+
int right = x / 2;
17+
18+
while (left <= right) {
19+
int mid = left + (right - left) / 2;
20+
if (mid > x / mid) {
21+
right = mid - 1;
22+
} else if (mid < x / mid) {
23+
left = mid + 1;
24+
} else {
25+
// only works if sqrt(x) is an integer
26+
return mid;
27+
}
28+
}
29+
30+
// we reached the BS out condition
31+
// -> left > right
32+
// -> right is the closest value to sqrt(x) rounded down
33+
return right;
34+
}
35+
}

0 commit comments

Comments
 (0)