当前位置:网站首页>Connected to surface test questions
Connected to surface test questions
2022-06-26 17:55:00 【Dandan's servant】
An interesting interview question , Record
An array [1,0,1], Think of him as a bar chart , Then you can get water 1,
An array [2,0,3], Think of him as a bar chart , Then you can get water 2.
Find a statistical method of water saving .
One . Violence law
function demo(arr=[]) {
if(Array.isArray(arr) && arr.length > 2) {
let sum = 0
let len = arr.length
for(let i = 1; i < len - 1; i ++) {
let cur = arr[i]
let leftMax = 0
let rightMax = 0
for(let j = i; j < len; j ++) {
rightMax = Math.max(arr[j], rightMax)
}
for(let j = i; j >= 0; j --) {
leftMax = Math.max(arr[j], leftMax)
}
sum += (Math.min(leftMax, rightMax) - cur)
}
return sum
}else {
return 0
}
}
Two . Violence act simplified
function demo(arr=[]) {
if(Array.isArray(arr) && arr.length > 2) {
let sum = 0
let len = arr.length
for(let i = 1; i < len - 1; i ++) {
let cur = arr[i]
let leftMax = Math.max(...arr.slice(0, i + 1))
let rightMax = Math.max(...arr.slice(i))
sum += (Math.min(rightMax, leftMax) - cur)
}
return sum
}else {
return 0
}
}
3、 ... and . Double finger needling
function demo(arr = []) {
if (Array.isArray(arr) && arr.length > 2) {
let [sum, len] = [0, arr.length]
let [leftIndex, rightIndex] = [0, len - 1]
let [leftMax, rightMax] = [arr[leftIndex], arr[rightIndex]]
while (leftIndex < rightIndex) {
leftMax = Math.max(leftMax, arr[leftIndex])
rightMax = Math.max(rightMax, arr[rightIndex])
if (leftMax < rightMax) {
sum += (leftMax - arr[leftIndex])
leftIndex++
} else {
sum += (rightMax - arr[rightIndex])
rightIndex--
}
}
return sum
} else {
return 0
}
}
边栏推荐
- RuntimeError: CUDA error: out of memory自己的解决方法(情况比较特殊估计对大部分人不适用)
- Microservice architecture practice: user login and account switching design, order query design of the mall
- 让torch.cuda.is_available()从false变成true的一点经验
- Using redis for user access data statistics hyperloglog and bitmap advanced data types
- 牛客网:设计LRU缓存结构 设计LFU缓存结构
- ZCMU--1367: Data Structure
- 贝叶斯网络详解
- 国信证券怎么开户?通过链接办理股票开户安全吗
- transforms. The input of randomcrop() can only be PIL image, not tensor
- Vscode usage - Remote SSH configuration description
猜你喜欢

二分查找法-1

Rich professional product lines, and Jiangling Ford Lingrui · Jijing version is listed

VSCode使用 - Remote-SSH 配置说明

sql中ROUND和TRUNCATE的区别(四舍五入还是截取小数点后几位)

Decision tree and random forest

关于FlowUs这一款国民好笔记

next(iter(dataloader))的一点点体会

解决pycharm里面每个字母占一格空格的问题

Lm06 the mystery of constructing the bottom and top trading strategy only by trading volume

让torch.cuda.is_available()从false变成true的一点经验
随机推荐
用redis做用户访问数据统计HyperLogLog及Bitmap高级数据类型
Applet setting button sharing function
RSA概念详解及工具推荐大全 - lmn
#26class中get和set设置
Which low code platform is more friendly to Xiaobai? Here comes the professional evaluation!
[buuctf.reverse] 126-130
【万字总结】以终为始,详细分析高考志愿该怎么填
VSCode使用 - Remote-SSH 配置说明
Uncover the secret of Agora lipsync Technology: driving portraits to simulate human speech through real-time voice
[dynamic planning] Jianzhi offer II 091 Paint the house
Dos et détails de la méthode d'attaque
10 cloud security best practices that enterprises need to know
Notes on flowus
贝叶斯网络详解
Plt How to keep show() not closed
#25class的类继承
Leetcode topic [array] -283- move zero
深层次安全定义剖析及加密技术
next(iter(dataloader))的一点点体会
解决pycharm里面每个字母占一格空格的问题