class Solution {
public boolean validMountainArray(int[] arr) {
int l = 0;
int r = arr.length-1;
while (l<r && arr[l+1] > arr[l]){
l++;
}
while (r>0 && arr[r-1] > arr[r]){
r--;
}
return l==r && l!=0 && l != arr.length-1;
}
}
class Solution {
public int numWaterBottles(int numBottles, int numExchange) {
int ans = numBottles;
int left = 0;
while (numBottles >= numExchange){
left = numBottles%numExchange;
numBottles /= numExchange;
ans += numBottles;
numBottles += left;
}
return ans;
}
}
class Solution {
int mod = 1337;
public int superPow(int a, int[] b) {
int ans = 1;
for (int i = b.length - 1; i >= 0; i--) {
ans *= pow(a,b[i]);
ans %= mod;
a = pow(a,10);
}
return ans;
}
public int pow(int a, int b){
if (b==0) return 1;
if (b==1) return a%mod;
int ans = pow(a,b/2);
return ((ans * ans)%mod) * pow(a,b%2) % mod;
}
}
class Solution {
public int findRadius(int[] houses, int[] heaters) {
Arrays.sort(heaters);
int ans = 0;
for (int house : houses) {
int l = 0;
int r = heaters.length-1;
int tmp = (int) 1e9+1;
while (l<=r){
int mid = (l + r)/2;
//怕遗漏边缘情况不好处理就每次二分的结果都比较下
tmp = Math.min(tmp, Math.abs(heaters[mid] - house));
if (heaters[mid] > house){
r = mid-1;
}else if (heaters[mid] < house){
l = mid+1;
}else {
break;
}
}
ans = Math.max(ans,tmp);
}
return ans;
}
}
class Solution {
public int countOrders(int n) {
long ans = 1;
long mod = (long) (1e9+7);
for (int i = 2; i <= n; i++) {
ans *= (2 * i * i - i);
ans %= mod;
}
return (int)ans;
}
}
class Solution {
/**
* dp[i][target] = dp[i-1][target-1] + .... + dp[i-1][target-k]
*/
public int numRollsToTarget(int n, int k, int target) {
int mod = (int) 1e9 + 7;
int[][] dp = new int[n][target+1];
for (int i = 1; i <= k && i <= target; i++) {
dp[0][i] = 1;
}
for (int row = 1; row < n; row++) {
for (int col = row+1; col <= target; col++) {
for (int i = 1; i <= k; i++) {
if (i>col){
continue;
}
dp[row][col] += dp[row-1][col-i];
dp[row][col] %= mod;
}
}
}
return dp[n-1][target];
}
}