当前位置:网站首页>接水面试题
接水面试题
2022-06-26 17:23:00 【丹丹的小跟班】
一道有意思的面试题,记录下
一个数组[1,0,1],将他看做柱状图,那么就可以接水1,
一个数组[2,0,3],将他看做柱状图,那么就可以接水2。
求一个统计节水量的方法。
一.暴力法
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
}
}
二.暴力法简化版
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
}
}
三.双指针法
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
}
}
边栏推荐
- Cloud native 02: Alibaba cloud cloud efficient flow pipeline
- When I was in the library, I thought of the yuan sharing mode
- 对NFT市场前景的7个看法
- Uncover the secret of Agora lipsync Technology: driving portraits to simulate human speech through real-time voice
- Troubleshooting ideas that can solve 80% of faults!
- 丰富专业化产品线, 江铃福特领睿·极境版上市
- 【推荐系统学习】推荐系统架构
- Demonstrate to Xiaobai the case of sub database and sub table
- Leetcode HOT100 (22--- bracket generation)
- Play with Linux and easily install and configure MySQL
猜你喜欢
![[C language] static modifies local variables](/img/bf/9084d2e924c3e1e244568562a83d74.jpg)
[C language] static modifies local variables

Ndroid development from introduction to mastery Chapter 2: view and ViewGroup

Army chat -- registration of Registration Center

Leetcode 1170. Frequency of occurrence of the minimum letter of the comparison string (yes, solved)

Various types of gypsum PBR multi-channel mapping materials, please collect them quickly!

Fire evacuation and self rescue... This safety production and fire training is full!
![[ten thousand words summary] starting from the end, analyze in detail how to fill in the college entrance examination volunteers](/img/77/715454c8203d722e246ed70e1fe0d8.png)
[ten thousand words summary] starting from the end, analyze in detail how to fill in the college entrance examination volunteers

Uncover the secret of Agora lipsync Technology: driving portraits to simulate human speech through real-time voice

Which low code platform is more friendly to Xiaobai? Here comes the professional evaluation!

She said she was tired! So tired! I want to change my career
随机推荐
Treasure and niche CTA animation material website sharing
20: Chapter 3: develop the pass service: 3: get through the redis server in the program; (it only connects with the redis server and does not involve specific business development)
Necessary decorator mode for 3 years' work
Multiply the values of the upper triangular elements of the array by M
Cloud native 02: Alibaba cloud cloud efficient flow pipeline
Can Luo Yonghao succeed in entering the AR field this time?
10 cloud security best practices that enterprises need to know
Here comes the hero League full skin Downloader
vue--vuerouter缓存路由组件
MySQL index
Programmer interview guide - self introduction
7 views on NFT market prospect
Don't believe it, 98% of programmers are like this
[buuctf.reverse] 126-130
Web3去中心化存储生态图景
牛客网:设计LRU缓存结构 设计LFU缓存结构
Calculate the sum of the main diagonals of the array
[latex bearer] use tables in \title (error \begin doesn't match its definition.)
Ndroid development from introduction to mastery Chapter 2: view and ViewGroup
宝藏又小众的CTA动画素材素材网站分享