Luyện nói tiếng Anh bằng Shadowing qua video: Two Sum - Leetcode 1 - HashMap - Python

Đang tạo bài học...
1
Let's solve leak code 1 to sum, the most popular leak code question.
2
So we're given an input array and some target, in this case 9.
3
And we want to find the two values in this input array that sum to 9.
4
So in this case, it's 2 and 7.
5
Now we want to return the indices of these two values.
6
So the index of 2 is 0, the index of 7 is 1.
7
So we return 0 and 1.
8
We're guaranteed that there's exactly one solution, so we don't have to worry about not finding a solution, and we don't have to worry about multiple solutions.
9
Now, the most intuitive way to solve this problem is basically just check every combination of two values
10
and see if they can sum up to our target.
11
So we start at two, we check every combination we can make that includes two.
12
So we scan through the remainder of the array, one, five, three, and check if any of those numbers added to two sums to our target four.
13
In this case, none of them do.
14
So next we can repeat the process.
15
Let's check every combination including one that sums up to target four.
16
So we scan through every element that comes after it, five and three, and we find that one added with three sums up to our target four.
17
Notice that we didn't have to check the values
18
that came before one because we already checked the combination two and one when we were up over here.
19
Remember when we checked every combination with 2.
20
So we didn't have to repeat that work down here.
21
We only had to check the numbers that came after 1.
22
So the runtime of this algorithm isn't super efficient.
23
This is basically brute force.
24
We're going through the entire array of length n and we're going to do that worst case n times for each number.
25
This means that overall worst case time complexity will be O of n squared.
26
So can we do better?
27
Now the thing to notice is that for each number,
28
for example, 1, the value we're looking for is the difference between the target and this value 1.
29
So we're looking for 4 minus 1, which is equal to 3.
30
So that means this is the only value we can add to 1 that'll equal the target.
31
So we don't have to check every number, we just want to know if 3 exists.
32
Now the easiest way we can do this, the most efficient, is by making a hash map of every value in our input array
33
so we can instantly check if the value 3 exists.
34
Now let's try the same problem except let's use a hash map this time.
35
Now in our hash map, we're going to be mapping each value to the index of each value.
36
So the index of 2 is 0, the index of 1 is 1, the index of 5 is 2, the index of 3 is 3.
37
So in our hash map, we're going to be mapping the value to the index.
38
now we could add every value in this array into the hash map before we start iterating through it
39
but there's actually an easier way to do it
40
if we added the entire array into the hash map initially
41
then we would get to the value 2 first right we
42
would want to check does the difference between target 4 minus this value 2
43
which is equal to 2 exists in our hash map and we would find that 2 does exist in our hash map, but we're not allowed to reuse the same one, right?
44
Because they're both at the same index.
45
We can't use the same value twice, so we would have to compare the index of our current
46
two with the index of the two that's in our hash map.
47
There's actually an easier way to do this though, and it's a little clever, and let me show you how to do it that way.
48
So doing it this clever way, initially we say our hash map is empty.
49
So we get to the value two first of all, right?
50
And we want to look for the difference 4 minus 2 in our hash map.
51
Our hash map is empty, so we don't find 2.
52
So then, after we've visited this element, then we can add it to our hash map.
53
So now that I'm done visiting it, I'm going to move to the second element 1.
54
And before I do that, I'm going to add this value 2 to our hash map, and the index of this value is going to be 0.
55
Now I'm at 1.
56
I'm looking for 4 minus 1, which is 3.
57
I see 3 isn't in our hash map, but it actually is in our array.
58
So what's the problem?
59
Well, for now, we're going to say we don't find a 3.
60
So we add 1 to our hash map.
61
The index of this 1 is 1.
62
And now we move to the next element, 5.
63
we check does 4 minus 5 uh it's 4 minus 5 exist in our hash map that's negative 1
64
so no it does not then we add this 5 to our hash map
65
and it's index which is 2
66
and we move to the last value in the array 3
67
we check does 4 minus 3 exist in our hash map now that's 1
68
so we see it does exist right over here.
69
The value exists and its index is one.
70
So now we found our two values that sum to the target and we want to return their indexes,
71
their indices, which are going to be one and three.
72
So with this algorithm, we don't have to initialize our hash map.
73
It can be initially empty and then we can just iterate through this array in one pass.
74
Now the reason the algorithm can work in that way with just one pass is this.
75
So let's say we had a giant array, right?
76
We know for sure that there are two elements in this array that sum to our target, right?
77
We don't know where they are.
78
They're at some arbitrary location.
79
When we visit the first one of those elements, our hash map is only gonna be this portion of the array.
80
It's only gonna have the values that came before the first value.
81
So we're going to notice that the second value
82
that can sum to the target is not going to be in our hash map yet.
83
But once we get to the second value, our hash map is going to be this portion.
84
So every value that comes before this, right?
85
So we're going to be guaranteed that once we visit the second element that sums up to the target, we're going to be guaranteed that the first one is already in our hash map.
86
So we're guaranteed to find the solution.
87
Now, since we only have to iterate through the array once, and we're adding each value to our hash map, which is a constant time operation,
88
and we're checking if a value exists in our hash map, which is also a constant time operation, the time complexity is going to be big O of n.
89
We are using extra memory, right?
90
That hash map isn't free, so the memory complexity is also going to be O of n
91
because we could potentially add every value to the hash map.
92
So now let's code the solution.
93
So remember we need a hash map, right?
94
I'm going to call this previous map because it's basically every element that comes before the current element.
95
Every previous element is going to be stored in this map.
96
We're going to be mapping the value to the index of that value.
97
So now let's iterate through every value in this array.
98
We need the index as well as the actual number.
99
So let's do it like this in Python.
100
Before we add this to our map, let's check if the difference, which is equal to target minus n.
101
Now let's check if this difference is already in the hash map.
102
if it is, then we can return the solution, which is going to be a pair of the indices.
103
So I can get the first index like this, and the second index is just i.
104
Now if we don't find the solution, then we have to update our hash map.
105
So for this value n, I'm going to say the index is i, and then we're going to continue.
106
Since we're guaranteed that a solution exists, we don't have to return anything out here, right?
107
But I'll just put a return for no reason.
108
Now let's see if it works. And it works perfectly.
109
So with this kind of neat little trick with just doing it in one pass, you can reduce the amount of code you have to write
110
and not have to worry about like edge cases and comparisons and things like that.

