Senior Software Engineer | Senior DevOps Engineer
leetcode's Introduction
leetcode's People
leetcode's Issues
理解二分法
核心: 循环不变量
二分查找又死循环了?一个视频讲透二分本质!【基础算法精讲 04】循环不变量 在排序数组中查找元素的第一个和最后一个位置 | 力扣 LeetCode 高频面试题
循环不变量
- 求最小的nums[i] >= target
- 双闭区间, L, M, R。 [L, R]表示需要查询的边界, M指向正在查询的数。[0, L-1]为已经确认的<target的区间,[R+1, n-1]属于>= target的区间
- 终止的情况, R在L的左边,即[R+1, n-1]都>= target, [0, L-1]都< target. 所以R+1或者L是答案
# 要求nums是非递减的, 即nums[i] <= nums[i+1]
# 返回最小的满足nums[i] >= target 的i
# 如果不存在,返回len(nums)
def lower_bound(nums: List[int], target: int) -> int:
left = 0
right = len(nums) - 1
while left <= right:
mid = left + (right - left) // 2
if nums[mid] < target:
left = mid + 1 # [mid+1, right]
else:
right = mid - 1 # [left, mid - 1]
return left
左闭右开区间
def lower_bound1(nums: List[int], target: int) -> int:
left = 0
right = len(nums) # 左闭右开区间 [left, right)
while left < right:
mid = left + (right - left) // 2
if nums[mid] < target:
left = mid + 1 # [mid+1, right)
else:
right = mid # [left, mid)
return left # right
开区间
def lower_bound2(nums: List[int], target: int) -> int:
left = -1
right = len(nums) # 开区间 (left, right)
while left + 1 < right:
mid = left + (right - left) // 2
if nums[mid] < target:
left = mid # (mid, right)
else:
right = mid # (left, mid)
return right
>, >=, <, <=都能互相转换
> 等价于 >= x + 1
< 等价于 (>= x) - 1
<= 等价于 (> x) - 1
minimum swaps required to sort an array
这里的swap不限于相邻元素间的swap。
所以应该找到互相成为循环的元素组
Minimum number of swaps required to sort an array
而对于相邻元素间的swap则可以通过逆序对来求出
1124. Longest Well-Performing Interval
932. Beautiful Array
Kadane算法
the maximum array sum
dp[i]: the maximum array sum ending at i
dp[i] = max(nums[i], dp[i-1] + nums[i])
状态压缩
理解backtracking
backtracing的本质还是dfs
可以从常见的简单的dfs进行推导。
简单的dfs例子200. Number of Islands, 地图类的dfs的方案是从当前的点(i, j)向四周(i+1, j), (i-1, j), (i, j+1), (i, j-1)移动,对于下一个点一般会有条件约束,
而对于46. Permutations从当前点(i, j)向其他所有的点移动,限制是不能取重复的点
二分法
calculator
dijkstra
2398. Maximum Number of Robots Within Budget
求凸包周长
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.