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的更多相关文章

  1. Careercup - Microsoft面试题 - 6314866323226624

    2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...

  2. Careercup - Microsoft面试题 - 6366101810184192

    2014-05-10 22:30 题目链接 原题: Design database locks to allow r/w concurrency and data consistency. 题目:设计 ...

  3. Careercup - Microsoft面试题 - 24308662

    2014-05-12 07:31 题目链接 原题: I have heard this question many times in microsoft interviews. Given two a ...

  4. Careercup - Microsoft面试题 - 5700293077499904

    2014-05-12 00:02 题目链接 原题: For a given map (ie Bing map) given longitude/latitude/ how would you desi ...

  5. Careercup - Microsoft面试题 - 5204967652589568

    2014-05-11 23:57 题目链接 原题: identical balls. one ball measurements ........ dead easy. 题目:9个看起来一样的球,其中 ...

  6. Careercup - Microsoft面试题 - 5175246478901248

    2014-05-11 23:52 题目链接 原题: design an alarm clock for a deaf person. 题目:为聋人设计闹钟? 解法:聋人听不见,那么闪光.震动都可行.睡 ...

  7. Careercup - Microsoft面试题 - 5718181884723200

    2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...

  8. Careercup - Microsoft面试题 - 5173689888800768

    2014-05-11 05:21 题目链接 原题: Complexity of a function: int func_fibonacci ( int n) { ) { return n; } el ...

  9. Careercup - Microsoft面试题 - 6282862240202752

    2014-05-11 03:56 题目链接 原题: Given an integer array. Perform circular right shift by n. Give the best s ...

随机推荐

  1. Ubuntu16.04安装JDK

    转载请注明源出处:http://www.cnblogs.com/lighten/p/6105463.html 1.简单的安装方法 安装JDK的最简单方法应该就是使用apt-get来安装了,但是源一般是 ...

  2. VBA添加表格

    Sub 添加表格() ' If MsgBox("要为所有表格添加列吗?", vbYesNo + vbQuestion) = vbYes Then To ActiveDocument ...

  3. Log4net使用笔记

    Log4net使用笔记   编写人:CC阿爸 2013-10-29 近来在处理项目时候,想将系统的操作日志以文本的形式记录下来,方便对系统操作记录进行追踪. 经过在网上搜索部分解决方案,大致可以归纳如 ...

  4. pjax 历史管理 jQuery.History.js

    更新 http://www.bootcdn.cn/jquery.pjax/ 简介 pjax是一个jQuery插件,使用ajax和pushState技术提供快速的浏览体验与真正的永久链接.网页标题.以及 ...

  5. Redis源码研究--字典

    计划每天花1小时学习Redis 源码.在博客上做个记录. --------6月18日----------- redis的字典dict主要涉及几个数据结构, dictEntry:具体的k-v链表结点 d ...

  6. 基于jQuery编写的横向自适应幻灯片切换特效

    基于jQuery编写的横向自适应幻灯片切换特效 全屏自适应jquery焦点图切换特效,在IE6这个蛋疼的浏览器兼容性问题上得到了和谐,兼容IE6. 适用浏览器:IE6.IE7.IE8.360.Fire ...

  7. ng-summit and $watch() funciton

    <div ng-app> <form ng-submit="requestFunding()" ng-controller="StartUpContro ...

  8. UCOS2_STM32F1移植详细过程(一)

    Ⅰ.概述 该文写针对初学µC/OS的朋友,基于以下平台来一步一步移植µC/OS嵌入式操作系统.UCOS移植相关平台: 系统平台:µC/OS-II  (最新V2.92版) 硬件平台:STM32F1    ...

  9. linux之i2c子系统架构---总线驱动

    编写i2c设备驱动(从设备)一般有两种方式: 1.用户自己编写独立的从设备驱动,应用程序直接使用即可. 2.linux内核内部已经实现了一个通用的设备驱动,利用通用设备驱动编写一个应用程序(用户态驱动 ...

  10. Oracle Database Concepts:介绍模式对象(Introduction to Schema Objects)

    数据库模式(schema)是数据结构的逻辑容器,被称作模式对象(schema objects) 每一个数据库用户拥有一个和用户名相同的模式,例如hr用户拥有hr模式. 在一个产品数据库中,模式的拥有者 ...