Ethan's Blog


  • Home

  • Archives

  • Tags

  • Search

居合斩!二分查找

Posted on 2022-07-19

算法解释

二分查找也常被称为二分法或者折半查找,每次查找时通过将待查找区间分成两部分并只取一部分继续查找,将查找的复杂度大大减少。对于一个长度为 O(n) 的数组,二分查找的时间复杂度为 O(logn)。

具体到代码上,二分查找时区间的左右端取开区间还是闭区间在绝大多数时候都可以,因此有些初学者会容易搞不清楚如何定义区间开闭性。这里我提供两个小诀窍:

  • 尝试熟练使用一种写法,比如左闭右开(满足 C++、Python 等语言的习惯)或左闭右闭(便于处理边界条件),尽量只保持这一种写法。
  • 在刷题时思考如果最后区间只剩下一个数或者两个数,自己的写法是否会陷入死循环,如果某种写法无法跳出死循环,则考虑尝试另一种写法。

二分查找也可以看作双指针的一种特殊情况,但我们一般会将二者区分。双指针类型的题,指针通常是一步一步移动的,而在二分查找里,指针每次移动半个区间长度。

求开方

69. Sqrt(x)

mid = (lo + hi) // 2 可能会因为 lo + hi 溢出而错误,因而采用 mid = lo + ((hi - lo) >> 1) 的写法。

另外,这道题还有一种更快的算法——牛顿迭代法:

1
2
3
4
5
6
def sqrt(x):
num = x
while num * num > x:
num = (num + x // num) // 2

return num
Read more »

玩转双指针

Posted on 2022-07-17

算法解释

双指针主要用于遍历数组,两个指针指向不同的元素,从而协同完成任务。也可以延伸到多个数组的多个指针:

  • 若两个指针指向同一数组,遍历方向相同且不会相交,则也称为滑动窗口(两个指针包围的区域即为当前的窗口),经常用于区间搜索。
  • 若两个指针指向同一数组,但是遍历方向相反,则可以用来进行搜索,待搜索的数组往往是排好序的。

在 C++ 中,要注意 const 的位置对指针效果的影响:

1
2
3
4
5
int x;
int * p1 = &x; // 指针可以被修改,值也可以被修改
const int * p2 = &x; // 指针可以被修改,值不可以被修改 (const int)
int * const p3 = &x; // 指针不可以被修改 (* const),值可以被修改
const int * const p4 = &x; // 指针不可以被修改,值也不可以被修改

Two Sum

167. Two Sum II - Input Array Is Sorted
因为数组已经排好序,我们可以采用方向相反的双指针来寻找这两个数字,一个初始指向最小的元素,即数组最左边,向右遍历;一个初始指向最大的元素,即数组最右边,向左遍历。

归并两个有序数组

88. Merge Sorted Array
因为这两个数组已经排好序,我们可以把两个指针分别放在两个数组的末尾,即 nums1 的 m−1 位和 nums2 的 n−1 位。每次将较大的那个数字复制到 nums1 的后边,然后向前移动一位。因为我们也要定位 nums1 的末尾,所以我们还需要第三个指针,以便复制。

a++ 和 ++a 都是将 a 加 1,但是 a++ 返回值为 a,而 ++a 返回值为 a+1。如果只是希望增加 a 的值,而不需要返回值,则推荐使用 ++a,其运行速度会略快一些。

Read more »

最易懂的贪心算法

Posted on 2022-07-11

算法解释

顾名思义,贪心算法或贪心思想采用贪心的策略,保证每次操作都是局部最优的,从而使最后得到的结果是全局最优的。

举一个最简单的例子:小明和小王喜欢吃苹果,小明可以吃五个,小王可以吃三个。已知苹果园里有吃不完的苹果,求小明和小王一共最多吃多少个苹果。在这个例子中,我们可以选用的贪心策略为,每个人吃自己能吃的最多数量的苹果,这在每个人身上都是局部最优的。又因为全局结果是局部结果的简单求和,且局部结果互不相干,因此局部最优的策略也同样是全局最优的。

证明一道题能用贪心算法解决,有时远比用贪心算法解决该题更复杂。一般情况下,在简单操作后,具有明显的从局部到整体的递推关系,或者可以通过数学归纳法推测结果时,我们才会使用贪心算法。

分配问题

455. Assign Cookies
因为饥饿度最小的孩子最容易吃饱,所以我们先考虑这个孩子。为了尽量使得剩下的饼干可以满足饥饿度更大的孩子,所以我们应该把大于等于这个孩子饥饿度的、且大小最小的饼干给这个孩子。

对数组或字符串排序是常见的操作,方便之后的大小比较,排序顺序默认是从小到大。在之后的讲解中,若我们谈论的是对连续空间的变量进行操作,我们并不会明确区分数组和字符串,因为他们本质上都是在连续空间上的有序变量集合。一个字符串 “abc” 可以被看作一个数组 [“a”, “b”, “c”]。

135. Candy
存在比较关系的贪心策略并不一定需要排序。这里的贪心策略即为,在每次遍历中,只考虑并更新相邻一侧的大小关系。

Read more »

Describe Core Azure Architectural Components

Posted on 2022-04-30

Overview of Azure subscriptions, management groups, and resources

You need to learn the organizing structure for resources in Azure, which has four levels: Management Groups, Subscriptions, Resource Groups, and Resources. The following image shows the top-down hierarchy of organization for these levels:

Having seen the top-down hierarchy of organization, let’s describe each of those levels from the bottom up:

  • Resources: Resources are instances of services that you create, like VMs, storage, or SQL databases;
  • Resource groups: Resources are combined into resource groups, which act as a logical container into which Azure resources like web apps, databases, and storage accounts are deployed and managed;
  • Subscriptions: A subscription groups together user accounts and the resources that have been created by those user accounts. For each subscription, there are limits or quotas on the amount of resources that you can create and use. Organizations can use subscriptions to manage costs and the resources that are created by users, teams, or projects;
  • Management groups: They help you manage access, policy, and compliance for multiple subscriptions. All subscriptions in a management group automatically inherit the conditions applied to the management group;
Read more »

Discuss Azure Fundamental Concepts

Posted on 2022-04-27

Discuss different types of cloud models

There are three deployment models for cloud computing: Public Cloud, Private Cloud, and Hybrid Cloud. Each deployment model has different aspects that you should consider as you migrate to the cloud.

What are public, private, and hybrid clouds?

Deployment Model Description
Public Cloud Services are offered over the public internet and available to anyone who wants to purchase them. Cloud resources, such as servers and storage, are owned and operated by a third-party cloud service provider, and delivered over the internet.
Private Cloud A private cloud consists of computing resources used exclusively by users from one business or organization. A private cloud can be physically located at your organization’s on-site (on-premises) datacenter, or it can be hosted by a third-party service provider.
Hybrid Cloud A hybrid cloud is a computing environment that combines a public cloud and a private cloud by allowing data and applications to be shared between them.
Read more »
1…678…55
necusjz

necusjz

271 posts
16 tags
© 2016 - 2026 necusjz
Powered by Hexo
Theme - NexT.Mist