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 ...
随机推荐
- hdu 2255 奔小康赚大钱 KM算法
看到这么奇葩的题目名我笑了,后来这么一个裸的KM调了2小时我哭了…… 这是个裸的KM算法,也没什么多说的,主要是注意多组数据时,每次都要把各种数组清空啊,赋值啊什么的,反正比较麻烦.至于为什么调了2小 ...
- Mir2源码详解之服务端-登录网关(LoginGate)
传奇这款游戏,一直对我的影响很大.当年为了玩传奇,逃课,被老师叫过N次家长.言归正传,网上有很多源码,当然了,都是delphi的.并且很多源码还不全, 由于一直学习的c.c++.delphi还真不懂. ...
- linux中sudoers别名规则
/etc/sudoers 配置文档中别名规则 别名规则定义格式如下: Alias_Type NAME = item1, item2, ... 或 Alias_Type NAME = item1, it ...
- jquery easyui combobox
$("#select_Dic").combobox({ url: "http://www.cnblogs.com/Ajax/ ...
- 利用js+canvas实现的时钟效果图
canvas+js时钟特效 运用js+canvas方面的知识完成一个时钟的效果图,再利用for循环实现指针的转动效果: <!--网页文档的声明--> <!doctype html&g ...
- 实现QQ机器人报警
如题,废话不说,直接上代码.首先是登录QQ的小脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...
- psd图片到html
正确的做法是:拿到psd后,先不要做别的,直接在文本编辑器中将网页的框架写出来,不要假设这块将来css要去怎么渲染,完全自然化的标签,不加任何的css.写完之后在各个浏览器运行之后确保大体定位都没有问 ...
- C++文件操作(输入输出、格式控制、文件打开模式、测试流状态、二进制读写)
1.向文件写数据 头文件#include <ofstream> ①Create an instance of ofstream(创建ofstream实例) ②Open the file w ...
- ios 总结
1 ocoa Touch Layer{ App Extensions https://developer.apple.com/library/ios/documentation/General/Con ...
- 菜鸟学习Hibernate——持久层框架
一.Java操作数据库的阶段. Java对数据库进行操作经历了三个阶段. 1.1操作JDBC阶段 这个阶段就是利用JDBC类来操作数据库.这个阶段出现了两个问题: 代码过度重复:在每一次数据库操作的是 ...