Algorithms for Work: Array - Rules and Ideas Part 2

Algorithms for Work: Array - Rules and Ideas Part 2

tldr;

Reduce the search range by using all the rules and constraints.

The idea of counting sort extends to hashes.


You can check out the problem at Part 1 here

A little reminder: it's about the balance scale and finding a fake coin out of 16 coins.

I said that 15 times sure do, 8 times can do also: you pick a pair of coins and try it.

Some others are using the binary idea and split it into 2 parts, so we have

First try: 8 vs 8

Second try: 4 vs. 4

Third, try 2 vs. 2 and find out, so it's 3 times.

It's kind of easy, right? How about it's 24 coins now?

If you apply the same idea, it will take you:

1st try: 12 vs 12; 2nd try: 6 vs 6; 3rd try: 3 vs 3;

and the 4th try will be 1 vs. 1 with 1 extra coin on side.

Total: 4 tries (Log2(n))


Do you notice that in the last try, you had 3 coins and split them to 1 vs. 1 and 1 out? You check if the scale leans to the left, right, or balance?

So the scale provide you with three possible case: Left, Right and balance

What happens if I try to find the fake coin in another way?

From 24 coin, split it into 3 groups: 8 + 8 + 8 (1st try)

Take 2 of it to the scale, depending on the case. I know where the fake coin is.

split 8 into: 3 + 3 + 2 (2nd try)

split 3 into 1 + 1 + 1 (3rd try)

I can find the fake coin after 3 tries. Log3(n)


Actually, we just forgot to make use of the balance case to "reduce the search size."

The idea of algorithms is that we want to make use of the rules and constraints to target the answer (reduce the search range or detect the possible solution).

So the above idea is the same, make use of all the cases.


Counting sort:

We have counting sort that uses the same idea here. We want to make use of the index.

The simple problem is: You are a teacher, and you want to count the number of students for all the scores that your students get during each exam. The score ranges from 0 to 10, with only one integer.

In real life, I will just go through all the scores and put them into the boxes for each score from 0 to 10. I saw the 9 put into the 9 box. After that, I can easily check it again and have an answer. When I need to get the list of students getting 0, I just go directly to the 0 box and get them.

In this case, we create an array and make use of both the index and value of it.

For now, index represents the score, and value represents its number.

And the word represent can be changed accordingly to its own use case.

The score can be 8.5, 7.1, or 5.25. We can still make the rules to map the score to an index, for example:

index 615 represent 6,15 score; From the score for example, 7.25, I know that the target index is 725 (multiply by 100)

We are coming up with a new idea here:

Basic: We can get the value from any index given to array.

New idea: We can get the index from any given value (with our rules).


This mapping from a given value to an index can be understood as representing value, and it comes from the HASH function that we made.

And now we come to the field of hash; see you in the next part.

要查看或添加评论,请登录

Nam Nguyen的更多文章

  • Weekly NewsLetter: ?i làm kh?ng vì ti?n

    Weekly NewsLetter: ?i làm kh?ng vì ti?n

    ?i Làm Kh?ng Vì Ti?n – M?t Cau Chuy?n Ngh?ch Ly ?ay là m?t tiêu ?? gay tò mò. ??c ??n cu?i, b?n s? th?y nó th?t s? ?úng…

    32 条评论
  • Weekly Newsletter: Th?i h?c 1 l?n dùng 1 ??i ?? h?t?

    Weekly Newsletter: Th?i h?c 1 l?n dùng 1 ??i ?? h?t?

    S? h?c Trong l?n ng?i chia s? v?i các anh, mình có nghe ???c m?t cau ?úc rút r?t hay. Th?i x?a các c? h?c 1 l?n ?? dùng…

    18 条评论
  • Weekly Newsletter: Relearn | Unlearn

    Weekly Newsletter: Relearn | Unlearn

    V? tay nhanh nh?t th? gi?i G?n ?ay mình ?i h?c nói m?i nh? l?i m?t video t? x?a: K? l?c v? tay nhanh nh?t th? gi?i…

  • Weekly NewsLetter: C?m ?n nh?ng l?i nh?n xét

    Weekly NewsLetter: C?m ?n nh?ng l?i nh?n xét

    Bài h?m nay mu?n nói v? hành trình mình ?i tìm nh?ng l?i nh?n xét ti?p theo ?? thay ??i b?n than mình t?t h?n. Ph?n ??u…

    2 条评论
  • Weekly NewsLetter: L?a ch?n ? tu?i 30 - Th?c t?

    Weekly NewsLetter: L?a ch?n ? tu?i 30 - Th?c t?

    ??u tiên là quay l?i Part 1: 4 ni?m tin c? s? 1 chút cho t? duy này 1. Cu?c ??i là m?t chu?i các l?a ch?n.

    20 条评论
  • Weekly NewsLetter: L?a ch?n ? tu?i 30 Part 2

    Weekly NewsLetter: L?a ch?n ? tu?i 30 Part 2

    N?u ph?i ?úc k?t c? qu?ng th?i gian mình h?c h?i và tìm cho mình ???c các b?n ch?t c?a v?n ?? thì th?t s? r?t dài…

    22 条评论
  • L?a ch?n tu?i 30 - Part 1 Ni?m tin

    L?a ch?n tu?i 30 - Part 1 Ni?m tin

    Ni?m tin s? 1 Cu?c ??i là m?t chu?i các l?a ch?n. Cu?c s?ng c?a m?i ng??i th?c ch?t là k?t qu? c?a nh?ng l?a ch?n chúng…

    8 条评论
  • Weekly NewsLetter: IT H?c nói P1: H?t h?i

    Weekly NewsLetter: IT H?c nói P1: H?t h?i

    Góc nhìn t? podcast v?i Tr?n Qu?c Huy H?m v?a r?i, khi tham gia podcast cùng anh Tr?n Qu?c Huy , mình nh?n ra r?ng c?ng…

    20 条评论
  • PDCA - Plan Do Check Act - H?c 1 bi?t nhi?u

    PDCA - Plan Do Check Act - H?c 1 bi?t nhi?u

    Trong chu?i bài vi?t n?u t?i bi?t tr??c khi 30, th?c ra ?i?u này v? ly thuy?t thì mình bi?t r?i, nh?ng nó l?i chính là…

    1 条评论
  • MVP Hành trình h?c nói - Step 7/7

    MVP Hành trình h?c nói - Step 7/7

    Xin chào mn 7 b??c rèn luy?n t? duy nói chia làm 3 ph?n chính là: Ti?p nh?n ki?n th?c ??nh ngh?a L?a ch?n th?ng tin…

    4 条评论

社区洞察

其他会员也浏览了