Skip to content

Commit e454dbb

Browse files
authoredJun 3, 2022
feat: add solutions to lc problems
* No.2190.Most Frequent Number Following Key In an Array * No.2191.Sort the Jumbled Numbers * NO.2193.Minimum Number of Moves to Make Palindrome
1 parent f180f5c commit e454dbb

File tree

29 files changed

+782
-18
lines changed

29 files changed

+782
-18
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
### 6. 图论
146146

147147
- [网络延迟时间](/solution/0700-0799/0743.Network%20Delay%20Time/README.md) - `最短路``Dijkstra 算法``Bellman Ford 算法``SPFA 算法`
148+
- [得到要求路径的最小带权子图](/solution/2200-2299/2203.Minimum%20Weighted%20Subgraph%20With%20the%20Required%20Paths/README.md) - `最短路``Dijkstra 算法`
148149
- [连接所有点的最小费用](/solution/1500-1599/1584.Min%20Cost%20to%20Connect%20All%20Points/README.md) - `最小生成树``Prim 算法``Kruskal 算法`
149150
- [最低成本联通所有城市](/solution/1100-1199/1135.Connecting%20Cities%20With%20Minimum%20Cost/README.md) - `最小生成树``Kruskal 算法``并查集`
150151
- [水资源分配优化](/solution/1100-1199/1168.Optimize%20Water%20Distribution%20in%20a%20Village/README.md) - `最小生成树``Kruskal 算法``并查集`

‎README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
141141
### 6. Graph Theory
142142

143143
- [Network Delay Time](/solution/0700-0799/0743.Network%20Delay%20Time/README_EN.md) - `Shortest Path`, `Dijkstra's algorithm`, `Bellman Ford's algorithm`, `SPFA`
144+
- [Minimum Weighted Subgraph With the Required Paths](/solution/2200-2299/2203.Minimum%20Weighted%20Subgraph%20With%20the%20Required%20Paths/README_EN.md) - `Shortest Path`, `Dijkstra's algorithm`
144145
- [Min Cost to Connect All Points](/solution/1500-1599/1584.Min%20Cost%20to%20Connect%20All%20Points/README_EN.md) - `Minimum Spanning Tree`, `Prim's algorithm`, `Kruskal's algorithm`, `Union find`
145146
- [Connecting Cities With Minimum Cost](/solution/1100-1199/1135.Connecting%20Cities%20With%20Minimum%20Cost/README_EN.md) - `Minimum Spanning Tree`, `Kruskal's algorithm`
146147
- [Optimize Water Distribution in a Village](/solution/1100-1199/1168.Optimize%20Water%20Distribution%20in%20a%20Village/README_EN.md) - `Minimum Spanning Tree`, `Kruskal's algorithm`, `Union find`

‎lcci/17.10.Find Majority Element/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ def majorityElement(self, nums: List[int]) -> int:
55
if cnt == 0:
66
m, cnt = v, 1
77
else:
8-
cnt += (1 if m == v else -1)
8+
cnt += 1 if m == v else -1
99
return m if nums.count(m) > len(nums) // 2 else -1

‎lcof/面试题39. 数组中出现次数超过一半的数字/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ def majorityElement(self, nums: List[int]) -> int:
55
if cnt == 0:
66
m, cnt = v, 1
77
else:
8-
cnt += (1 if m == v else -1)
8+
cnt += 1 if m == v else -1
99
return m

‎solution/0100-0199/0160.Intersection of Two Linked Lists/Solution.py

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# self.val = x
55
# self.next = None
66

7+
78
class Solution:
89
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
910
a, b = headA, headB

‎solution/0100-0199/0169.Majority Element/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ def majorityElement(self, nums: List[int]) -> int:
55
if cnt == 0:
66
m, cnt = v, 1
77
else:
8-
cnt += (1 if m == v else -1)
8+
cnt += 1 if m == v else -1
99
return m

‎solution/2100-2199/2190.Most Frequent Number Following Key In an Array/README.md

+74-1
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,88 @@ target = 2 是紧跟着 key 之后出现次数最多的数字,所以我们返
5858
<!-- 这里可写当前语言的特殊实现逻辑 -->
5959

