Ethan's Blog


  • Home

  • Archives

  • Tags

  • Search

Two Pointers

Posted on 2021-01-30

一个很灵活的模式,有序与否都有应用的场景。需要根据题意来决定,while 循环的条件是 l < r 还是 l <= r。

In problems where we deal with sorted arrays (or linked lists) and need to find a set of elements that fulfill certain constraints, the Two Pointers approach becomes quite useful. The set of elements could be a pair, a triplet or even a subarray. For example, take a look at the following problem:

Given an array of sorted numbers and a target sum, find a pair in the array whose sum is equal to the given target.

To solve this problem, we can consider each element one by one (pointed out by the first pointer) and iterate through the remaining elements (pointed out by the second pointer) to find a pair with the given sum. The time complexity of this algorithm will be O(N^2) where “N” is the number of elements in the input array.

Given that the input array is sorted, an efficient way would be to start with one pointer in the beginning and another pointer at the end. At every step, we will see if the numbers pointed by the two pointers add up to the target sum. If they do not, we will do one of two things:

  • If the sum of the two numbers pointed by the two pointers is greater than the target sum, this means that we need a pair with a less sum. So, to try more pairs, we can decrement the end-pointer;
  • If the sum of the two numbers pointed by the two pointers is less than the target sum, this means that we need a pair with a greater sum. So, to try more pairs, we can increment the start-pointer;
Read more »

Sliding Window

Posted on 2021-01-30

难点大概是 shrink 的时机。窗口大小固定时,借助 end - start + 1;不固定时,借助基于 matched 的 while 循环。

In many problems dealing with an array (or a linked list), we are asked to find or calculate something among all the contiguous subarrays (or sublists) of a given size. For example, take a look at this problem:

Given an array, find the average of all contiguous subarrays of size “K” in it.

Let’s understand this problem with a real input:

1
Array: [1, 3, 2, 6, -1, 4, 1, 8, 2], K=5

Here, we are asked to find the average of all contiguous subarrays of size “5” in the given array. Let’s solve this:

  1. For the first 5 numbers (subarray from index 0~4), the average is: (1+3+2+6-1)/5 = 2.2;
  2. The average of next 5 numbers (subarray from index 1~5) is: (3+2+6-1+4)/5 = 2.8;
  3. For the next 5 numbers (subarray from index 2~6), the average is: (2+6-1+4+1)/5 = 2.4;
  4. …

Here is the final output containing the averages of all contiguous subarrays of size 5:

1
Output: [2.2, 2.8, 2.4, 3.6, 2.8]
Read more »

Load Balancing

Posted on 2021-01-30

Load Balancer (LB) is another critical component of any distributed system. It helps to spread the traffic across a cluster of servers to improve responsiveness and availability of applications, websites or databases. LB also keeps track of the status of all the resources while distributing requests. If a server is not available to take new requests or is not responding or has elevated error rate, LB will stop sending traffic to such a server.

Typically a load balancer sits between the client and the server accepting incoming network and application traffic and distributing the traffic across multiple backend servers using various algorithms. By balancing application requests across multiple servers, a load balancer reduces individual server load and prevents any one application server from becoming a single point of failure, thus improving overall application availability and responsiveness:

Read more »

System Design Basics

Posted on 2021-01-30

Whenever we are designing a large system, we need to consider a few things:

  1. What are the different architectural pieces that can be used?
  2. How do these pieces work with each other?
  3. How can we best utilize these pieces: what are the right tradeoffs?

Investing in scaling before it is needed is generally not a smart business proposition; however, some forethought into the design can save valuable time and resources in the future. In the following chapters, we will try to define some of the core building blocks of scalable systems. Familiarizing these concepts would greatly benefit in understanding distributed system concepts. In the next section, we will go through Consistent Hashing, CAP Theorem, Load Balancing, Caching, Data Partitioning, Indexes, Proxies, Queues, Replication, and choosing between SQL vs. NoSQL.

Key characteristics of a distributed system include Scalability, Reliability, Availability, Efficiency, and Manageability.

Read more »

如何选择数据结构和算法

Posted on 2021-01-29

工程上的问题往往都比较开放,在选择数据结构和算法的时候,我们往往需要综合各种因素,比如编码难度、维护成本、数据特征、数据规模等,最终选择一个工程的最合适解,而非理论上的最优解。为了让你能做到活学活用,在实际的软件开发中,不生搬硬套数据结构和算法,今天,我们就聊一聊,在实际的软件开发中,如何权衡各种因素,合理地选择使用哪种数据结构和算法?关于这个问题,我总结了六条经验。

时间、空间复杂度不能跟性能划等号

我们在学习每种数据结构和算法的时候,都详细分析了算法的时间复杂度、空间复杂度,但是,在实际的软件开发中,复杂度不能与性能简单划等号,不能表示执行时间和内存消耗的确切数据量:

  • 复杂度不是执行时间和内存消耗的精确值:
    在用大 O 表示法表示复杂度的时候,我们会忽略掉低阶、常数、系数,只保留高阶,并且它的度量单位是语句的执行频度。每条语句的执行时间,并非是相同、确定的。所以,复杂度给出的只能是一个非精确量值的趋势;
  • 代码的执行时间有时不跟时间复杂度成正比:
    我们常说,时间复杂度是 O(nlogn) 的算法,比时间复杂度是 O(n^2) 的算法,执行效率要高。这样说的一个前提是,算法处理的是大规模数据的情况。对于小规模数据的处理,算法的执行效率并不一定跟时间复杂度成正比,有时还会跟复杂度成反比;
  • 对于处理不同问题的不同算法,其复杂度大小没有可比性:
    复杂度只能用来表征不同算法,在处理同样的问题,以及同样数据类型的情况下的性能表现。但是,对于不同的问题、不同的数据类型,不同算法之间的复杂度大小并没有可比性;
Read more »
1…242526…55
necusjz

necusjz

274 posts
16 tags
© 2016 - 2025 necusjz
Powered by Hexo
Theme - NexT.Mist