水池问题的lua语言算法(面试题分析:我的Twitter技术面试失败了)
twitter面试题内容
“看下面这个图片”

“在这个图片里我们有不同高度的墙。这个图片由一个整数数组所代表,数组中每个数是墙的高度。上边的图可以表示为数组[2,5,1,2,3,4,7,7,6]”
“假如开始下雨了,那么墙之间的水坑能够装多少水呢?”
闲来无事给出一份解决此问题的lua代码(https://gist.github.com/jianchenglee/7262518):
-- author ljc
-- 1) find the max value, split the array to 2 array
-- 2) compute the increment ,get 2 increment array
-- 3) get the result by the increment value(if there are a minus value,then retain water,until one by one accumulation value become plus,then go on next)
waterpool={2,4,3,5,1,2,3,2,3,7,7,6,7,6,4,5}
testcase_1 = {2,5,1,2,3,4,7,7,6}
testcase_2 = {2,5,1,3,1,2,1,7,7,6}
testcase_3 = {6,1,4,6,7,5,1,6,4}
function watertotal(o)
-- v1:tempvalue total: result value
-- v_max,i_max: the biggest value and the index of the input array
-- waterpool_q1: first half increment value array waterpool_q2: secopd half increment value array
local v1,v_max,i_max,total=0,0,0,0,0;
local waterpool_q1={};
local waterpool_q2={};
local allnumber=table.maxn(o)
--1)
for i,v in ipairs(o) do
if v>v_max then
v_max=v
i_max=i
end
end
--2)
-- generate waterpool_q1
for i=1,i_max-1,1 do
--print(o[i]-v1)
table.insert(waterpool_q1,o[i]-v1)
v1=o[i]
end
-- generate waterpool_q2
v1=0
for i=allnumber,i_max,-1 do
--print(o[i]-v1)
table.insert(waterpool_q2,o[i]-v1)
v1=o[i]
end
--3)
-- the result of waterpool_q1
v1=0
for i,v in ipairs(waterpool_q1) do
if v<0 and v1>=0 then
v1=v
total=total+math.abs(v1)
elseif (v1+v<0) then
v1=v1+v
total=total+math.abs(v1)
elseif v+v1>=0 then
v1=0
end
--print(math.abs(v1))
end
-- the result of waterpool_q1
v1=0
for i,v in ipairs(waterpool_q2) do
if v<0 and v1>=0 then
v1=v
total=total+math.abs(v1)
elseif (v1+v<0) then
v1=v1+v
total=total+math.abs(v1)
elseif v+v1>=0 then
v1=0
end
--print(math.abs(v1))
end
print(total)
end
-- test the function
watertotal(waterpool)
watertotal(testcase_1)
watertotal(testcase_2)
watertotal(testcase_3)
测试结果:
>lua -e "io.stdout:setvbuf 'no'" "waterpool.lua"
17
10
17
13
水池问题的lua语言算法(面试题分析:我的Twitter技术面试失败了)的更多相关文章
- 【转载】经典10道c/c++语言经典笔试题(含全部所有参考答案)
经典10道c/c++语言经典笔试题(含全部所有参考答案) 1. 下面这段代码的输出是多少(在32位机上). char *p; char *q[20]; char *m[20][20]; int (*n ...
- Lua语言中文手册 转载自网络
Programming in LuaCopyright ® 2005, Translation Team, www.luachina.net Programming in LuaProgramming ...
- C语言编程规范试题(标准答案)
C语言编程规范试题(标准答案) 一.单选题(每小题3分,共20小题60分) 1.1-1.5 B D A C B 1.6-1.10 C A D B C 1.11 ...
- C语言编程规范试题
C语言编程规范试题 [说明]: 1.本试题中不考虑头文件引用问题(假定已经包含正确的头文件),C语言的标准函数都可用: 2.如果不特别说明,假定程序运行环境为:操作系统Windows 2000, VC ...
- 用VC编译lua源码,生成lua语言的解释器和编译器
用VC编译lua源码,生成lua语言的解释器和编译器 1.去网址下载源码 http://www.lua.org/download.html 2.装一个VC++,我用的是VC6.0 3.接下来我们开始编 ...
- 51CTO专访淘宝清无:漫谈Nginx服务器与Lua语言
http://os.51cto.com/art/201112/307610.htm 说到Web服务器,也许你第一时间会想到Apache,也许你会想到Nginx.虽然说Apache依然是Web服务器的老 ...
- 【quick-cocos2d-x】Lua 语言基础
版权声明:本文为博主原创文章,转载请注明出处. 使用quick-x开发游戏有两年时间了,quick-x是cocos2d-Lua的一个豪华升级版的框架,使用Lua编程.相比于C++,lua的开发确实快速 ...
- lua语言入门之Sublime Text设置lua的Build System
转自: http://blog.csdn.net/wangbin_jxust/article/details/8911956 最近开始学习LUA语言,使用Sublime Text作为编辑器,不得不说, ...
- 编译并使用Lua语言
Lua是一个小巧的脚本语言,该语言设计的目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能. 可扩展性.Lua的扩展性非常卓越,可以通过Lua代码或C代码扩展,很多功能可以通过外部库来扩 ...
随机推荐
- Pandas基本介绍
1.pandas主要的两个数据结构:Series和DataFrame Series的字符串表现形式为:索引在左边,值在右边.由于我们没有为数据指定索引.于是会自动创建一个0到N-1(N为长度)的整数型 ...
- codeforces round#509
博主水平不高, 只能打完$4$题, QAQ什么时候才能变强啊嘤嘤嘤 订正完6题了, 还想打今天下午的CF , 只能迟十分钟了, 掉分预定 A. Heist 输出 $max - min + n - 1 ...
- 07. pt-fifo-split
iostat -dxm 1 42 1>iostat.log 2>&1 ---------------------------------------- #!/bin/bash of ...
- Java 线程池 Executor 框架
在Java中,可以通过new Thread 的方法来创建一个新的线程执行任务,但是线程的创建是非常耗时的,而且创建出来的新的线程都各自运行.缺乏统一的管理,这样的后果是可能导致创建过多的线程从而过度消 ...
- JDK 之资源文件管理
JDK 之资源文件管理 JDK 规范目录(https://www.cnblogs.com/binarylei/p/10200503.html) 一.文件资源 user.home 用户目录,如 Linu ...
- nginx记录post数据日志
1.vi nginx.conf 找到http {}中log_foramt ,定义post 日志格式 #log_format main '$remote_addr - $remote_user [$ti ...
- 52ABP视频学习
https://study.163.com/course/courseMain.htm?courseId=1005208064 网易视频 https://www.52abp.com/ReadWiki/ ...
- 2017/2/8 hibernate + oracle 实现id的自增 同时 hibernate项目跑起来 会自己增加字段的原因 oracle触发器的使用
hibernate + oracle 实现id的自增 1.在oracle中先创建一个序列 : 序列语法 如下 create sequence (序列名称)seq_student_id minva ...
- 【搜索】 Find The Multiple
#include<stdio.h> #include<stdlib.h> #include<string.h> bool found; void DFS(unsig ...
- kbmmw 5.02发布
5.02.00 May 27 2017 Important notes (changes that may break existing code) ========================= ...