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 ...
随机推荐
- 建立交叉编译环境(arm-linux-gcc)
linux系统内核版本:2.6.32-358.el6.x86_64(在64位系统上安装32位程序需要另外安装一些库) arm-linux-gcc版本:本文安装的是友善之臂tiny6410光盘中自带的a ...
- 鼠标HOVER时区块动画旋转变色的CSS3样式掩码
鼠标hover时区块动画旋转变色的css3样式掩码<!DOCTYPE html> <html> <head> <meta charset="utf- ...
- 有趣的EditView为空时的抖动效果(用户名和密码)--第三方开源--ClearEditText
ClearEditText在github上的链接地址是:https://github.com/zhangphil/ClearEditText 用法十分简单,在布局中使用ClearEditText,在J ...
- CentOS6.4安装VNC
http://jingyan.baidu.com/article/ca2d939dd1dabbeb6c31ce24.html 一.安装 VNC 默认情况下,CentOS 6.4 是没有安装的. 检查是 ...
- js实现touch移动触屏滑动事件
在开始描述touch事件之前,需要先描述一下多触式系统中特有的touch对象(android和iOS乃至nokia最新的meego系统都模拟了类 似的对象).这个对象封装一次屏幕触摸,一般来自于手指. ...
- IOS学习3
@property属性使用 copy:NSString strong: 一般对象 weak: UI空间 assign:基本数据类型 retain: (对象,先上述类型使用) id 万能指针. id缺点 ...
- 一月份实现Adb小程序
As Brian said: According to a post on xda-developers, you can enable ADB over WiFi from the device w ...
- U6会计科目导入致对账不平
一个客户,用的是U6版本,想将己使用的账套的科目导入新建的账套中,本想用用友本身自带的总账工具导入,发现导不了.没办法,只能打开数据库,手工导入. 月末结账时,发现对账不平.问题是余额表与明细不平,大 ...
- 下一代NoSQL:最终一致性的末日
相比关系型数据库,NoSQL解决方案提供了shared-nothing.容错和可扩展的分布式架构等特性,同时也放弃了关系型数据库的强数据一致性和隔离性,美其名曰:"最终一致性". ...
- linux设备驱动层次
USB 采用树形拓扑结构,主机侧和设备侧的USB 控制器分别称为主机控制器(HostController)和USB 设备控制器(UDC),每条总线上只有一个主机控制器,负责协调主机和设备间的通信,而设 ...