Skip to content

Commit 3eba49d

Browse files
committed
deploy: 7fd03bc
1 parent 5eee245 commit 3eba49d

File tree

11 files changed

+170
-116
lines changed

11 files changed

+170
-116
lines changed

en/lc/763/index.html

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24245,9 +24245,9 @@
2424524245
<ul class="md-nav__list">
2424624246

2424724247
<li class="md-nav__item">
24248-
<a href="#solution-1" class="md-nav__link">
24248+
<a href="#solution-1-greedy" class="md-nav__link">
2424924249
<span class="md-ellipsis">
24250-
Solution 1
24250+
Solution 1: Greedy
2425124251
</span>
2425224252
</a>
2425324253

@@ -86532,7 +86532,14 @@ <h2 id="description">Description</h2>
8653286532
<h2 id="solutions">Solutions</h2>
8653386533
<!-- solution:start -->
8653486534

86535-
<h3 id="solution-1">Solution 1</h3>
86535+
<h3 id="solution-1-greedy">Solution 1: Greedy</h3>
86536+
<p>We first use an array or hash table <span class="arithmatex">\(\textit{last}\)</span> to record the last occurrence of each letter in the string <span class="arithmatex">\(s\)</span>.</p>
86537+
<p>Next, we use a greedy approach to partition the string into as many segments as possible.</p>
86538+
<p>Traverse the string <span class="arithmatex">\(s\)</span> from left to right, while maintaining the start index <span class="arithmatex">\(j\)</span> and end index <span class="arithmatex">\(i\)</span> of the current segment, both initially set to <span class="arithmatex">\(0\)</span>.</p>
86539+
<p>For each letter <span class="arithmatex">\(c\)</span> visited, get the last occurrence position <span class="arithmatex">\(\textit{last}[c]\)</span>. Since the end index of the current segment must not be less than <span class="arithmatex">\(\textit{last}[c]\)</span>, let <span class="arithmatex">\(\textit{mx} = \max(\textit{mx}, \textit{last}[c])\)</span>.</p>
86540+
<p>When visiting the index <span class="arithmatex">\(\textit{mx}\)</span>, it means the current segment ends. The index range of the current segment is <span class="arithmatex">\([j,.. i]\)</span>, and the length is <span class="arithmatex">\(i - j + 1\)</span>. We add this length to the result array. Then set <span class="arithmatex">\(j = i + 1\)</span> and continue to find the next segment.</p>
86541+
<p>Repeat the above process until the string traversal is complete to get the lengths of all segments.</p>
86542+
<p>Time complexity is <span class="arithmatex">\(O(n)\)</span>, and space complexity is <span class="arithmatex">\(O(|\Sigma|)\)</span>. Where <span class="arithmatex">\(n\)</span> is the length of the string <span class="arithmatex">\(s\)</span>, and <span class="arithmatex">\(|\Sigma|\)</span> is the size of the character set. In this problem, <span class="arithmatex">\(|\Sigma| = 26\)</span>.</p>
8653686543
<div class="tabbed-set tabbed-alternate" data-tabs="1:8"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><input id="__tabbed_1_7" name="__tabbed_1" type="radio" /><input id="__tabbed_1_8" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python3</label><label for="__tabbed_1_2">Java</label><label for="__tabbed_1_3">C++</label><label for="__tabbed_1_4">Go</label><label for="__tabbed_1_5">TypeScript</label><label for="__tabbed_1_6">Rust</label><label for="__tabbed_1_7">JavaScript</label><label for="__tabbed_1_8">C#</label></div>
8653786544
<div class="tabbed-content">
8653886545
<div class="tabbed-block">

en/lc/765/index.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24289,9 +24289,9 @@
2428924289
<ul class="md-nav__list">
2429024290

2429124291
<li class="md-nav__item">
24292-
<a href="#solution-1" class="md-nav__link">
24292+
<a href="#solution-1-union-find" class="md-nav__link">
2429324293
<span class="md-ellipsis">
24294-
Solution 1
24294+
Solution 1: Union-Find
2429524295
</span>
2429624296
</a>
2429724297

@@ -86539,7 +86539,12 @@ <h2 id="description">Description</h2>
8653986539
<h2 id="solutions">Solutions</h2>
8654086540
<!-- solution:start -->
8654186541

86542-
<h3 id="solution-1">Solution 1</h3>
86542+
<h3 id="solution-1-union-find">Solution 1: Union-Find</h3>
86543+
<p>We can assign a number to each pair of couples. Person with number <span class="arithmatex">\(0\)</span> and <span class="arithmatex">\(1\)</span> corresponds to couple <span class="arithmatex">\(0\)</span>, person with number <span class="arithmatex">\(2\)</span> and <span class="arithmatex">\(3\)</span> corresponds to couple <span class="arithmatex">\(1\)</span>, and so on. In other words, the person corresponding to <span class="arithmatex">\(row[i]\)</span> has a couple number of <span class="arithmatex">\(\lfloor \frac{row[i]}{2} \rfloor\)</span>.</p>
86544+
<p>If there are <span class="arithmatex">\(k\)</span> pairs of couples who are seated incorrectly with respect to each other, i.e., if <span class="arithmatex">\(k\)</span> pairs of couples are in the same permutation cycle, it will take <span class="arithmatex">\(k-1\)</span> swaps for all of them to be seated correctly.</p>
86545+
<p>Why? Consider the following: we first adjust the positions of a couple to their correct seats. After this, the problem reduces from <span class="arithmatex">\(k\)</span> couples to <span class="arithmatex">\(k-1\)</span> couples. This process continues, and when <span class="arithmatex">\(k = 1\)</span>, the number of swaps required is <span class="arithmatex">\(0\)</span>. Therefore, if <span class="arithmatex">\(k\)</span> pairs of couples are in the wrong positions, we need <span class="arithmatex">\(k-1\)</span> swaps.</p>
86546+
<p>Thus, we only need to traverse the array once, use union-find to determine how many permutation cycles there are. Suppose there are <span class="arithmatex">\(x\)</span> cycles, and the size of each cycle (in terms of couple pairs) is <span class="arithmatex">\(y_1, y_2, \cdots, y_x\)</span>. The number of swaps required is <span class="arithmatex">\(y_1-1 + y_2-1 + \cdots + y_x-1 = y_1 + y_2 + \cdots + y_x - x = n - x\)</span>.</p>
86547+
<p>The time complexity is <span class="arithmatex">\(O(n \times \alpha(n))\)</span>, and the space complexity is <span class="arithmatex">\(O(n)\)</span>, where <span class="arithmatex">\(\alpha(n)\)</span> is the inverse Ackermann function, which can be considered a very small constant.</p>
8654386548
<div class="tabbed-set tabbed-alternate" data-tabs="1:6"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python3</label><label for="__tabbed_1_2">Java</label><label for="__tabbed_1_3">C++</label><label for="__tabbed_1_4">Go</label><label for="__tabbed_1_5">TypeScript</label><label for="__tabbed_1_6">C#</label></div>
8654486549
<div class="tabbed-content">
8654586550
<div class="tabbed-block">

0 commit comments

Comments
 (0)