6060
```python
61-
61+
class Solution:
62+
def mostFrequent(self, nums: List[int], key: int) -> int:
63+
cnt = Counter()
64+
mx = ans = 0
65+
for i, v in enumerate(nums[:-1]):
66+
if v == key:
67+
target = nums[i + 1]
68+
cnt[target] += 1
69+
if mx < cnt[target]:
70+
mx = cnt[target]
71+
ans = nums[i + 1]
72+
return ans
6273
```
6374

6475
### **Java**
6576

6677
<!-- 这里可写当前语言的特殊实现逻辑 -->
6778

6879
```java
80+
class Solution {
81+
public int mostFrequent(int[] nums, int key) {
82+
int[] cnt = new int[1010];
83+
int mx = 0, ans = 0;
84+
for (int i = 0; i < nums.length - 1; ++i) {
85+
if (nums[i] == key) {
86+
int target = nums[i + 1];
87+
++cnt[target];
88+
if (mx < cnt[target]) {
89+
mx = cnt[target];
90+
ans = nums[i + 1];
91+
}
92+
}
93+
}
94+
return ans;
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int mostFrequent(vector<int>& nums, int key) {
105+
vector<int> cnt(1010);
106+
int mx = 0, ans = 0;
107+
for (int i = 0; i < nums.size() - 1; ++i)
108+
{
109+
if (nums[i] == key)
110+
{
111+
int target = nums[i + 1];
112+
++cnt[target];
113+
if (mx < cnt[target])
114+
{
115+
mx = cnt[target];
116+
ans = nums[i + 1];
117+
}
118+
}
119+
}
120+
return ans;
121+
}
122+
};
123+
```
69124
125+
### **Go**
126+
127+
```go
128+
func mostFrequent(nums []int, key int) int {
129+
cnt := make([]int, 1010)
130+
mx, ans := 0, 0
131+
for i, v := range nums[:len(nums)-1] {
132+
if v == key {
133+
target := nums[i+1]
134+
cnt[target]++
135+
if mx < cnt[target] {
136+
mx = cnt[target]
137+
ans = nums[i+1]
138+
}
139+
}
140+
}
141+
return ans
142+
}
70143
```
71144

72145
### **TypeScript**

‎solution/2100-2199/2190.Most Frequent Number Following Key In an Array/README_EN.md

