Skip to content

Commit 1f385de

Browse files
Add solutions for problems 3667, 3668, 3669, 3670, and 3671
- Implemented sorting by absolute value in C++, Go, Java, Python, Rust, and TypeScript for problem 3667. - Developed a solution to restore finishing order for friends in C++, Go, Java, Python, and TypeScript for problem 3668. - Created a balanced K-factor decomposition solution in C++, Go, Java, Python, and TypeScript for problem 3669. - Added a problem statement for maximum product of two integers with no common bits (3670) without a solution yet. - Introduced a problem statement for the sum of beautiful subsequences (3671) without a solution yet.
1 parent 82bc104 commit 1f385de

File tree

32 files changed

+1982
-0
lines changed

32 files changed

+1982
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
use std::cmp::Ordering;
2+
use std::collections::BinaryHeap;
3+
4+
impl Solution {
5+
pub fn max_average_ratio(classes: Vec<Vec<i32>>, extra_students: i32) -> f64 {
6+
struct Node {
7+
gain: f64,
8+
a: i32,
9+
b: i32,
10+
}
11+
12+
impl PartialEq for Node {
13+
fn eq(&self, other: &Self) -> bool {
14+
self.gain == other.gain
15+
}
16+
}
17+
impl Eq for Node {}
18+
19+
impl PartialOrd for Node {
20+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
21+
self.gain.partial_cmp(&other.gain)
22+
}
23+
}
24+
impl Ord for Node {
25+
fn cmp(&self, other: &Self) -> Ordering {
26+
self.partial_cmp(other).unwrap()
27+
}
28+
}
29+
30+
fn calc_gain(a: i32, b: i32) -> f64 {
31+
(a + 1) as f64 / (b + 1) as f64 - a as f64 / b as f64
32+
}
33+
34+
let n = classes.len() as f64;
35+
let mut pq = BinaryHeap::new();
36+
37+
for c in classes {
38+
let a = c[0];
39+
let b = c[1];
40+
pq.push(Node {
41+
gain: calc_gain(a, b),
42+
a,
43+
b,
44+
});
45+
}
46+
47+
let mut extra = extra_students;
48+
while extra > 0 {
49+
if let Some(mut node) = pq.pop() {
50+
node.a += 1;
51+
node.b += 1;
52+
node.gain = calc_gain(node.a, node.b);
53+
pq.push(node);
54+
}
55+
extra -= 1;
56+
}
57+
58+
let mut sum = 0.0;
59+
while let Some(node) = pq.pop() {
60+
sum += node.a as f64 / node.b as f64;
61+
}
62+
63+
sum / n
64+
}
65+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function maxAverageRatio(classes: number[][], extraStudents: number): number {
2+
function calcGain(a: number, b: number): number {
3+
return (a + 1) / (b + 1) - a / b;
4+
}
5+
const pq = new PriorityQueue<[number, number]>(
6+
(p, q) => calcGain(q[0], q[1]) - calcGain(p[0], p[1]),
7+
);
8+
for (const [a, b] of classes) {
9+
pq.enqueue([a, b]);
10+
}
11+
while (extraStudents-- > 0) {
12+
const item = pq.dequeue();
13+
const [a, b] = item;
14+
pq.enqueue([a + 1, b + 1]);
15+
}
16+
let ans = 0;
17+
while (!pq.isEmpty()) {
18+
const item = pq.dequeue()!;
19+
const [a, b] = item;
20+
ans += a / b;
21+
}
22+
return ans / classes.length;
23+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
---
2+
comments: true
3+
difficulty: Easy
4+
---
5+
6+
<!-- problem:start -->
7+
8+
# [3663. Find The Least Frequent Digit](https://leetcode.com/problems/find-the-least-frequent-digit)
9+
10+
[中文文档](/solution/3600-3699/3663.Find%20The%20Least%20Frequent%20Digit/README.md)
11+
12+
## Description
13+
14+
<!-- description:start -->
15+
16+
<p>Given an integer <code>n</code>, find the digit that occurs <strong>least</strong> frequently in its decimal representation. If multiple digits have the same frequency, choose the <strong>smallest</strong> digit.</p>
17+
18+
<p>Return the chosen digit as an integer.</p>
19+
The <strong>frequency</strong> of a digit <code>x</code> is the number of times it appears in the decimal representation of <code>n</code>.
20+
<p>&nbsp;</p>
21+
<p><strong class="example">Example 1:</strong></p>
22+
23+
<div class="example-block">
24+
<p><strong>Input:</strong> <span class="example-io">n = 1553322</span></p>
25+
26+
<p><strong>Output:</strong> 1</p>
27+
28+
<p><strong>Explanation:</strong></p>
29+
30+
<p>The least frequent digit in <code>n</code> is 1, which appears only once. All other digits appear twice.</p>
31+
</div>
32+
33+
<p><strong class="example">Example 2:</strong></p>
34+
35+
<div class="example-block">
36+
<p><strong>Input:</strong> <span class="example-io">n = 723344511</span></p>
37+
38+
<p><strong>Output:</strong> 2</p>
39+
40+
<p><strong>Explanation:</strong></p>
41+
42+
<p>The least frequent digits in <code>n</code> are 7, 2, and 5; each appears only once.</p>
43+
</div>
44+
45+
<p>&nbsp;</p>
46+
<p><strong>Constraints:</strong></p>
47+
48+
<ul>
49+
<li><code>1 &lt;= n &lt;= 2<sup>31</sup>​​​​​​​ - 1</code></li>
50+
</ul>
51+
52+
<!-- description:end -->
53+
54+
## Solutions
55+
56+
<!-- solution:start -->
57+
58+
### Solution 1: Counting
59+
60+
We use an array $\textit{cnt}$ to count the frequency of each digit. We iterate through each digit of the number $n$ and update the $\textit{cnt}$ array.
61+
62+
Then, we use a variable $f$ to record the current lowest frequency among the digits, and a variable $\textit{ans}$ to record the corresponding digit.
63+
64+
Next, we iterate through the $\textit{cnt}$ array. If $0 < \textit{cnt}[x] < f$, it means we have found a digit with a lower frequency, so we update $f = \textit{cnt}[x]$ and $\textit{ans} = x$.
65+
66+
After the iteration, we return $\textit{ans}$ as the answer.
67+
68+
The time complexity is $O(\log n)$, and the space complexity is $O(1)$.
69+
70+
<!-- tabs:start -->
71+
72+
#### Python3
73+
74+
```python
75+
class Solution:
76+
def getLeastFrequentDigit(self, n: int) -> int:
77+
cnt = [0] * 10
78+
while n:
79+
n, x = divmod(n, 10)
80+
cnt[x] += 1
81+
ans, f = 0, inf
82+
for x, v in enumerate(cnt):
83+
if 0 < v < f:
84+
f = v
85+
ans = x
86+
return ans
87+
```
88+
89+
#### Java
90+
91+
```java
92+
class Solution {
93+
public int getLeastFrequentDigit(int n) {
94+
int[] cnt = new int[10];
95+
for (; n > 0; n /= 10) {
96+
++cnt[n % 10];
97+
}
98+
int ans = 0, f = 1 << 30;
99+
for (int x = 0; x < 10; ++x) {
100+
if (cnt[x] > 0 && cnt[x] < f) {
101+
f = cnt[x];
102+
ans = x;
103+
}
104+
}
105+
return ans;
106+
}
107+
}
108+
```
109+
110+
#### C++
111+
112+
```cpp
113+
class Solution {
114+
public:
115+
int getLeastFrequentDigit(int n) {
116+
int cnt[10]{};
117+
for (; n > 0; n /= 10) {
118+
++cnt[n % 10];
119+
}
120+
int ans = 0, f = 1 << 30;
121+
for (int x = 0; x < 10; ++x) {
122+
if (cnt[x] > 0 && cnt[x] < f) {
123+
f = cnt[x];
124+
ans = x;
125+
}
126+
}
127+
return ans;
128+
}
129+
};
130+
```
131+
132+
#### Go
133+
134+
```go
135+
func getLeastFrequentDigit(n int) (ans int) {
136+
cnt := [10]int{}
137+
for ; n > 0; n /= 10 {
138+
cnt[n%10]++
139+
}
140+
f := 1 << 30
141+
for x, v := range cnt {
142+
if v > 0 && v < f {
143+
f = v
144+
ans = x
145+
}
146+
}
147+
return
148+
}
149+
```
150+
151+
#### TypeScript
152+
153+
```ts
154+
function getLeastFrequentDigit(n: number): number {
155+
const cnt: number[] = Array(10).fill(0);
156+
for (; n; n = (n / 10) | 0) {
157+
cnt[n % 10]++;
158+
}
159+
let [ans, f] = [0, Number.MAX_SAFE_INTEGER];
160+
for (let x = 0; x < 10; ++x) {
161+
if (cnt[x] > 0 && cnt[x] < f) {
162+
f = cnt[x];
163+
ans = x;
164+
}
165+
}
166+
return ans;
167+
}
168+
```
169+
170+
<!-- tabs:end -->
171+
172+
<!-- solution:end -->
173+
174+
<!-- problem:end -->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int getLeastFrequentDigit(int n) {
4+
int cnt[10]{};
5+
for (; n > 0; n /= 10) {
6+
++cnt[n % 10];
7+
}
8+
int ans = 0, f = 1 << 30;
9+
for (int x = 0; x < 10; ++x) {
10+
if (cnt[x] > 0 && cnt[x] < f) {
11+
f = cnt[x];
12+
ans = x;
13+
}
14+
}
15+
return ans;
16+
}
17+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func getLeastFrequentDigit(n int) (ans int) {
2+
cnt := [10]int{}
3+
for ; n > 0; n /= 10 {
4+
cnt[n%10]++
5+
}
6+
f := 1 << 30
7+
for x, v := range cnt {
8+
if v > 0 && v < f {
9+
f = v
10+
ans = x
11+
}
12+
}
13+
return
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int getLeastFrequentDigit(int n) {
3+
int[] cnt = new int[10];
4+
for (; n > 0; n /= 10) {
5+
++cnt[n % 10];
6+
}
7+
int ans = 0, f = 1 << 30;
8+
for (int x = 0; x < 10; ++x) {
9+
if (cnt[x] > 0 && cnt[x] < f) {
10+
f = cnt[x];
11+
ans = x;
12+
}
13+
}
14+
return ans;
15+
}
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def getLeastFrequentDigit(self, n: int) -> int:
3+
cnt = [0] * 10
4+
while n:
5+
n, x = divmod(n, 10)
6+
cnt[x] += 1
7+
ans, f = 0, inf
8+
for x, v in enumerate(cnt):
9+
if 0 < v < f:
10+
f = v
11+
ans = x
12+
return ans
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function getLeastFrequentDigit(n: number): number {
2+
const cnt: number[] = Array(10).fill(0);
3+
for (; n; n = (n / 10) | 0) {
4+
cnt[n % 10]++;
5+
}
6+
let [ans, f] = [0, Number.MAX_SAFE_INTEGER];
7+
for (let x = 0; x < 10; ++x) {
8+
if (cnt[x] > 0 && cnt[x] < f) {
9+
f = cnt[x];
10+
ans = x;
11+
}
12+
}
13+
return ans;
14+
}

0 commit comments

Comments
 (0)