有一个长为n的数组A,求满足0≤a≤b<n的A[b]-A[a]的最大值。
给定数组A及它的大小n,请返回最大差值。
测试样例:
[10,5],2
返回:0
class LongestDistance {
public:
int getDis(vector<int> A, int n) {//时间复杂度O(n) 空间复杂度O(1)
// write code here
int maxDiff=0;//初始化最大差值
int minNum=A[0];//初始化最小值
for(int i=1;i<n;++i){//遍历
if(A[i]<minNum)minNum=A[i];//更新最小值
if(A[i]-minNum>maxDiff)maxDiff=A[i]-minNum;//更新最大差值
}
return maxDiff;
}
};
题目描述
在4x4的棋盘上摆满了黑白棋子,黑白两色的位置和数目随机其中左上角坐标为(1,1),右下角坐标为(4,4),现在依次有一些翻转操作,要对一些给定支点坐标为中心的上下左右四个棋子的颜色进行翻转,请计算出翻转后的棋盘颜色。
给定两个数组A和f,分别为初始棋盘和翻转位置。其中翻转位置共有3个。请返回翻转后的棋盘。
测试样例:
[[0,0,1,1],[1,0,1,0],[0,1,1,0],[0,0,1,0]],[[2,2],[3,3],[4,4]]
返回:[[0,1,1,1],[0,0,1,0],[0,1,1,0],[0,0,1,0]] //把支点坐标上下左右位置上的数取反即可
class Flip {
public:
vector<vector<int> > flipChess(vector<vector<int> > A, vector<vector<int> > f) {
// write code here
int row;
int col;
for(int i=0;i<3;++i)
{
row=f[i][0]-1;
col=f[i][1]-1;
if(row-1>=0)
{
A[row-1][col]=(!A[row-1][col]);
}
if(row+1<4)
{
A[row+1][col]=(!A[row+1][col]);
}
if(col-1>=0)
{
A[row][col-1]=(!A[row][col-1]);
}
if(col+1<4)
{
A[row][col+1]=(!A[row][col+1]);
}
}
return A;
}
};
题目描述
现在有一个城市销售经理,需要从公司出发,去拜访市内的商家,已知他的位置以及商家的位置,但是由于城市道路交通的原因,他只能在左右中选择一个方向,在上下中选择一个方向,现在问他有多少种方案到达商家地址。
给定一个地图map及它的长宽n和m,其中1代表经理位置,2代表商家位置,-1代表不能经过的地区,0代表可以经过的地区,请返回方案数,保证一定存在合法路径。保证矩阵的长宽都小于等于10。
测试样例:
[[0,1,0],[2,0,0]],2,3
返回:2
//思路:动态规划
class Visit {
public:
int countPath(vector<vector<int> > map, int n, int m) {
// write code here
// write code here
int x1 = -1,y1 = -1;//经理的坐标
int x2 = -1,y2 = -1;//商家的坐标
for(int i = 0;i<n;i++){
for(int j = 0; j<m;j++){
if(map[i][j]==1){
x1 = j;
y1 = i;
}else if(map[i][j]==2){
x2 = j;
y2 = i;
}
}
}
int xto = x1>x2?-1:1;//根据经理和商家的方向判断向左还是向右走
int yto = y1>y2?-1:1;//向上还是向下
//动态规划的思想 map[y][x]记录着经理到x,y点最多的路程数
for(int y = y1;y!=(y2+yto);y+=yto){
for(int x = x1;x!=(x2+xto);x+=xto){
if(y==y1||x==x1){
map[y][x] = 1;
continue;
}
map[y][x] = map[y-yto][x]+map[y][x-xto];
}
}
return map[y2][x2];
}
};
题目描述
有一个直方图,用一个整数数组表示,其中每列的宽度为1,求所给直方图包含的最大矩形面积。比如,对于直方图[2,7,9,4],它所包含的最大矩形的面积为14(即[7,9]包涵的7x2的矩形)。
给定一个直方图A及它的总宽度n,请返回最大矩形面积。保证直方图宽度小于等于500。保证结果在int范围内。
测试样例:
[2,7,9,4,1],5
返回:14 //动态规划
法一:
class MaxInnerRec {
public:
int countArea(vector<int> A, int n) {
int max=0,mm,nn;
for(int i=0;i<n;i++){
mm=i,nn=i;
for(int j=i-1;j>=0&&A[j]>=A[i];j--,mm--);
for(int j=i+1;j<n&&A[j]>=A[i];j++,nn++);
if(max<A[i]*(nn-mm+1))
max=A[i]*(nn-mm+1);
}
return max;
}
};
法二:
class MaxInnerRec {
public:
int countArea(vector<int> A, int n) {
// write code here
int max=0;
int* array=new int[A.size()]; //从i开始,记录i~n中的最小值。
for (int i = 0; i < A.size(); i++) {
for (int j = i; j < A.size(); j++)
{
if (j==i)
{
array[j]=A[i];
max=max>(A[j])?max:(A[j]);
continue;
}
array[i]=array[i]<A[j]?array[i]:A[j];
max=max>(array[i]*(j-i+1))?max:(array[i]*(j-i+1));
}
}
return max;
delete[] array;
}
};
题目描述
求字典序在s1和s2之间的,长度在len1到len2的字符串的个数,结果mod 1000007。
输入描述:
每组数据包涵s1(长度小于100),s2(长度小于100),len1(小于100000),len2(大于len1,小于100000)
输出描述:
输出答案。
输入例子:
ab ce 1 2
输出例子:
56
//求解大于str1的字符串个数以及大于str2的字符串个数,然后两者相减就能得到处于str1和str2之间的字符串个数
//对于求解长度len在[len1,len2]之间,且字典序大于某个字符串(str)的字符串个数:
//顺序遍历(i=0:n-1)str的每个字符str[i],则若一个字符串destr大于str,则有两种情况:
//(1)destr第i个字符大于str[i],则之后的字符无论是什么,destr都大于str
//(2)destr第i个字符等于str[i],则i++,并继续讨论后续字符
//最后如果len>strLen,需要考虑destr前strLen位和str完全一样,则剩余位置字符可以是任意字符。
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int getcount(char str[], int strLen, int len1, int len2){
int count = 0;
for(int len = len1; len <= len2; len++){
for(int i = 0; i < strLen && i < len; i++)
count += (26 - (str[i] - 'a' +1)) * pow(26,len - i - 1);
if(len > strLen){
count += pow(26,len - strLen);
}
}
return count;
}
int main(){
char str1[120];
char str2[120];
memset(str1,0,sizeof(str1));
memset(str2,0,sizeof(str2));
int len1, len2;
while(cin >> str1 >> str2 >> len1 >> len2){
int strlen1 = strlen(str1);
int strlen2 = strlen(str2);
int count1 = getcount(str1,strlen1,len1,len2);
int count2 = getcount(str2,strlen2,len1,len2);
int count = count1 - count2 - 1;
cout << count << endl;
}
return 0;
}
转载于:https://blog.51cto.com/10541556/1851183