Skip to content

feat: add solutions to lc problems: No.0506,0507 #3823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions solution/0500-0599/0506.Relative Ranks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:排序

我们使用一个数组 $\textit{idx}$ 存储 $0$ 到 $n-1$ 的下标,然后对 $\textit{idx}$ 进行排序,排序规则为:按照 $\textit{score}$ 的值从大到小排序。

然后我们定义一个数组 $\textit{top3} = [\text{Gold Medal}, \text{Silver Medal}, \text{Bronze Medal}]$,遍历 $\textit{idx}$,对于每个下标 $j$,如果 $j$ 小于 $3$,则 $\textit{ans}[j]$ 为 $\textit{top3}[j]$,否则为 $j+1$。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{score}$ 的长度。

<!-- tabs:start -->

Expand All @@ -77,10 +83,10 @@ class Solution:
n = len(score)
idx = list(range(n))
idx.sort(key=lambda x: -score[x])
top3 = ['Gold Medal', 'Silver Medal', 'Bronze Medal']
top3 = ["Gold Medal", "Silver Medal", "Bronze Medal"]
ans = [None] * n
for i in range(n):
ans[idx[i]] = top3[i] if i < 3 else str(i + 1)
for i, j in enumerate(idx):
ans[j] = top3[i] if i < 3 else str(i + 1)
return ans
```

Expand Down Expand Up @@ -112,15 +118,16 @@ class Solution {
public:
vector<string> findRelativeRanks(vector<int>& score) {
int n = score.size();
vector<pair<int, int>> idx;
for (int i = 0; i < n; ++i)
idx.push_back(make_pair(score[i], i));
sort(idx.begin(), idx.end(),
[&](const pair<int, int>& x, const pair<int, int>& y) { return x.first > y.first; });
vector<int> idx(n);
iota(idx.begin(), idx.end(), 0);
sort(idx.begin(), idx.end(), [&score](int a, int b) {
return score[a] > score[b];
});
vector<string> ans(n);
vector<string> top3 = {"Gold Medal", "Silver Medal", "Bronze Medal"};
for (int i = 0; i < n; ++i)
ans[idx[i].second] = i < 3 ? top3[i] : to_string(i + 1);
for (int i = 0; i < n; ++i) {
ans[idx[i]] = i < 3 ? top3[i] : to_string(i + 1);
}
return ans;
}
};
Expand Down Expand Up @@ -151,6 +158,26 @@ func findRelativeRanks(score []int) []string {
}
```

#### TypeScript

```ts
function findRelativeRanks(score: number[]): string[] {
const n = score.length;
const idx = Array.from({ length: n }, (_, i) => i);
idx.sort((a, b) => score[b] - score[a]);
const top3 = ['Gold Medal', 'Silver Medal', 'Bronze Medal'];
const ans: string[] = Array(n);
for (let i = 0; i < n; i++) {
if (i < 3) {
ans[idx[i]] = top3[i];
} else {
ans[idx[i]] = (i + 1).toString();
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
49 changes: 38 additions & 11 deletions solution/0500-0599/0506.Relative Ranks/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Sorting

We use an array $\textit{idx}$ to store the indices from $0$ to $n-1$, then sort $\textit{idx}$ based on the values in $\textit{score}$ in descending order.

Next, we define an array $\textit{top3} = [\text{Gold Medal}, \text{Silver Medal}, \text{Bronze Medal}]$. We traverse $\textit{idx}$, and for each index $j$, if $j$ is less than $3$, then $\textit{ans}[j]$ is $\textit{top3}[j]$; otherwise, it is $j+1$.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{score}$.

<!-- tabs:start -->

Expand All @@ -76,10 +82,10 @@ class Solution:
n = len(score)
idx = list(range(n))
idx.sort(key=lambda x: -score[x])
top3 = ['Gold Medal', 'Silver Medal', 'Bronze Medal']
top3 = ["Gold Medal", "Silver Medal", "Bronze Medal"]
ans = [None] * n
for i in range(n):
ans[idx[i]] = top3[i] if i < 3 else str(i + 1)
for i, j in enumerate(idx):
ans[j] = top3[i] if i < 3 else str(i + 1)
return ans
```