+74-1
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,86 @@ target = 2 has the maximum number of occurrences following an occurrence of key,
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
def mostFrequent(self, nums: List[int], key: int) -> int:
57+
cnt = Counter()
58+
mx = ans = 0
59+
for i, v in enumerate(nums[:-1]):
60+
if v == key:
61+
target = nums[i + 1]
62+
cnt[target] += 1
63+
if mx < cnt[target]:
64+
mx = cnt[target]
65+
ans = nums[i + 1]
66+
return ans
5667
```
5768

5869
### **Java**
5970

6071
```java
72+
class Solution {
73+
public int mostFrequent(int[] nums, int key) {
74+
int[] cnt = new int[1010];
75+
int mx = 0, ans = 0;
76+
for (int i = 0; i < nums.length - 1; ++i) {
77+
if (nums[i] == key) {
78+
int target = nums[i + 1];
79+
++cnt[target];
80+
if (mx < cnt[target]) {
81+
mx = cnt[target];
82+
ans = nums[i + 1];
83+
}
84+
}
85+
}
86+
return ans;
87+
}
88+
}
89+
```
90+
91+
### **C++**
92+
93+
```cpp
94+
class Solution {
95+
public:
96+
int mostFrequent(vector<int>& nums, int key) {
97+
vector<int> cnt(1010);
98+
int mx = 0, ans = 0;
99+
for (int i = 0; i < nums.size() - 1; ++i)
100+
{
101+
if (nums[i] == key)
102+
{
103+
int target = nums[i + 1];
104+
++cnt[target];
105+
if (mx < cnt[target])
106+
{
107+
mx = cnt[target];
108+
ans = nums[i + 1];
109+
}
110+
}
111+
}
112+
return ans;
113+
}
114+
};
115+
```
61116
117+
### **Go**
118+
119+
```go
120+
func mostFrequent(nums []int, key int) int {
121+
cnt := make([]int, 1010)
122+
mx, ans := 0, 0
123+
for i, v := range nums[:len(nums)-1] {
124+
if v == key {
125+
target := nums[i+1]
126+
cnt[target]++
127+
if mx < cnt[target] {
128+
mx = cnt[target]
129+
ans = nums[i+1]
130+
}
131+
}
132+
}
133+
return ans
134+
}
62135
```
63136

64137
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int mostFrequent(vector<int>& nums, int key) {
4+
vector<int> cnt(1010);
5+
int mx = 0, ans = 0;
6+
for (int i = 0; i < nums.size() - 1; ++i)
7+
{
8+
if (nums[i] == key)
9+
{
10+
int target = nums[i + 1];
11+
++cnt[target];
12+
if (mx < cnt[target])
13+
{
14+
mx = cnt[target];
15+
ans = nums[i + 1];
16+
}
17+
}
18+
}
19+
return ans;
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func mostFrequent(nums []int, key int) int {
2+
cnt := make([]int, 1010)
3+
mx, ans := 0, 0
4+
for i, v := range nums[:len(nums)-1] {
5+
if v == key {
6+
target := nums[i+1]
7+
cnt[target]++
8+
if mx < cnt[target] {
9+
mx = cnt[target]
10+
ans = nums[i+1]
11+
}
12+
}
13+
}
14+
return ans
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int mostFrequent(int[] nums, int key) {
3+
int[] cnt = new int[1010];
4+
int mx = 0, ans = 0;
5+
for (int i = 0; i < nums.length - 1; ++i) {
6+
if (nums[i] == key) {
7+
int target = nums[i + 1];
8+
++cnt[target];
9+
if (mx < cnt[target]) {
10+
mx = cnt[target];
11+
ans = nums[i + 1];
12+
}
13+
}
14+
}
15+
return ans;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def mostFrequent(self, nums: List[int], key: int) -> int:
3+
cnt = Counter()
4+
mx = ans = 0
5+
for i, v in enumerate(nums[:-1]):
6+
if v == key:
7+
target = nums[i + 1]
8+
cnt[target] += 1
9+
if mx < cnt[target]:
10+
mx = cnt[target]
11+
ans = nums[i + 1]
12+
return ans

‎solution/2100-2199/2191.Sort the Jumbled Numbers/README.md

+52-2
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,72 @@
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
**方法一:自定义排序**
63+
6264
<!-- tabs:start -->
6365

6466
### **Python3**
6567

6668
<!-- 这里可写当前语言的特殊实现逻辑 -->
6769

6870
```python
69-
71+
class Solution:
72+
def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:
73+
m = []
74+
for i, v in enumerate(nums):
75+
a, b, t = v, 0, 1
76+
while 1:
77+
a, x = divmod(a, 10)
78+
x = mapping[x]
79+
b = x * t + b
80+
t *= 10
81+
if a == 0:
82+
break
83+
m.append((b, i, v))
84+
m.sort()
85+
for i, v in enumerate(m):
86+
nums[i] = v[2]
87+
return nums
7088
```
7189

7290
### **Java**
7391

7492
<!-- 这里可写当前语言的特殊实现逻辑 -->
7593

7694
```java
77-
95+
class Solution {
96+
public int[] sortJumbled(int[] mapping, int[] nums) {
97+
List<int[]> m = new ArrayList<>();
98+
for (int i = 0; i < nums.length; ++i) {
99+
int v = nums[i];
100+
int a = v, b = 0, t = 1;
101+
while (true) {
102+
int x = a % 10;
103+
x = mapping[x];
104+
a /= 10;
105+
b = x * t + b;
106+
t *= 10;
107+
if (a == 0) {
108+
break;
109+
}
110+
}
111+
m.add(new int[]{b, i, v});
112+
}
113+
m.sort((a, b) -> {
114+
if (a[0] != b[0]) {
115+
return a[0] - b[0];
116+
}
117+
if (a[1] != b[1]) {
118+
return a[1] - b[1];
119+
}
120+
return 0;
121+
});
122+
for (int i = 0; i < m.size(); ++i) {
123+
nums[i] = m.get(i)[2];
124+
}
125+
return nums;
126+
}
127+
}
78128
```
79129

80130
### **TypeScript**

0 commit comments

Comments
 (0)
Please sign in to comment.