sort algorithm

Interesting problems about sorting algorithm intro problem - Leetcode 215: Kth Largest Element in an Array Given an integer array nums and an integer k, return the kth largest element in the array. Note that it is the kth largest element in the sorted order, not the kth distinct element. Can you solve it without sorting? Example 1: Input: nums = [3,2,1,5,6,4], k = 2 Output: 5 Example 2: Input: nums = [3,2,3,1,2,4,5,5,6], k = 4 Output: 4...

April 26, 2024 · 9 min · 1711 words · Me

trapping rain water

leetcode 42. trapping rain water 力扣题目链接 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 输入:height = [0,1,0,2,1,0,1,3,2,1,2,1] 输出:6 解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 思路: 盛水的地块,一定是 min (左边最高的, 右边最高)&& (当前比左右两边都地),那如何找到左右最高的部分,就成为了关键解题思路。 left ,right 表示移动的指针,需要计算当前 位置能盛水的量。 leftMax, rightMax 表示当前左边,右边最高的位置,(left ,right) 一定是在 (leftMax, rightMax) 范围内。 每次移动时,小的往里面移动,大的不动, 如果移动之后,新小的比较之前小的大了,那么更新对应max标志位,这首也不需要计算水位。 如果移动之后,新小的比较之前小的小了,那么不需要更新max标志位,但需要计算当前坑位的水位,并加入 结果中 The approach involves identifying the land that can contain water, which is contingent upon the minimum height between the tallest barrier to the left and the tallest to the right, with the current position being lower than both....

April 26, 2024 · 2 min · 360 words · Me