Dynamic Programming
, Amazon OA
Description:
Given numeric string str
, the task is to count the number of ways the given string can be split, such that each segment is a prime number. Since the answer can be large, return the answer modulo 10^9 + 7
.
Note: A split that contains numbers with leading zeroes will be invalid and the initial string does not contain leading zeroes.
Solution:
public class CountPrimeStrings {
public int CountPrimeString(String str) {
int n = str.length();
boolean[] sieve = buildSieve();
// dp[i]: # of ways to split substring(0, i) into primes
long[] dp = new long[n + 1];
int mod = 1000000000 + 7;
dp[0] = 1L;
for (int i = 1; i <= n; i++) {
for (int j = i - 1; j >= 0; j--) {
String sub = str.substring(j, i);
if (sub.startsWith("0")) continue;
int num = Integer.parseInt(sub);
if (sieve[num]) dp[i] += dp[j];
dp[i] %= mod;
}
}
return (int)dp[n];
}
private boolean[] buildSieve() {
boolean[] sieve = new boolean[1000001];
sieve[0] = false;
sieve[1] = false;
for (int i = 0; i < sieve.length; i++) {
sieve[i] = isPrime(i);
}
return sieve;
}
private boolean isPrime(int num) {
if (num == 1) return false;
int i = 2;
while (i * i <= num) {
if (num % i == 0) {
return false;
}
i++;
}
return true;
}
}
Q1 Q2 BQ Group project比較OK Motivation What excites me? What is my motivation of doing that project. Good mo: excitation
Jan 9, 2022Description: You are given a rectangular cake of size h x w and two arrays of integers horizontalCuts and verticalCuts where: horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, and verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut. Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a large number, return this modulo 10^9 + 7. Solution:
Nov 23, 2021Description: A newly designed keypad was tested, where a tester pressed a sequence of n keys, one at a time. You are given a string keysPressed of length n, where keysPressed[i] was the ith key pressed in the testing sequence, and a sorted list releaseTimes, where releaseTimes[i] was the time the ith key was released. Both arrays are 0-indexed. The 0th key was pressed at the time 0, and every subsequent key was pressed at the exact time the previous key was released. The tester wants to know the key of the keypress that had the longest duration. The ith keypress had a duration of releaseTimes[i] - releaseTimes[i - 1], and the 0th keypress had a duration of releaseTimes[0]. Note that the same key could have been pressed multiple times during the test, and these multiple presses of the same key may not have had the same duration. Return the key of the keypress that had the longest duration. If there are multiple such keypresses, return the lexicographically largest key of the keypresses.
Nov 22, 2021or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up