Careercup - Microsoft面试题 - 5752271719628800
2014-05-10 20:31
原题:
Given an array of integers and a length L, find a sub-array of length L such that the products of all integers are the biggest.
Example:
Input: {, , -, -, },
Output: {-,-,}
题目:给定一个整数数组,其中包含正数、负数或者0。请找出乘积最大的子数组(子数组是连续的,子序列可以不连续)。不过,此处限制子数组的长度为L。
解法:因为限制了子数组的长度为L,问题解法瞬间就变了。如果没有长度限制,这应该是个比“最大子数组和”更难的动态规划。限制了长度为L以后,我们可以直接顺序计算每个长度为L的子数组的乘积。这种算法可以在线性时间内完成。不过缺点也是显而易见的:连乘一堆整数,很容易就溢出了。暂时还没想出能够不计算乘积就得出结果的办法,等我想到了再来更新这篇解题报告吧。
代码:
// http://www.careercup.com/question?id=5752271719628800
#include <climits>
#include <iostream>
#include <vector>
using namespace std; int maxSubarrayProduct(vector<int> &v, int k)
{
int n = (int)v.size(); if (n == ) {
return -;
}
if (k >= n) {
return ;
} int i, ll;
long long int product;
long long int max_product = INT_MIN; int j;
i = ;
while (i + k <= n) {
product = ;
for (j = i; j < i + k; ++j) {
if (v[j] == ) {
product = ;
break;
} else {
product *= v[j];
}
}
if (product > max_product) {
max_product = product;
ll = i;
}
if (j < i + k) {
i = j + ;
} else {
++i;
while (i + k <= n && v[i + k - ] != ) {
product = product / v[i - ] * v[i + k - ];
if (product > max_product) {
max_product = product;
ll = i;
}
++i;
}
if (i + k <= n) {
if (max_product < ) {
max_product = ;
ll = i;
}
i = i + k;
}
}
} return ll;
} int main()
{
int i;
int n;
int k;
int ll;
vector<int> v; while (cin >> n && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
cin >> v[i];
}
cin >> k;
k = k < n ? k : n;
ll = maxSubarrayProduct(v, k);
cout << '{';
for (i = ; i < k; ++i) {
i ? (cout << ' '), : ;
cout << v[i + ll];
}
cout << '}' << endl;
v.clear();
} return ;
}
Careercup - Microsoft面试题 - 5752271719628800的更多相关文章
- Careercup - Microsoft面试题 - 6314866323226624
2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...
- Careercup - Microsoft面试题 - 6366101810184192
2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...
- Careercup - Microsoft面试题 - 24308662
2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...
- Careercup - Microsoft面试题 - 5700293077499904
2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...
- Careercup - Microsoft面试题 - 5204967652589568
2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...
- Careercup - Microsoft面试题 - 5175246478901248
2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...
- Careercup - Microsoft面试题 - 5718181884723200
2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...
- Careercup - Microsoft面试题 - 5173689888800768
2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...
- Careercup - Microsoft面试题 - 6282862240202752
2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...
随机推荐
- Transaction 'IREG', Abend 'APCT', at '????'.
应用的问题: Transactions can fail with an APCT abend, when there is a failure in a transaction attempting ...
- 设置peoplecode trace
Configuring PeopleCode Trace Select PeopleTools, Utilities, Debug, Trace PeopleCode to access the Tr ...
- HUE 忘记密码
解决方法: 启动HUE的Shell /opt/cloudera/parcels/CDH/lib/hue/build/env/bin/hue shell from django.contrib.auth ...
- GoLang安装
GoLang的官网被墙,镜像下载地址:http://tip.golang.so/dl/ 或者 http://golang.so/dl/ 安装说明:http://tip.golang.so/doc/i ...
- Decorator模式
Decorator模式能够像标准的继承一样为类添加新的功能. 不同于标准继承机制的是,如果对象进行了实例化,Decorator模式能够在运行时动态地为对象添加新的功能. <?php abstra ...
- JS中cookie的基本使用
cookie是本身是HTML中ducument中的一个属性,可以用来保存一些简单的数据信息,比如用户名.密码等,提高一些网站的用户体验度.下面就来简单的说说cookie,它有下面几个特性: 1.有过期 ...
- 用C#写的读写CSV文件
用C#写的读取CSV文件的源代码 CSV文件的格子中包含逗号,引号,换行等,都能轻松读取,而且可以把数据转化成DATATABLE格式 using System; using System.Text; ...
- 将python2.7+django1.10部署到SAE上
首先我想说的是我为什么选择SAE呢?本人学生一枚,没钱.然后sae好像又有免费的一定限额,所以我就选了它. 期间曲折颇多,实在不是三言两语所能道情的.各种百度,谷歌,最后所幸成功了,幸哉! 主要参考了 ...
- DataSet中的relation
DataSet中的relation DataSet是ADO.Net中相当重要的数据访问模型.有一个很大的优点是可以记录多个表之间的关系.有点类似与数据库的外键. 在DataSet中也可以定义类似的关系 ...
- python入门总结-函数
函数形式: def functionname(paramlist): function body 局部变量不改变实参的值,如果需要改变,声明global.比如,global x 可以给函数默认值,注意 ...