Bối cảnh & Nền tảng

Trong video, người thuyết trình giải thích cách giải quyết một bài toán phổ biến trên Leetcode mang tên "Two Sum". Bài toán này yêu cầu tìm hai giá trị trong một mảng sao cho tổng của chúng bằng một số mục tiêu đã cho. Người thuyết trình sử dụng các ví dụ cụ thể trong quá trình giải thích, điều này giúp người xem dễ dàng theo dõi và áp dụng các kỹ năng giải quyết vấn đề của mình. Để có thể nắm vững kiến thức, bạn sẽ cần luyện tập nói theo cách mà người thuyết trình giao tiếp. Phương pháp shadow speak hay shadowing tiếng anh sẽ rất hữu ích trong trường hợp này.

5 Cụm từ hàng đầu cho Giao tiếp Hàng ngày

  • Giải bài tập: "Let's solve leak code 1 to sum."
  • Mảng đầu vào: "We're given an input array."
  • Số mục tiêu: "We want to find the two values... that sum to 9."
  • Chỉ số của giá trị: "We return the indices of these two values."
  • Đảm bảo có một giải pháp: "We're guaranteed that there's exactly one solution."

Hướng dẫn Shadowing theo từng bước

Để nâng cao kỹ năng giao tiếp tiếng Anh của bạn thông qua shadowing, hãy làm theo các bước sau:

  1. Xem video một lần: Trước tiên, hãy xem video để hiểu nội dung chung và cách diễn đạt của người thuyết trình.
  2. Nghe và ghi chú: Nghe lại video và ghi chú lại các cụm từ quan trọng cùng cách phát âm của chúng.
  3. Luyện tập theo từng câu: Hãy tạm dừng video sau mỗi câu và lặp lại theo cách phát âm của diễn giả. Điều này sẽ giúp bạn cải thiện cách nói và ngữ điệu.
  4. Kết hợp với mạch thuyết trình: Khi bạn đã quen với từng câu, hãy cố gắng nói theo khi người thuyết trình diễn giải. Cố gắng đồng bộ hóa với tốc độ của họ.
  5. Ghi âm và lắng nghe lại: Ghi âm giọng nói của bạn khi thực hiện shadow speaks và so sánh với diễn giả để thấy được sự tiến bộ của bạn.

Thực hành thường xuyên với shadowing tiếng anh sẽ giúp bạn tự tin hơn trong giao tiếp và mở rộng vốn từ vựng của mình.

Phương Pháp Shadowing Là Gì?

Shadowing là kỹ thuật học ngôn ngữ có cơ sở khoa học, ban đầu được phát triển cho chương trình đào tạo phiên dịch viên chuyên nghiệp và được phổ biến rộng rãi bởi nhà đa ngôn ngữ học Dr. Alexander Arguelles. Nguyên lý cốt lõi đơn giản nhưng cực kỳ hiệu quả: bạn nghe tiếng Anh của người bản xứ và lặp lại to ngay lập tức — như một "cái bóng" (shadow) đuổi theo người nói với độ trễ chỉ 1–2 giây. Khác với luyện ngữ pháp hay học từ vựng bị động, Shadowing buộc não bộ và cơ miệng phải đồng thời xử lý và tái tạo ngôn ngữ thực tế. Các nghiên cứu khoa học xác nhận phương pháp này cải thiện đáng kể phát âm, ngữ điệu, nhịp điệu, nối âm, kỹ năng nghe và độ lưu loát khi nói — đặc biệt hiệu quả cho người luyện IELTS Speaking và muốn giao tiếp tiếng Anh tự nhiên như người bản ngữ.