Skip to content

Commit c44adba

Browse files
committed
feat: add solutions to lc problems
* No.2357.Make Array Zero by Subtracting Equal Amounts * No.2359.Find Closest Node to Given Two Nodes * No.2363.Merge Similar Items * No.2364.Count Number of Bad Pair
1 parent fdebfee commit c44adba

File tree

25 files changed

+360
-168
lines changed

25 files changed

+360
-168
lines changed

solution/0400-0499/0410.Split Array Largest Sum/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class Solution {
8282
}
8383
return left;
8484
}
85-
85+
8686
private boolean check(int[] nums, int mx, int k) {
8787
int s = 1 << 30, cnt = 0;
8888
for (int x : nums) {

solution/1100-1199/1157.Online Majority Element In Subarray/README_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class SegmentTree {
194194
} else {
195195
tr[u].x = tr[u << 1 | 1].x;
196196
tr[u].cnt = tr[u << 1 | 1].cnt - tr[u << 1].cnt;
197-
}
197+
}
198198
}
199199
}
200200

@@ -208,7 +208,7 @@ class MajorityChecker {
208208
d.computeIfAbsent(arr[i], k -> new ArrayList<>()).add(i);
209209
}
210210
}
211-
211+
212212
public int query(int left, int right, int threshold) {
213213
int x = tree.query(1, left + 1, right + 1)[0];
214214
int l = search(d.get(x), left);

solution/1100-1199/1157.Online Majority Element In Subarray/Solution.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ def pushup(self, u):
5252

5353

5454
class MajorityChecker:
55-
5655
def __init__(self, arr: List[int]):
5756
self.tree = SegmentTree(arr)
5857
self.d = defaultdict(list)

solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class Solution:
9494
return False
9595
x //= 10
9696
return True
97-
97+
9898
for a in range(1, n):
9999
b = n - a
100100
if f(a) and f(b):

solution/1300-1399/1317.Convert Integer to the Sum of Two No-Zero Integers/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Solution:
6666
return False
6767
x //= 10
6868
return True
69-
69+
7070
for a in range(1, n):
7171
b = n - a
7272
if f(a) and f(b):

solution/1400-1499/1483.Kth Ancestor of a Tree Node/README_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class TreeAncestor {
9999
}
100100
}
101101
}
102-
102+
103103
public int getKthAncestor(int node, int k) {
104104
for (int i = 17; i >= 0; --i) {
105105
if (((k >> i) & 1) == 1) {
@@ -139,7 +139,7 @@ public:
139139
}
140140
}
141141
}
142-
142+
143143
int getKthAncestor(int node, int k) {
144144
for (int i = 17; ~i; --i) {
145145
if (k >> i & 1) {

solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,18 @@ func minimumOperations(nums []int) (ans int) {
125125
### **TypeScript**
126126

127127
```ts
128-
128+
function minimumOperations(nums: number[]): number {
129+
const s = new Array(101).fill(false);
130+
s[0] = true;
131+
let ans = 0;
132+
for (const x of nums) {
133+
if (!s[x]) {
134+
s[x] = true;
135+
++ans;
136+
}
137+
}
138+
return ans;
139+
}
129140
```
130141

131142
### **...**

solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/README_EN.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,18 @@ func minimumOperations(nums []int) (ans int) {
110110
### **TypeScript**
111111

112112
```ts
113-
113+
function minimumOperations(nums: number[]): number {
114+
const s = new Array(101).fill(false);
115+
s[0] = true;
116+
let ans = 0;
117+
for (const x of nums) {
118+
if (!s[x]) {
119+
s[x] = true;
120+
++ans;
121+
}
122+
}
123+
return ans;
124+
}
114125
```
115126

116127
### **...**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function minimumOperations(nums: number[]): number {
2+
const s = new Array(101).fill(false);
3+
s[0] = true;
4+
let ans = 0;
5+
for (const x of nums) {
6+
if (!s[x]) {
7+
s[x] = true;
8+
++ans;
9+
}
10+
}
11+
return ans;
12+
}

solution/2300-2399/2359.Find Closest Node to Given Two Nodes/README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,47 @@ func max(a, b int) int {
437437
### **TypeScript**
438438

439439
```ts
440-
440+
function closestMeetingNode(
441+
edges: number[],
442+
node1: number,
443+
node2: number,
444+
): number {
445+
const n = edges.length;
446+
const g = Array.from({ length: n }, () => []);
447+
for (let i = 0; i < n; ++i) {
448+
if (edges[i] != -1) {
449+
g[i].push(edges[i]);
450+
}
451+
}
452+
const inf = 1 << 30;
453+
const f = (i: number) => {
454+
const dist = new Array(n).fill(inf);
455+
dist[i] = 0;
456+
const q: number[] = [i];
457+
while (q.length) {
458+
i = q.shift();
459+
for (const j of g[i]) {
460+
if (dist[j] == inf) {
461+
dist[j] = dist[i] + 1;
462+
q.push(j);
463+
}
464+
}
465+
}
466+
return dist;
467+
};
468+
const d1 = f(node1);
469+
const d2 = f(node2);
470+
let ans = -1;
471+
let d = inf;
472+
for (let i = 0; i < n; ++i) {
473+
const t = Math.max(d1[i], d2[i]);
474+
if (t < d) {
475+
d = t;
476+
ans = i;
477+
}
478+
}
479+
return ans;
480+
}
441481
```
442482

443483
### **...**

0 commit comments

Comments
 (0)