Skip to content

Commit 4eb8755

Browse files
authored
feat: update solutions to lc problem: No.0436
* No.0436.Find Right Interval
1 parent ee0a0f0 commit 4eb8755

File tree

13 files changed

+52
-56
lines changed

13 files changed

+52
-56
lines changed

solution/0400-0499/0436.Find Right Interval/README.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58-
二分查找
58+
**方法一:二分查找**
5959

6060
<!-- tabs:start -->
6161

@@ -66,20 +66,16 @@
6666
```python
6767
class Solution:
6868
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
69+
for i, v in enumerate(intervals):
70+
v.append(i)
71+
intervals.sort()
6972
n = len(intervals)
70-
starts = [(intervals[i][0], i) for i in range(n)]
71-
starts.sort(key=lambda x : x[0])
72-
res = []
73-
for _, end in intervals:
74-
left, right = 0, n - 1
75-
while left < right:
76-
mid = (left + right) >> 1
77-
if starts[mid][0] >= end:
78-
right = mid
79-
else:
80-
left = mid + 1
81-
res.append(-1 if starts[left][0] < end else starts[left][1])
82-
return res
73+
ans = [-1] * n
74+
for _, e, i in intervals:
75+
j = bisect_left(intervals, [e])
76+
if j < n:
77+
ans[i] = intervals[j][2]
78+
return ans
8379
```
8480

8581
### **Java**

solution/0400-0499/0436.Find Right Interval/README_EN.md

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,16 @@ Binary search.
5959
```python
6060
class Solution:
6161
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
62+
for i, v in enumerate(intervals):
63+
v.append(i)
64+
intervals.sort()
6265
n = len(intervals)
63-
starts = [(intervals[i][0], i) for i in range(n)]
64-
starts.sort(key=lambda x : x[0])
65-
res = []
66-
for _, end in intervals:
67-
left, right = 0, n - 1
68-
while left < right:
69-
mid = (left + right) >> 1
70-
if starts[mid][0] >= end:
71-
right = mid
72-
else:
73-
left = mid + 1
74-
res.append(-1 if starts[left][0] < end else starts[left][1])
75-
return res
66+
ans = [-1] * n
67+
for _, e, i in intervals:
68+
j = bisect_left(intervals, [e])
69+
if j < n:
70+
ans[i] = intervals[j][2]
71+
return ans
7672
```
7773

7874
### **Java**
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
class Solution:
22
def findRightInterval(self, intervals: List[List[int]]) -> List[int]:
3+
for i, v in enumerate(intervals):
4+
v.append(i)
5+
intervals.sort()
36
n = len(intervals)
4-
starts = [(intervals[i][0], i) for i in range(n)]
5-
starts.sort(key=lambda x: x[0])
6-
res = []
7-
for _, end in intervals:
8-
left, right = 0, n - 1
9-
while left < right:
10-
mid = (left + right) >> 1
11-
if starts[mid][0] >= end:
12-
right = mid
13-
else:
14-
left = mid + 1
15-
res.append(-1 if starts[left][0] < end else starts[left][1])
16-
return res
7+
ans = [-1] * n
8+
for _, e, i in intervals:
9+
j = bisect_left(intervals, [e])
10+
if j < n:
11+
ans[i] = intervals[j][2]
12+
return ans

solution/1500-1599/1534.Count Good Triplets/Solution.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
55
for i in range(n):
66
for j in range(i + 1, n):
77
for k in range(j + 1, n):
8-
if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
8+
if (
9+
abs(arr[i] - arr[j]) <= a
10+
and abs(arr[j] - arr[k]) <= b
11+
and abs(arr[i] - arr[k]) <= c
12+
):
913
ans += 1
1014
return ans

solution/2200-2299/2259.Remove Digit From Number to Maximize Result/Solution.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ function removeDigit(number: string, digit: string): string {
55
for (let i = 0; i < n; i++) {
66
if (nums[i] != digit) continue;
77
ans = i;
8-
if ((i + 1 < n) && (nums[i + 1] > nums[i])) break;
8+
if (i + 1 < n && nums[i + 1] > nums[i]) break;
99
}
1010
nums.splice(ans, 1);
1111
return nums.join('');
12-
};
12+
}

solution/2200-2299/2260.Minimum Consecutive Cards to Pick Up/Solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ function minimumCardPickup(cards: number[]): number {
1111
hashMap.set(num, i);
1212
}
1313
return ans == max ? -1 : ans;
14-
};
14+
}

solution/2200-2299/2261.K Divisible Elements Subarrays/Solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ function countDistinct(nums: number[], k: number, p: number): number {
1919
}
2020
}
2121
return ans.size;
22-
};
22+
}

solution/2200-2299/2262.Total Appeal of A String/Solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ function appealSum(s: string): number {
88
hashMap.set(c, i + 1);
99
}
1010
return dp.reduce((a, c) => a + c, 0);
11-
};
11+
}

solution/2200-2299/2264.Largest 3-Same-Digit Number in String/Solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ function largestGoodInteger(num: string): string {
44
if (num.includes(c)) return c;
55
}
66
return '';
7-
};
7+
}

solution/2200-2299/2273.Find Resultant Array After Removing Anagrams/Solution.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ function removeAnagrams(words: string[]): string[] {
1111
}
1212
}
1313
return ans;
14-
};
14+
}
1515

16-
function countWord (word: string): number[] {
16+
function countWord(word: string): number[] {
1717
let count = new Array(128).fill(0);
1818
for (let i = 0; i < word.length; i++) {
1919
count[word.charCodeAt(i)]++;
2020
}
2121
return count;
22-
}
22+
}

0 commit comments

Comments
 (0)