{"id":1616,"date":"2025-09-29T21:21:19","date_gmt":"2025-09-29T14:21:19","guid":{"rendered":"https:\/\/kienthucmo.com\/?p=1616"},"modified":"2025-11-05T23:43:03","modified_gmt":"2025-11-05T16:43:03","slug":"sap-xep-cac-phan-tu-trong-set-voi-python","status":"publish","type":"post","link":"https:\/\/kienthucmo.com\/vi\/sap-xep-cac-phan-tu-trong-set-voi-python\/","title":{"rendered":"S\u1eafp x\u1ebfp c\u00e1c ph\u1ea7n t\u1eed trong Set v\u1edbi Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Set l\u00e0 m\u1ed9t c\u1ea5u tr\u00fac r\u1ea5t h\u1eefu \u00edch: l\u01b0u ph\u1ea7n t\u1eed <strong>duy nh\u1ea5t<\/strong>, h\u1ed7 tr\u1ee3 ki\u1ec3m tra membership O(1) trung b\u00ecnh, ph\u00f9 h\u1ee3p khi ta quan t\u00e2m \u201cc\u00f3 hay kh\u00f4ng\u201d ch\u1ee9 kh\u00f4ng ph\u1ea3i \u201c\u1edf v\u1ecb tr\u00ed n\u00e0o\u201d. Tuy nhi\u00ean v\u00ec <strong>set l\u00e0 unordered<\/strong>, ta kh\u00f4ng th\u1ec3 &#8220;s\u1eafp x\u1ebfp tr\u1ef1c ti\u1ebfp&#8221; tr\u00ean set nh\u01b0 list. Khi c\u1ea7n th\u1ee9 t\u1ef1 \u2014 \u0111\u1ec3 in theo th\u1ee9 t\u1ef1, \u0111\u1ec3 t\u00ecm top-k hay \u0111\u1ec3 d\u00f9ng binary search \u2014 ta c\u1ea7n chuy\u1ec3n sang m\u1ed9t d\u1eef li\u1ec7u c\u00f3 th\u1ee9 t\u1ef1 (list\/tuple) ho\u1eb7c d\u00f9ng m\u1ed9t c\u1ea5u tr\u00fac d\u1eef li\u1ec7u \u0111\u1eb7c bi\u1ec7t \u201csorted set\u201d.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Trong b\u00e0i n\u00e0y h\u00e3y c\u00f9ng m\u00ecnh tim hi\u1ec3u :<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>B\u1ea3n ch\u1ea5t vi\u1ec7c s\u1eafp x\u1ebfp set.<\/li>\n\n\n\n<li>C\u00e1c c\u00e1ch ph\u1ed5 bi\u1ebfn: <code>sorted()<\/code>, chuy\u1ec3n \u0111\u1ed5i th\u00e0nh list\/tuple, duy tr\u00ec b\u1ed9 d\u1eef li\u1ec7u \u0111\u00e3 s\u1eafp b\u1eb1ng <code>bisect<\/code> ho\u1eb7c d\u00f9ng th\u01b0 vi\u1ec7n ngo\u00e0i.<\/li>\n\n\n\n<li>V\u00ed d\u1ee5 chi ti\u1ebft k\u00e8m gi\u1ea3i th\u00edch t\u1eebng b\u01b0\u1edbc.<\/li>\n\n\n\n<li>Ph\u00e2n t\u00edch \u0111\u1ed9 ph\u1ee9c t\u1ea1p, l\u01b0u \u00fd v\u00e0 c\u00e1c sai l\u1ea7m th\u01b0\u1eddng g\u1eb7p.<\/li>\n\n\n\n<li>Khi n\u00e0o d\u00f9ng c\u00e1ch n\u00e0o.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. T\u1ed5ng quan v\u1ec1 Set v\u00e0 Sorting<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Set<\/strong> trong Python: t\u1eadp h\u1ee3p c\u00e1c ph\u1ea7n t\u1eed kh\u00f4ng tr\u00f9ng nhau, kh\u00f4ng c\u00f3 th\u1ee9 t\u1ef1 (implementation uses hash tables).<\/li>\n\n\n\n<li><strong>Immutable vs mutable:<\/strong> <code>set<\/code> l\u00e0 mutable (c\u00f3 th\u1ec3 th\u00eam\/x\u00f3a), <code>frozenset<\/code> l\u00e0 immutable nh\u01b0ng c\u0169ng unordered.<\/li>\n\n\n\n<li><strong>Sorting<\/strong> \u1edf \u0111\u00e2y kh\u00f4ng ph\u1ea3i thay \u0111\u1ed5i set, m\u00e0 l\u00e0 <strong>t\u1ea1o ra m\u1ed9t bi\u1ec3u di\u1ec5n c\u00f3 th\u1ee9 t\u1ef1<\/strong> t\u1eeb set (v\u00ed d\u1ee5 m\u1ed9t list \u0111\u00e3 s\u1eafp).<\/li>\n\n\n\n<li><strong>H\u1ec7 qu\u1ea3:<\/strong> m\u1ecdi thao t\u00e1c \u201cs\u1eafp\u201d \u0111\u00f2i h\u1ecfi sao ch\u00e9p d\u1eef li\u1ec7u (O(n) memory) v\u00ec b\u1ea3n th\u00e2n set kh\u00f4ng gi\u1eef th\u1ee9 t\u1ef1.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">K\u1ebft lu\u1eadn ng\u1eafn: n\u1ebfu m\u1ee5c ti\u00eau ch\u1ec9 l\u00e0 <em>l\u1ea5y d\u1eef li\u1ec7u theo th\u1ee9 t\u1ef1 t\u1ea1m th\u1eddi<\/em> th\u00ec <code>sorted()<\/code> th\u01b0\u1eddng \u0111\u1ee7. N\u1ebfu c\u1ea7n <em>duy tr\u00ec th\u1ee9 t\u1ef1 khi ch\u00e8n\/x\u00f3a<\/em> th\u00ec ph\u1ea3i d\u00f9ng c\u1ea5u tr\u00fac kh\u00e1c.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. S\u1eafp x\u1ebfp Set b\u1eb1ng h\u00e0m d\u1ef1ng s\u1eb5n (<code>sorted()<\/code>)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">C\u00e1ch \u0111\u01a1n gi\u1ea3n nh\u1ea5t l\u00e0 d\u00f9ng <code>sorted()<\/code> \u2014 n\u00f3 nh\u1eadn b\u1ea5t k\u1ef3 iterable n\u00e0o v\u00e0 tr\u1ea3 v\u1ec1 m\u1ed9t list \u0111\u00e3 s\u1eafp.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>my_set = {7, 2, 5, 1, 4}\n\n# sorted returns a new list (ascending by default)\nsorted_list = sorted(my_set)  # &#91;1, 2, 4, 5, 7&#93;\n\n# If you want a tuple result\nsorted_tuple = tuple(sorted(my_set))  # (1, 2, 4, 5, 7)\n\n# Sort descending\ndesc_list = sorted(my_set, reverse=True)  # &#91;7, 5, 4, 2, 1&#93;\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">my_set <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #B48EAD\">7<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">2<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">5<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">4<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># sorted returns a new list (ascending by default)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">sorted_list <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">sorted<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">my_set<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># &#91;1, 2, 4, 5, 7&#93;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># If you want a tuple result<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">sorted_tuple <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">tuple<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #88C0D0\">sorted<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">my_set<\/span><span style=\"color: #ECEFF4\">))<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># (1, 2, 4, 5, 7)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Sort descending<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">desc_list <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">sorted<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">my_set<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">reverse<\/span><span style=\"color: #81A1C1\">=True<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># &#91;7, 5, 4, 2, 1&#93;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Gi\u1ea3i th\u00edch chi ti\u1ebft:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>sorted()<\/code> s\u1ebd iterate qua set, thu th\u1eadp ph\u1ea7n t\u1eed r\u1ed3i s\u1eafp x\u1ebfp (th\u01b0\u1eddng d\u00f9ng Timsort) \u2014 time O(n log n), space O(n) (k\u1ebft qu\u1ea3 l\u00e0 list).<\/li>\n\n\n\n<li>V\u00ec set kh\u00f4ng c\u00f3 ch\u1ec9 s\u1ed1, <code>sorted()<\/code> kh\u00f4ng thay \u0111\u1ed5i set g\u1ed1c \u2014 n\u00f3 t\u1ea1o ra list m\u1edbi.<\/li>\n\n\n\n<li>N\u1ebfu c\u1ea7n gi\u1eef c\u1ea5u tr\u00fac l\u00e0 set (nh\u01b0ng <em>mu\u1ed1n bi\u1ec3u di\u1ec5n s\u1eafp<\/em>), b\u1ea1n c\u00f3 th\u1ec3 l\u01b0u list ho\u1eb7c tuple s\u1eafp \u0111\u01b0\u1ee3c; \u0111\u1eb7t l\u1ea1i v\u00e0o set s\u1ebd m\u1ea5t th\u1ee9 t\u1ef1.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>L\u01b0u \u00fd:<\/strong> <code>tuple(sorted(my_set))<\/code> t\u1ea1o tuple b\u1ea5t bi\u1ebfn ch\u1ee9a ph\u1ea7n t\u1eed \u0111\u00e3 s\u1eafp.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. S\u1eafp x\u1ebfp theo ti\u00eau ch\u00ed t\u00f9y ch\u1ec9nh (key &amp; reverse)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>sorted()<\/code> h\u1ed7 tr\u1ee3 <code>key<\/code> v\u00e0 <code>reverse<\/code>. \u0110i\u1ec1u n\u00e0y r\u1ea5t quan tr\u1ecdng khi set ch\u1ee9a ph\u1ea7n t\u1eed ph\u1ee9c t\u1ea1p nh\u01b0 tuple, dict keys, strings v\u1edbi y\u00eau c\u1ea7u \u0111\u1eb7c bi\u1ec7t.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>people = {(\"Alice\", 30), (\"Bob\", 25), (\"Charlie\", 30)}\n\n# Sort by age ascending, then by name ascending (stable sort keeps order of equal keys)\nsorted_by_age_then_name = sorted(people, key=lambda x: (x&#91;1&#93;, x&#91;0&#93;))\n# &#91;('Bob', 25), ('Alice', 30), ('Charlie', 30)&#93;\n\n# Example: sort strings by length\nwords = {\"apple\", \"fig\", \"banana\", \"kiwi\"}\nsorted_by_len = sorted(words, key=len)  # &#91;'fig', 'kiwi', 'apple', 'banana'&#93;\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">people <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Alice<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">30<\/span><span style=\"color: #ECEFF4\">),<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Bob<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">25<\/span><span style=\"color: #ECEFF4\">),<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Charlie<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">30<\/span><span style=\"color: #ECEFF4\">)}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Sort by age ascending, then by name ascending (stable sort keeps order of equal keys)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">sorted_by_age_then_name <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">sorted<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">people<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">key<\/span><span style=\"color: #81A1C1\">=lambda<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">x<\/span><span style=\"color: #ECEFF4\">:<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">x<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #ECEFF4\">&#93;,<\/span><span style=\"color: #D8DEE9FF\"> x<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #B48EAD\">0<\/span><span style=\"color: #ECEFF4\">&#93;))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># &#91;(&#39;Bob&#39;, 25), (&#39;Alice&#39;, 30), (&#39;Charlie&#39;, 30)&#93;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Example: sort strings by length<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">words <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">apple<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">fig<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">banana<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">kiwi<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">sorted_by_len <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">sorted<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">words<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">key<\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #88C0D0\">len<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># &#91;&#39;fig&#39;, &#39;kiwi&#39;, &#39;apple&#39;, &#39;banana&#39;&#93;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Gi\u1ea3i th\u00edch:<\/strong> <code>key<\/code> tr\u1ea3 v\u1ec1 gi\u00e1 tr\u1ecb so s\u00e1nh cho m\u1ed7i ph\u1ea7n t\u1eed; v\u1edbi tuple tr\u1ea3 v\u1ec1 tuple key \u0111\u1ec3 quy\u1ebft \u0111\u1ecbnh nhi\u1ec1u ti\u00eau ch\u00ed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Khi c\u1ea7n \u201cd\u1eef li\u1ec7u lu\u00f4n s\u1eafp\u201d \u2014 c\u00e1c l\u1ef1a ch\u1ecdn duy tr\u00ec sorted set<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">N\u1ebfu b\u1ea1n c\u1ea7n d\u1eef li\u1ec7u lu\u00f4n \u1edf tr\u1ea1ng th\u00e1i s\u1eafp khi ch\u00e8n\/x\u00f3a (v\u00ed d\u1ee5 leaderboard hay x\u1eed l\u00fd streaming), <code>sorted(my_set)<\/code> m\u1ed7i l\u1ea7n s\u1ebd t\u1ed1n O(n log n) \u2014 kh\u00f4ng hi\u1ec7u qu\u1ea3 n\u1ebfu th\u01b0\u1eddng xuy\u00ean c\u1eadp nh\u1eadt. C\u00e1c l\u1ef1a ch\u1ecdn:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.1 D\u00f9ng list + <code>bisect<\/code> (standard library)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Gi\u1eef m\u1ed9t danh s\u00e1ch \u0111\u00e3 s\u1eafp v\u00e0 d\u00f9ng <code>bisect.insort<\/code> \u0111\u1ec3 ch\u00e8n ph\u1ea7n t\u1eed m\u1edbi \u1edf v\u1ecb tr\u00ed \u0111\u00fang \u2014 ch\u00e8n O(n) (do d\u1ecbch chuy\u1ec3n), t\u00ecm v\u1ecb tr\u00ed O(log n).<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>import bisect\n\n# maintain a sorted list representing a set (unique items)\nsorted_list = []\n\ndef insert_unique(sorted_list, value):\n    # find insertion point\n    pos = bisect.bisect_left(sorted_list, value)\n    # if value not present, insert\n    if pos == len(sorted_list) or sorted_list&#91;pos&#93; != value:\n        bisect.insort_left(sorted_list, value)\n\n# Usage\ninsert_unique(sorted_list, 5)\ninsert_unique(sorted_list, 3)\ninsert_unique(sorted_list, 7)\nprint(sorted_list)  # &#91;3, 5, 7&#93;\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">import<\/span><span style=\"color: #D8DEE9FF\"> bisect<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># maintain a sorted list representing a set (unique items)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">sorted_list <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">[]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">def<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">insert_unique<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9\">sorted_list<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">value<\/span><span style=\"color: #ECEFF4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #616E88\"># find insertion point<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    pos <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> bisect<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">bisect_left<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">sorted_list<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> value<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #616E88\"># if value not present, insert<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">if<\/span><span style=\"color: #D8DEE9FF\"> pos <\/span><span style=\"color: #81A1C1\">==<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">len<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">sorted_list<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">or<\/span><span style=\"color: #D8DEE9FF\"> sorted_list<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">pos<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">!=<\/span><span style=\"color: #D8DEE9FF\"> value<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        bisect<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">insort_left<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">sorted_list<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> value<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Usage<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">insert_unique<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">sorted_list<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">5<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">insert_unique<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">sorted_list<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">insert_unique<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">sorted_list<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">7<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">sorted_list<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># &#91;3, 5, 7&#93;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Gi\u1ea3i th\u00edch:<\/strong> <code>bisect<\/code> nhanh \u0111\u1ec3 t\u00ecm v\u1ecb tr\u00ed nh\u01b0ng ch\u00e8n v\u1eabn O(n) v\u00ec ph\u1ea3i d\u1ecbch chuy\u1ec3n ph\u1ea7n t\u1eed. Tuy nhi\u00ean n\u1ebfu k\u00edch th\u01b0\u1edbc v\u1eeba ph\u1ea3i v\u00e0 ch\u00e8n t\u01b0\u01a1ng \u0111\u1ed1i \u00edt, \u0111\u00e2y l\u00e0 c\u00e1ch \u0111\u01a1n gi\u1ea3n, kh\u00f4ng c\u1ea7n th\u01b0 vi\u1ec7n ngo\u00e0i.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.2 D\u00f9ng heap (heapq) \u2014 ph\u00f9 h\u1ee3p top-k \/ priority<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>heapq<\/code> kh\u00f4ng gi\u1eef to\u00e0n b\u1ed9 danh s\u00e1ch theo th\u1ee9 t\u1ef1 nh\u01b0ng gi\u00fap l\u1ea5y min\/max hi\u1ec7u qu\u1ea3. Kh\u00f4ng thay th\u1ebf cho sorted set n\u1ebfu c\u1ea7n truy c\u1eadp theo th\u1ee9 t\u1ef1 to\u00e0n b\u1ed9.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4.3 D\u00f9ng th\u01b0 vi\u1ec7n b\u00ean th\u1ee9 ba (v\u00ed d\u1ee5 <code>sortedcontainers.SortedSet<\/code>)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">C\u00f3 c\u00e1c th\u01b0 vi\u1ec7n nh\u01b0 <code>sortedcontainers<\/code> (n\u1ebfu c\u00e0i th\u00eam) cung c\u1ea5p <code>SortedList<\/code>\/<code>SortedSet<\/code> v\u1edbi ch\u00e8n\/x\u00f3a O(log n) ho\u1eb7c amortized t\u1ed1t. V\u00ed d\u1ee5 c\u00e0i <code>pip install sortedcontainers<\/code> v\u00e0 d\u00f9ng <code>from sortedcontainers import SortedSet<\/code>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">L\u01b0u \u00fd: \u0111\u00e2y l\u00e0 th\u01b0 vi\u1ec7n ngo\u00e0i, ph\u00f9 h\u1ee3p khi hi\u1ec7u n\u0103ng quan tr\u1ecdng.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">5. C\u00e1c v\u00ed d\u1ee5 th\u1ef1c t\u1ebf<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">5.1 L\u1ea5y top-k l\u1edbn nh\u1ea5t t\u1eeb set (efficient)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">N\u1ebfu ch\u1ec9 c\u1ea7n top-k, kh\u00f4ng c\u1ea7n s\u1eafp to\u00e0n b\u1ed9 set \u2014 d\u00f9ng <code>heapq.nlargest<\/code> (O(n log k)):<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>import heapq\n\ndata_set = {15, 3, 9, 20, 7, 30}\ntop_3 = heapq.nlargest(3, data_set)  # &#91;30, 20, 15&#93;\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">import<\/span><span style=\"color: #D8DEE9FF\"> heapq<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">data_set <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #B48EAD\">15<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">9<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">20<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">7<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">30<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">top_3 <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> heapq<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">nlargest<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #B48EAD\">3<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> data_set<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># &#91;30, 20, 15&#93;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Gi\u1ea3i th\u00edch:<\/strong> so v\u1edbi <code>sorted(data_set, reverse=True)[:3]<\/code> l\u00e0 O(n log n), <code>nlargest<\/code> t\u1ed1i \u01b0u khi k nh\u1ecf.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5.2 L\u01b0u set theo th\u1ee9 t\u1ef1 ch\u00e8n nh\u01b0ng s\u1eafp theo gi\u00e1 tr\u1ecb khi in (keep original)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">N\u1ebfu b\u1ea1n mu\u1ed1n gi\u1eef set g\u1ed1c (v\u00ec t\u00ednh unique &amp; mutation) nh\u01b0ng khi in ra c\u1ea7n s\u1eafp:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>events = {\"login\", \"logout\", \"signup\", \"purchase\"}\n# keep original set unchanged\nprint(\"Sorted events:\", sorted(events))\n# original set remains\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">events <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">{<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">login<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">logout<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">signup<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">purchase<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># keep original set unchanged<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">print<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">Sorted events:<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">sorted<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">events<\/span><span style=\"color: #ECEFF4\">))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># original set remains<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Gi\u1ea3i th\u00edch:<\/strong> \u0111\u01a1n gi\u1ea3n v\u00e0 an to\u00e0n \u2014 d\u00f9ng <code>sorted()<\/code> m\u1ed7i khi c\u1ea7n view.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5.3 Duy tr\u00ec leaderboard (score set) b\u1eb1ng SortedList (concept)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">N\u1ebfu b\u1ea1n c\u1ea7n leaderboard lu\u00f4n s\u1eafp x\u1ebfp khi c\u1eadp nh\u1eadt, d\u00f9ng list+bisect ho\u1eb7c SortedSet t\u1eeb th\u01b0 vi\u1ec7n.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>import bisect\n\nleaderboard = []  # sorted descending by score, store tuples (-score, user)\n\ndef add_score(leaderboard, user, score):\n    entry = (-score, user)  # negative for descending order\n    pos = bisect.bisect_left(leaderboard, entry)\n    if pos == len(leaderboard) or leaderboard&#91;pos&#93; != entry:\n        leaderboard.insert(pos, entry)  # O(n) insert\n\nadd_score(leaderboard, \"alice\", 120)\nadd_score(leaderboard, \"bob\", 150)\nadd_score(leaderboard, \"charlie\", 130)\n# convert back to readable format\nreadable = &#91;(user, -score) for score, user in leaderboard&#93;\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">import<\/span><span style=\"color: #D8DEE9FF\"> bisect<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">leaderboard <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">[]<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># sorted descending by score, store tuples (-score, user)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">def<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">add_score<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9\">leaderboard<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">user<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #D8DEE9\">score<\/span><span style=\"color: #ECEFF4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    entry <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #81A1C1\">-<\/span><span style=\"color: #D8DEE9FF\">score<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> user<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># negative for descending order<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    pos <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> bisect<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">bisect_left<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">leaderboard<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> entry<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">if<\/span><span style=\"color: #D8DEE9FF\"> pos <\/span><span style=\"color: #81A1C1\">==<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #88C0D0\">len<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">leaderboard<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">or<\/span><span style=\"color: #D8DEE9FF\"> leaderboard<\/span><span style=\"color: #ECEFF4\">&#91;<\/span><span style=\"color: #D8DEE9FF\">pos<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">!=<\/span><span style=\"color: #D8DEE9FF\"> entry<\/span><span style=\"color: #ECEFF4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">        leaderboard<\/span><span style=\"color: #ECEFF4\">.<\/span><span style=\"color: #88C0D0\">insert<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">pos<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> entry<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\">  <\/span><span style=\"color: #616E88\"># O(n) insert<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">add_score<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">leaderboard<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">alice<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">120<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">add_score<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">leaderboard<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">bob<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">150<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #88C0D0\">add_score<\/span><span style=\"color: #ECEFF4\">(<\/span><span style=\"color: #D8DEE9FF\">leaderboard<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">charlie<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">130<\/span><span style=\"color: #ECEFF4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># convert back to readable format<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">readable <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #ECEFF4\">&#91;(<\/span><span style=\"color: #D8DEE9FF\">user<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">-<\/span><span style=\"color: #D8DEE9FF\">score<\/span><span style=\"color: #ECEFF4\">)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">for<\/span><span style=\"color: #D8DEE9FF\"> score<\/span><span style=\"color: #ECEFF4\">,<\/span><span style=\"color: #D8DEE9FF\"> user <\/span><span style=\"color: #81A1C1\">in<\/span><span style=\"color: #D8DEE9FF\"> leaderboard<\/span><span style=\"color: #ECEFF4\">&#93;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Gi\u1ea3i th\u00edch:<\/strong> \u0111\u00e2y l\u00e0 minh h\u1ecda \u0111\u01a1n gi\u1ea3n; n\u1ebfu c\u1ea7n hi\u1ec7u n\u0103ng l\u1edbn, d\u00f9ng specialized sorted data structure.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. \u0110\u1ed9 ph\u1ee9c t\u1ea1p (time &amp; space) \u2014 so s\u00e1nh nhanh<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Operation \/ Approach<\/th><th>Time complexity<\/th><th>Space complexity<\/th><\/tr><\/thead><tbody><tr><td><code>sorted(my_set)<\/code><\/td><td>O(n log n)<\/td><td>O(n)<\/td><\/tr><tr><td><code>tuple(sorted(my_set))<\/code><\/td><td>O(n log n)<\/td><td>O(n)<\/td><\/tr><tr><td><code>bisect.insort<\/code> (list)<\/td><td>find O(log n), insert O(n)<\/td><td>O(n)<\/td><\/tr><tr><td><code>heapq.nlargest(k, set)<\/code><\/td><td>O(n log k)<\/td><td>O(k)<\/td><\/tr><tr><td>SortedSet (third-party)<\/td><td>typically O(log n) inserts<\/td><td>O(n)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Gi\u1ea3i th\u00edch:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>sorted()<\/code> l\u00e0 l\u1ef1a ch\u1ecdn \u0111\u01a1n gi\u1ea3n, chi ph\u00ed O(n log n) cho m\u1ed7i l\u1ea7n s\u1eafp to\u00e0n b\u1ed9.<\/li>\n\n\n\n<li>N\u1ebfu ch\u1ec9 c\u1ea7n top-k th\u00ec <code>heapq.nlargest<\/code> hi\u1ec7u qu\u1ea3 h\u01a1n.<\/li>\n\n\n\n<li>Duy tr\u00ec th\u1ee9 t\u1ef1 khi c\u1eadp nh\u1eadt th\u01b0\u1eddng y\u00eau c\u1ea7u c\u1ea5u tr\u00fac chuy\u00ean d\u1ee5ng \u0111\u1ec3 tr\u00e1nh O(n) ch\u00e8n.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">7. Nh\u1eefng sai l\u1ea7m th\u01b0\u1eddng g\u1eb7p khi s\u1eafp x\u1ebfp Set (v\u00e0 c\u00e1ch tr\u00e1nh)<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>C\u1ed1 g\u1eafng g\u1ecdi <code>.sort()<\/code> tr\u00ean set<\/strong> \u2192 <code>AttributeError<\/code>.<br><em>Fix:<\/em> d\u00f9ng <code>sorted()<\/code> ho\u1eb7c chuy\u1ec3n th\u00e0nh list.<\/li>\n\n\n\n<li><strong>G\u00e1n k\u1ebft qu\u1ea3 <code>sorted()<\/code> tr\u1edf l\u1ea1i th\u00e0nh set<\/strong> \u2192 m\u1ea5t th\u1ee9 t\u1ef1. <code>my_set = {3,1,2} my_set = set(sorted(my_set)) # WRONG: set() will lose order<\/code> <em>Fix:<\/em> l\u01b0u v\u00e0o list\/tuple n\u1ebfu c\u1ea7n gi\u1eef th\u1ee9 t\u1ef1: <code>sorted_list = sorted(my_set)<\/code>.<\/li>\n\n\n\n<li><strong>Qu\u00ean r\u1eb1ng converting costs O(n)<\/strong> \u2014 nhi\u1ec1u l\u1ea7n chuy\u1ec3n \u0111\u1ed5i trong v\u00f2ng l\u1eb7p g\u00e2y overhead l\u1edbn.<br><em>Fix:<\/em> n\u1ebfu c\u1ea7n thao t\u00e1c nhi\u1ec1u l\u1ea7n, chuy\u1ec3n 1 l\u1ea7n v\u00e0 reuse sorted list, ho\u1eb7c d\u00f9ng data structure duy tr\u00ec s\u1eafp th\u1eb3ng.<\/li>\n\n\n\n<li><strong>Kh\u00f4ng quan t\u00e2m stability khi sort keys b\u1eb1ng nhau<\/strong> \u2014 v\u1edbi set ph\u1ea7n t\u1eed duy nh\u1ea5t, nh\u01b0ng n\u1ebfu key b\u1eb1ng nhau (v\u00ed d\u1ee5 sort theo length c\u1ee7a strings), stable sort gi\u1eef th\u1ee9 t\u1ef1 nh\u01b0ng v\u00ec set unordered n\u00ean th\u1ee9 t\u1ef1 g\u1ed1c kh\u00f4ng x\u00e1c \u0111\u1ecbnh.<br><em>Gi\u1ea3i th\u00edch:<\/em> n\u1ebfu mu\u1ed1n \u1ed5n \u0111\u1ecbnh d\u1ef1a tr\u00ean th\u1ee9 t\u1ef1 ch\u00e8n, ph\u1ea3i s\u1eed d\u1ee5ng c\u1ea5u tr\u00fac gi\u1eef th\u1ee9 t\u1ef1 ch\u00e8n (e.g. <code>list<\/code> ho\u1eb7c <code>dict<\/code> trong Python 3.7+).<\/li>\n\n\n\n<li><strong>D\u00f9ng thu\u1eadt to\u00e1n O(n\u00b2) (v\u00ed d\u1ee5 ch\u00e8n b\u1eb1ng list insert \u1edf \u0111\u1ea7u) cho d\u1eef li\u1ec7u l\u1edbn<\/strong> \u2192 ch\u1eadm.<br><em>Fix:<\/em> ch\u1ecdn gi\u1ea3i ph\u00e1p O(n log n) ho\u1eb7c data structure ph\u00f9 h\u1ee3p.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">8. K\u1ebft lu\u1eadn<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Set l\u00e0 unordered<\/strong> \u2192 kh\u00f4ng th\u1ec3 s\u1eafp tr\u1ef1c ti\u1ebfp; \u201cs\u1eafp set\u201d = t\u1ea1o <strong>bi\u1ec3u di\u1ec5n c\u00f3 th\u1ee9 t\u1ef1<\/strong> (list\/tuple) t\u1eeb set.<\/li>\n\n\n\n<li><strong><code>sorted()<\/code><\/strong> l\u00e0 c\u00f4ng c\u1ee5 m\u1ea1nh m\u1ebd v\u00e0 \u0111\u00fang \u0111\u1eafn cho h\u1ea7u h\u1ebft nhu c\u1ea7u: nhanh, \u1ed5n \u0111\u1ecbnh v\u00e0 d\u1ec5 d\u00f9ng. Tuy nhi\u00ean n\u00f3 tr\u1ea3 v\u1ec1 list (sao ch\u00e9p d\u1eef li\u1ec7u) \u2014 \u1ea3nh h\u01b0\u1edfng b\u1ed9 nh\u1edb n\u1ebfu dataset l\u1edbn.<\/li>\n\n\n\n<li>N\u1ebfu c\u1ea7n <strong>top-k<\/strong>, d\u00f9ng <code>heapq.nlargest<\/code> \u0111\u1ec3 ti\u1ebft ki\u1ec7m th\u1eddi gian.<\/li>\n\n\n\n<li>N\u1ebfu c\u1ea7n <strong>duy tr\u00ec th\u1ee9 t\u1ef1 khi c\u1eadp nh\u1eadt th\u01b0\u1eddng xuy\u00ean<\/strong>, d\u00f9ng <code>bisect<\/code> + list (\u0111\u01a1n gi\u1ea3n) ho\u1eb7c th\u01b0 vi\u1ec7n chuy\u00ean d\u1ee5ng nh\u01b0 <code>sortedcontainers.SortedSet<\/code> (n\u1ebfu performance quan tr\u1ecdng).<\/li>\n\n\n\n<li>Tr\u00e1nh c\u00e1c sai l\u1ea7m: g\u1ecdi <code>.sort()<\/code> tr\u00ean set, g\u00e1n l\u1ea1i v\u00e0o set l\u00e0m m\u1ea5t order, chuy\u1ec3n \u0111\u1ed5i nhi\u1ec1u l\u1ea7n kh\u00f4ng c\u1ea7n thi\u1ebft.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">M\u00ecnh khuy\u00ean: v\u1edbi c\u00e1c t\u00e1c v\u1ee5 th\u00f4ng th\u01b0\u1eddng, <strong>d\u00f9ng <code>sorted()<\/code><\/strong>; khi y\u00eau c\u1ea7u hi\u1ec7u n\u0103ng \u1edf m\u1ee9c cao ho\u1eb7c c\u1eadp nh\u1eadt li\u00ean t\u1ee5c, <strong>\u0111\u1ea7u t\u01b0 v\u00e0o c\u1ea5u tr\u00fac d\u1eef li\u1ec7u ph\u00f9 h\u1ee3p<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">9. T\u00e0i li\u1ec7u tham kh\u1ea3o<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Python Software Foundation. <em>Built-in Functions \u2014 sorted()<\/em>. Python Documentation.<\/li>\n\n\n\n<li>Python Software Foundation. <em>bisect \u2014 Array bisection algorithm<\/em>. Python Standard Library docs.<\/li>\n\n\n\n<li>Python Software Foundation. <em>heapq \u2014 Heap queue algorithm<\/em>. Python Standard Library docs.<\/li>\n\n\n\n<li>(Optional) <code>sortedcontainers<\/code> project \u2014 a high-performance pure-Python sorted collections library.<\/li>\n\n\n\n<li>Python for Professionals: Learning Python as a Second Language: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710002222624882405978&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fpython-for-professionals-3\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/python-for-professionals-3<\/a><\/li>\n\n\n\n<li>Python: Deeper Insights into Machine Learning: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710015810095319857183&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fpython-deeper-insights-into-machine-learning\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/python-deeper-insights-into-machine-learning<\/a><\/li>\n\n\n\n<li>DataFusion Python Bindings in Practice: The Complete Guide for Developers and Engineers: <a href=\"https:\/\/click.linksynergy.com\/link?id=*C\/UgjGtUZ8&amp;offerid=1562891.3721710049093362364820452&amp;type=15&amp;murl=https%3A%2F%2Fwww.kobo.com%2Fus%2Fen%2Febook%2Fdatafusion-python-bindings-in-practice\" target=\"_blank\" rel=\"noopener\">https:\/\/www.kobo.com\/us\/en\/ebook\/datafusion-python-bindings-in-practice<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Set l\u00e0 m\u1ed9t c\u1ea5u tr\u00fac r\u1ea5t h\u1eefu \u00edch: l\u01b0u ph\u1ea7n t\u1eed duy nh\u1ea5t, h\u1ed7 tr\u1ee3 ki\u1ec3m tra membership O(1) trung b\u00ecnh, ph\u00f9 h\u1ee3p khi ta quan t\u00e2m \u201cc\u00f3 hay kh\u00f4ng\u201d ch\u1ee9 kh\u00f4ng ph\u1ea3i \u201c\u1edf v\u1ecb tr\u00ed n\u00e0o\u201d. Tuy nhi\u00ean v\u00ec set l\u00e0 unordered, ta kh\u00f4ng th\u1ec3 &#8220;s\u1eafp x\u1ebfp tr\u1ef1c ti\u1ebfp&#8221; tr\u00ean set nh\u01b0 list. Khi&#8230;<\/p>\n","protected":false},"author":1,"featured_media":824,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"googlesitekit_rrm_CAowieHDDA:productID":"","footnotes":""},"categories":[41],"tags":[40],"class_list":["post-1616","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kien-thuc-lap-trinh","tag-python-co-ban"],"_links":{"self":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/comments?post=1616"}],"version-history":[{"count":2,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1616\/revisions"}],"predecessor-version":[{"id":2462,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/posts\/1616\/revisions\/2462"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/media\/824"}],"wp:attachment":[{"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/media?parent=1616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/categories?post=1616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kienthucmo.com\/vi\/wp-json\/wp\/v2\/tags?post=1616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}