Expand Down Expand Up @@ -111,15 +117,16 @@ class Solution {
public:
vector<string> findRelativeRanks(vector<int>& score) {
int n = score.size();
vector<pair<int, int>> idx;
for (int i = 0; i < n; ++i)
idx.push_back(make_pair(score[i], i));
sort(idx.begin(), idx.end(),
[&](const pair<int, int>& x, const pair<int, int>& y) { return x.first > y.first; });
vector<int> idx(n);
iota(idx.begin(), idx.end(), 0);
sort(idx.begin(), idx.end(), [&score](int a, int b) {
return score[a] > score[b];
});
vector<string> ans(n);
vector<string> top3 = {"Gold Medal", "Silver Medal", "Bronze Medal"};
for (int i = 0; i < n; ++i)
ans[idx[i].second] = i < 3 ? top3[i] : to_string(i + 1);
for (int i = 0; i < n; ++i) {
ans[idx[i]] = i < 3 ? top3[i] : to_string(i + 1);
}
return ans;
}
};
Expand Down Expand Up @@ -150,6 +157,26 @@ func findRelativeRanks(score []int) []string {
}
```

#### TypeScript

```ts
function findRelativeRanks(score: number[]): string[] {
const n = score.length;
const idx = Array.from({ length: n }, (_, i) => i);
idx.sort((a, b) => score[b] - score[a]);
const top3 = ['Gold Medal', 'Silver Medal', 'Bronze Medal'];
const ans: string[] = Array(n);
for (let i = 0; i < n; i++) {
if (i < 3) {
ans[idx[i]] = top3[i];
} else {
ans[idx[i]] = (i + 1).toString();
}
}
return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
17 changes: 9 additions & 8 deletions solution/0500-0599/0506.Relative Ranks/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ class Solution {
public:
vector<string> findRelativeRanks(vector<int>& score) {
int n = score.size();
vector<pair<int, int>> idx;
for (int i = 0; i < n; ++i)
idx.push_back(make_pair(score[i], i));
sort(idx.begin(), idx.end(),
[&](const pair<int, int>& x, const pair<int, int>& y) { return x.first > y.first; });
vector<int> idx(n);
iota(idx.begin(), idx.end(), 0);
sort(idx.begin(), idx.end(), [&score](int a, int b) {
return score[a] > score[b];
});
vector<string> ans(n);
vector<string> top3 = {"Gold Medal", "Silver Medal", "Bronze Medal"};
for (int i = 0; i < n; ++i)
ans[idx[i].second] = i < 3 ? top3[i] : to_string(i + 1);
for (int i = 0; i < n; ++i) {
ans[idx[i]] = i < 3 ? top3[i] : to_string(i + 1);
}
return ans;
}
};
};
6 changes: 3 additions & 3 deletions solution/0500-0599/0506.Relative Ranks/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ def findRelativeRanks(self, score: List[int]) -> List[str]:
n = len(score)
idx = list(range(n))
idx.sort(key=lambda x: -score[x])
top3 = ['Gold Medal', 'Silver Medal', 'Bronze Medal']
top3 = ["Gold Medal", "Silver Medal", "Bronze Medal"]
ans = [None] * n
for i in range(n):
ans[idx[i]] = top3[i] if i < 3 else str(i + 1)
for i, j in enumerate(idx):
ans[j] = top3[i] if i < 3 else str(i + 1)
return ans
15 changes: 15 additions & 0 deletions solution/0500-0599/0506.Relative Ranks/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function findRelativeRanks(score: number[]): string[] {
const n = score.length;
const idx = Array.from({ length: n }, (_, i) => i);
idx.sort((a, b) => score[b] - score[a]);
const top3 = ['Gold Medal', 'Silver Medal', 'Bronze Medal'];
const ans: string[] = Array(n);
for (let i = 0; i < n; i++) {
if (i < 3) {
ans[idx[i]] = top3[i];
} else {
ans[idx[i]] = (i + 1).toString();
}
}
return ans;
}
51 changes: 41 additions & 10 deletions solution/0500-0599/0507.Perfect Number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:枚举

我们首先判断 $\textit{num}$ 是否为 1,如果为 1,则 $\textit{num}$ 不是完美数,返回 $\text{false}$。

然后,我们从 2 开始枚举 $\textit{num}$ 的所有正因子,如果 $\textit{num}$ 能被 $\textit{num}$ 的某个正因子 $i$ 整除,那么我们将 $i$ 加入到答案 $\textit{s}$ 中。如果 $\textit{num}$ 除以 $i$ 得到的商不等于 $i$,我们也将 $\textit{num}$ 除以 $i$ 得到的商加入到答案 $\textit{s}$ 中。

最后,我们判断 $\textit{s}$ 是否等于 $\textit{num}$ 即可。

时间复杂度 $O(\sqrt{n})$,其中 $n$ 为 $\textit{num}$ 的大小。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -63,7 +71,7 @@ class Solution:
if num == 1:
return False
s, i = 1, 2
while i * i <= num:
while i <= num // i:
if num % i == 0:
s += i
if i != num // i:
Expand All @@ -76,13 +84,12 @@ class Solution:

```java
class Solution {

public boolean checkPerfectNumber(int num) {
if (num == 1) {
return false;
}
int s = 1;
for (int i = 2; i * i <= num; ++i) {
for (int i = 2; i <= num / i; ++i) {
if (num % i == 0) {
s += i;
if (i != num / i) {
Expand All @@ -101,12 +108,16 @@ class Solution {
class Solution {
public:
bool checkPerfectNumber(int num) {
if (num == 1) return false;
if (num == 1) {
return false;
}
int s = 1;
for (int i = 2; i * i <= num; ++i) {
for (int i = 2; i <= num / i; ++i) {
if (num % i == 0) {
s += i;
if (i != num / i) s += num / i;
if (i != num / i) {
s += num / i;
}
}
}
return s == num;
Expand All @@ -122,18 +133,38 @@ func checkPerfectNumber(num int) bool {
return false
}
s := 1
for i := 2; i*i <= num; i++ {
for i := 2; i <= num/i; i++ {
if num%i == 0 {
s += i
if i != num/i {
s += num / i
if j := num / i; i != j {
s += j
}
}
}
return s == num
}
```

#### TypeScript

```ts
function checkPerfectNumber(num: number): boolean {
if (num <= 1) {
return false;
}
let s = 1;
for (let i = 2; i <= num / i; ++i) {
if (num % i === 0) {
s += i;
if (i * i !== num) {
s += num / i;
}
}
}
return s === num;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Loading