#include
#include
#include
using namespace std;
class Solution {
public:
vector<int> maxInWindows(const vector<int>& num, unsigned int size);
bool hasPath(char* matrix, int rows, int cols, char* str);
bool hasPathcore(char* matrix, int rows, int cols, char* str, int row, int col, int &strindex, int* &isarrise);
int movingCount(int threshold, int rows, int cols);
int movingCountCore(int threshold, int rows, int cols, int row, int col, int* &isarrise);
bool issatisfydigits(int threshold, int row, int col);
int cutRope(int number);
};
vector<int> Solution::maxInWindows(const vector<int>& num, unsigned int size) {
int len = num.size();
if (len < size || size <= 0) return vector<int>();
vector<int> result;
deque<int> que;
for (int i = 0; i < size; i++) {
while (!que.empty() && num[que.back()] <= num[i]){
que.pop_back();
}
que.push_back(i);
}
for (int i = size; i < len; i++) {
result.push_back(num[que.front()]);
while (!que.empty() && num[que.back()] <= num[i]) {
que.pop_back();
}
que.push_back(i);
if (i - que.front() >= size)
que.pop_front();
}
result.push_back(num[que.front()]);
return result;
}
bool Solution::hasPath(char* matrix, int rows, int cols, char* str) {
if (!matrix || rows <= 0 || cols <= 0 || !str) return false;
bool result = false;
int *isarrise = new int[rows*cols]();
int strindex = 0;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (hasPathcore(matrix, rows, cols, str, row, col, strindex, isarrise)) return true;
}
}
delete[] isarrise;
return false;
}
bool Solution::hasPathcore(char* matrix, int rows, int cols, char* str, int row, int col, int &strindex, int* &isarrise) {
if (str[strindex] == '\0') return true;
bool result = false;
int index = row * cols + col;
if (row >=0 && col >= 0 && row < rows && col < cols && isarrise[index] == 0 && matrix[index] == str[strindex]) {
isarrise[index] = 1;
strindex++;
result = hasPathcore(matrix, rows, cols, str, row + 1, col, strindex, isarrise) || hasPathcore(matrix, rows, cols, str, row, col + 1, strindex, isarrise) ||
hasPathcore(matrix, rows, cols, str, row - 1, col, strindex, isarrise) || hasPathcore(matrix, rows, cols, str, row, col - 1, strindex, isarrise);
if (!result) {
isarrise[index] = 0;
strindex--;
}
}
return result;
}
int Solution::movingCount(int threshold, int rows, int cols) {
if (threshold < 0 || rows <= 0 || cols <= 0) return 0;
int* isarrise = new int[rows * cols]();
int result = movingCountCore(threshold, rows, cols, 0, 0, isarrise);
delete[] isarrise;
return result;
}
int Solution::movingCountCore(int threshold, int rows, int cols, int row, int col, int* &isarrise) {
int index = row * cols + col;
if (row >= rows || col >= cols || row < 0 || col < 0 || isarrise[index] == 1) return 0;
int result = 0;
if (issatisfydigits(threshold, row, col)) {
result++;
isarrise[index] = 1;
result += movingCountCore(threshold, rows, cols, row + 1, col, isarrise) + movingCountCore(threshold, rows, cols, row , col+1, isarrise)
+ movingCountCore(threshold, rows, cols, row - 1, col, isarrise) + movingCountCore(threshold, rows, cols, row, col-1, isarrise);
}
return result;
}
bool Solution::issatisfydigits(int threshold, int row, int col) {
int temp = 0;
while (row) {
temp += row % 10;
row /= 10;
}
while (col) {
temp += col % 10;
col /= 10;
}
if (temp <= threshold) return true;
else
return false;
}
int Solution::cutRope(int number) {
if (number <= 1) return 0;
if (number == 2) return 1;
if (number == 3) return 2;
int* result = new int[number];
result[0] = 1;
result[1] = 2;
result[2] = 3;
for (int i = 3; i < number; i++) {
int max;
if (i == number - 1) max = 0;
else
{
max = i;
}
for (int j = 0; j < (i/2 + 1); j++) {
int temp = result[j] * result[i - j - 1];
max = temp > max ? temp : max;
}
result[i] = max;
}
return result[number - 1];
}
void test1() {
const vector<int> num = { 2,3,4,2,6,2,5,1 };
int size = 3;
Solution s;
vector <int> result = s.maxInWindows(num, size);
for (vector<int>::iterator it = result.begin(); it != result.end(); it++)
cout << *it << ' ';
return;
}
void test2() {
char* matrix = "abcesfcsadee";
char* str = "bcced";
char* str_1 = "abcd";
int rows = 3;
int cols = 4;
Solution s;
cout << s.hasPath(matrix, rows, cols, str) << endl;
cout << s.hasPath(matrix, rows, cols, str_1);
return;
}
void test3() {
int threshold = 4;
int rows = 3;
int cols = 3;
Solution s;
cout << s.movingCount(threshold, rows, cols);
return;
}
void test4() {
int number = 8;
Solution s;
cout << s.cutRope(number);
return;
}
int main() {
test4();
system("pause");
return 0;
}