Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3ef2fee

Browse files
feat: add solutions to lc problems: No.3079,3080 (doocs#2454)
* No.3079.Find the Sum of Encrypted Integers * No.3080.Mark Elements on Array by Performing Queries
1 parent 6ba88a9 commit 3ef2fee

File tree

14 files changed

+683
-16
lines changed

14 files changed

+683
-16
lines changed

‎solution/3000-3099/3079.Find the Sum of Encrypted Integers/README.md‎

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,101 @@
4545

4646
## 解法
4747

48-
### 方法一
48+
### 方法一:模拟
49+
50+
我们直接模拟加密的过程,定义一个函数 $encrypt(x),ドル将一个整数 $x$ 中每一个数位都用 $x$ 中的最大数位替换。函数的实现如下:
51+
52+
我们可以通过不断地对 $x$ 取模和整除 10ドル$ 来得到 $x$ 的每一位数,找到最大的数位,记为 $mx$。在循环的过程中,我们还可以用一个变量 $p$ 来记录 $mx$ 的基础底数,即 $p = 1, 11, 111, \cdots$。最后返回 $mx \times p$ 即可。
53+
54+
时间复杂度 $O(n \times \log M),ドル其中 $n$ 是数组的长度,而 $M$ 是数组中元素的最大值。空间复杂度 $O(1)$。
4955

5056
<!-- tabs:start -->
5157

5258
```python
53-
59+
class Solution:
60+
def sumOfEncryptedInt(self, nums: List[int]) -> int:
61+
def encrypt(x: int) -> int:
62+
mx = p = 0
63+
while x:
64+
x, v = divmod(x, 10)
65+
mx = max(mx, v)
66+
p = p * 10 + 1
67+
return mx * p
68+
69+
return sum(encrypt(x) for x in nums)
5470
```
5571

5672
```java
57-
73+
class Solution {
74+
public int sumOfEncryptedInt(int[] nums) {
75+
int ans = 0;
76+
for (int x : nums) {
77+
ans += encrypt(x);
78+
}
79+
return ans;
80+
}
81+
82+
private int encrypt(int x) {
83+
int mx = 0, p = 0;
84+
for (; x > 0; x /= 10) {
85+
mx = Math.max(mx, x % 10);
86+
p = p * 10 + 1;
87+
}
88+
return mx * p;
89+
}
90+
}
5891
```
5992

6093
```cpp
61-
94+
class Solution {
95+
public:
96+
int sumOfEncryptedInt(vector<int>& nums) {
97+
auto encrypt = [&](int x) {
98+
int mx = 0, p = 0;
99+
for (; x; x /= 10) {
100+
mx = max(mx, x % 10);
101+
p = p * 10 + 1;
102+
}
103+
return mx * p;
104+
};
105+
int ans = 0;
106+
for (int x : nums) {
107+
ans += encrypt(x);
108+
}
109+
return ans;
110+
}
111+
};
62112
```
63113
64114
```go
115+
func sumOfEncryptedInt(nums []int) (ans int) {
116+
encrypt := func(x int) int {
117+
mx, p := 0, 0
118+
for ; x > 0; x /= 10 {
119+
mx = max(mx, x%10)
120+
p = p*10 + 1
121+
}
122+
return mx * p
123+
}
124+
for _, x := range nums {
125+
ans += encrypt(x)
126+
}
127+
return
128+
}
129+
```
65130

131+
```ts
132+
function sumOfEncryptedInt(nums: number[]): number {
133+
const encrypt = (x: number): number => {
134+
let [mx, p] = [0, 0];
135+
for (; x > 0; x = Math.floor(x / 10)) {
136+
mx = Math.max(mx, x % 10);
137+
p = p * 10 + 1;
138+
}
139+
return mx * p;
140+
};
141+
return nums.reduce((acc, x) => acc + encrypt(x), 0);
142+
}
66143
```
67144

68145
<!-- tabs:end -->

‎solution/3000-3099/3079.Find the Sum of Encrypted Integers/README_EN.md‎

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,101 @@
4141

4242
## Solutions
4343

44-
### Solution 1
44+
### Solution 1: Simulation
45+
46+
We directly simulate the encryption process by defining a function $encrypt(x),ドル which replaces each digit in an integer $x$ with the maximum digit in $x$. The implementation of the function is as follows:
47+
48+
We can obtain each digit of $x$ by continuously taking the modulus and integer division of $x$ by 10ドル,ドル and find the maximum digit, denoted as $mx$. During the loop, we can also use a variable $p$ to record the base number of $mx,ドル i.e., $p = 1, 11, 111, \cdots$. Finally, return $mx \times p$.
49+
50+
The time complexity is $O(n \times \log M),ドル where $n$ is the length of the array, and $M$ is the maximum value in the array. The space complexity is $O(1)$.
4551

4652
<!-- tabs:start -->
4753

4854
```python
49-
55+
class Solution:
56+
def sumOfEncryptedInt(self, nums: List[int]) -> int:
57+
def encrypt(x: int) -> int:
58+
mx = p = 0
59+
while x:
60+
x, v = divmod(x, 10)
61+
mx = max(mx, v)
62+
p = p * 10 + 1
63+
return mx * p
64+
65+
return sum(encrypt(x) for x in nums)
5066
```
5167

5268
```java
53-
69+
class Solution {
70+
public int sumOfEncryptedInt(int[] nums) {
71+
int ans = 0;
72+
for (int x : nums) {
73+
ans += encrypt(x);
74+
}
75+
return ans;
76+
}
77+
78+
private int encrypt(int x) {
79+
int mx = 0, p = 0;
80+
for (; x > 0; x /= 10) {
81+
mx = Math.max(mx, x % 10);
82+
p = p * 10 + 1;
83+
}
84+
return mx * p;
85+
}
86+
}
5487
```
5588

5689
```cpp
57-
90+
class Solution {
91+
public:
92+
int sumOfEncryptedInt(vector<int>& nums) {
93+
auto encrypt = [&](int x) {
94+
int mx = 0, p = 0;
95+
for (; x; x /= 10) {
96+
mx = max(mx, x % 10);
97+
p = p * 10 + 1;
98+
}
99+
return mx * p;
100+
};
101+
int ans = 0;
102+
for (int x : nums) {
103+
ans += encrypt(x);
104+
}
105+
return ans;
106+
}
107+
};
58108
```
59109
60110
```go
111+
func sumOfEncryptedInt(nums []int) (ans int) {
112+
encrypt := func(x int) int {
113+
mx, p := 0, 0
114+
for ; x > 0; x /= 10 {
115+
mx = max(mx, x%10)
116+
p = p*10 + 1
117+
}
118+
return mx * p
119+
}
120+
for _, x := range nums {
121+
ans += encrypt(x)
122+
}
123+
return
124+
}
125+
```
61126

127+
```ts
128+
function sumOfEncryptedInt(nums: number[]): number {
129+
const encrypt = (x: number): number => {
130+
let [mx, p] = [0, 0];
131+
for (; x > 0; x = Math.floor(x / 10)) {
132+
mx = Math.max(mx, x % 10);
133+
p = p * 10 + 1;
134+
}
135+
return mx * p;
136+
};
137+
return nums.reduce((acc, x) => acc + encrypt(x), 0);
138+
}
62139
```
63140

64141
<!-- tabs:end -->
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int sumOfEncryptedInt(vector<int>& nums) {
4+
auto encrypt = [&](int x) {
5+
int mx = 0, p = 0;
6+
for (; x; x /= 10) {
7+
mx = max(mx, x % 10);
8+
p = p * 10 + 1;
9+
}
10+
return mx * p;
11+
};
12+
int ans = 0;
13+
for (int x : nums) {
14+
ans += encrypt(x);
15+
}
16+
return ans;
17+
}
18+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func sumOfEncryptedInt(nums []int) (ans int) {
2+
encrypt := func(x int) int {
3+
mx, p := 0, 0
4+
for ; x > 0; x /= 10 {
5+
mx = max(mx, x%10)
6+
p = p*10 + 1
7+
}
8+
return mx * p
9+
}
10+
for _, x := range nums {
11+
ans += encrypt(x)
12+
}
13+
return
14+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int sumOfEncryptedInt(int[] nums) {
3+
int ans = 0;
4+
for (int x : nums) {
5+
ans += encrypt(x);
6+
}
7+
return ans;
8+
}
9+
10+
private int encrypt(int x) {
11+
int mx = 0, p = 0;
12+
for (; x > 0; x /= 10) {
13+
mx = Math.max(mx, x % 10);
14+
p = p * 10 + 1;
15+
}
16+
return mx * p;
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def sumOfEncryptedInt(self, nums: List[int]) -> int:
3+
def encrypt(x: int) -> int:
4+
mx = p = 0
5+
while x:
6+
x, v = divmod(x, 10)
7+
mx = max(mx, v)
8+
p = p * 10 + 1
9+
return mx * p
10+
11+
return sum(encrypt(x) for x in nums)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function sumOfEncryptedInt(nums: number[]): number {
2+
const encrypt = (x: number): number => {
3+
let [mx, p] = [0, 0];
4+
for (; x > 0; x = Math.floor(x / 10)) {
5+
mx = Math.max(mx, x % 10);
6+
p = p * 10 + 1;
7+
}
8+
return mx * p;
9+
};
10+
return nums.reduce((acc, x) => acc + encrypt(x), 0);
11+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /