Skip to content

Commit 6afa941

Browse files
committed
feat: add solutions to lc problem: No.1150
No.1150.Check If a Number Is Majority Element in a Sorted Array
1 parent 3059cd7 commit 6afa941

File tree

4 files changed

+227
-5
lines changed

4 files changed

+227
-5
lines changed

solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@
5555

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

58-
**方法一:贪心 + 递归或双指针**
58+
**方法一:贪心 + 双指针**
5959

6060
我们可以从字符串的两端开始,寻找长度最短的、相同且不重叠的前后缀:
6161

6262
- 如果找不到这样的前后缀,那么整个字符串作为一个段式回文,答案加 $1$;
63-
- 如果找到了这样的前后缀,那么这个前后缀作为一个段式回文,答案加 $2$,然后继续寻找剩下的字符串的前后缀。这里我们可以使用递归或双指针来实现。
63+
- 如果找到了这样的前后缀,那么这个前后缀作为一个段式回文,答案加 $2$,然后继续寻找剩下的字符串的前后缀。
6464

6565
以上贪心策略的证明如下:
6666

@@ -72,7 +72,9 @@
7272

7373
**方法二:字符串哈希**
7474

75-
在方法一的基础上,我们可以使用字符串哈希的方法,在 $O(1)$ 时间内比较两个字符串是否相等。
75+
**字符串哈希**是把一个任意长度的字符串映射成一个非负整数,并且其冲突的概率几乎为 $0$。字符串哈希用于计算字符串哈希值,快速判断两个字符串是否相等。
76+
77+
因此,在方法一的基础上,我们可以使用字符串哈希的方法,在 $O(1)$ 时间内比较两个字符串是否相等。
7678

7779
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串的长度。
7880

solution/1100-1199/1150.Check If a Number Is Majority Element in a Sorted Array/README.md

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,15 @@
4848

4949
**方法一:二分查找**
5050

51-
“二分查找”求 `target` 在数组 `nums` 中的左右边界
51+
我们注意到,数组 $nums$ 中的元素是非递减的,也就是说,数组 $nums$ 中的元素单调递增。因此,我们可以使用二分查找的方法,找到数组 $nums$ 中第一个大于等于 $target$ 的元素的下标 $left$,以及第一个大于 $target$ 的元素的下标 $right$。如果 $right - left > \frac{n}{2}$,则说明数组 $nums$ 中的元素 $target$ 出现的次数超过了数组长度的一半,因此返回 $true$,否则返回 $false$
5252

53-
时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
53+
时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $nums$ 的长度。
54+
55+
**方法二:二分查找(优化)**
56+
57+
方法一中,我们使用了两次二分查找,分别找到数组 $nums$ 中第一个大于等于 $target$ 的元素的下标 $left$,以及第一个大于 $target$ 的元素的下标 $right$。但是,我们可以使用一次二分查找,找到数组 $nums$ 中第一个大于等于 $target$ 的元素的下标 $left$,然后判断 $nums[left + \frac{n}{2}]$ 是否等于 $target$,如果相等,说明数组 $nums$ 中的元素 $target$ 出现的次数超过了数组长度的一半,因此返回 $true$,否则返回 $false$。
58+
59+
时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $nums$ 的长度。
5460

5561
<!-- tabs:start -->
5662

@@ -66,6 +72,14 @@ class Solution:
6672
return right - left > len(nums) // 2
6773
```
6874

75+
```python
76+
class Solution:
77+
def isMajorityElement(self, nums: List[int], target: int) -> bool:
78+
left = bisect_left(nums, target)
79+
right = left + len(nums) // 2
80+
return right < len(nums) and nums[right] == target
81+
```
82+
6983
### **Java**
7084

7185
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -93,6 +107,30 @@ class Solution {
93107
}
94108
```
95109

110+
```java
111+
class Solution {
112+
public boolean isMajorityElement(int[] nums, int target) {
113+
int n = nums.length;
114+
int left = search(nums, target);
115+
int right = left + n / 2;
116+
return right < n && nums[right] == target;
117+
}
118+
119+
private int search(int[] nums, int x) {
120+
int left = 0, right = nums.length;
121+
while (left < right) {
122+
int mid = (left + right) >> 1;
123+
if (nums[mid] >= x) {
124+
right = mid;
125+
} else {
126+
left = mid + 1;
127+
}
128+
}
129+
return left;
130+
}
131+
}
132+
```
133+
96134
### **C++**
97135

98136
```cpp
@@ -106,6 +144,18 @@ public:
106144
};
107145
```
108146
147+
```cpp
148+
class Solution {
149+
public:
150+
bool isMajorityElement(vector<int>& nums, int target) {
151+
int n = nums.size();
152+
int left = lower_bound(nums.begin(), nums.end(), target) - nums.begin();
153+
int right = left + n / 2;
154+
return right < n && nums[right] == target;
155+
}
156+
};
157+
```
158+
109159
### **Go**
110160

111161
```go
@@ -117,6 +167,60 @@ func isMajorityElement(nums []int, target int) bool {
117167
}
118168
```
119169

170+
```go
171+
func isMajorityElement(nums []int, target int) bool {
172+
n := len(nums)
173+
left := sort.Search(n, func(i int) bool { return nums[i] >= target })
174+
right := left + n/2
175+
return right < n && nums[right] == target
176+
}
177+
```
178+
179+
### **TypeScript**
180+
181+
```ts
182+
function isMajorityElement(nums: number[], target: number): boolean {
183+
const search = (x: number) => {
184+
let left = 0;
185+
let right = nums.length;
186+
while (left < right) {
187+
const mid = (left + right) >> 1;
188+
if (nums[mid] >= x) {
189+
right = mid;
190+
} else {
191+
left = mid + 1;
192+
}
193+
}
194+
return left;
195+
};
196+
const left = search(target);
197+
const right = search(target + 1);
198+
return right - left > nums.length >> 1;
199+
}
200+
```
201+
202+
```ts
203+
function isMajorityElement(nums: number[], target: number): boolean {
204+
const search = (x: number) => {
205+
let left = 0;
206+
let right = n;
207+
while (left < right) {
208+
const mid = (left + right) >> 1;
209+
if (nums[mid] >= x) {
210+
right = mid;
211+
} else {
212+
left = mid + 1;
213+
}
214+
}
215+
return left;
216+
};
217+
const n = nums.length;
218+
const left = search(target);
219+
const right = left + (n >> 1);
220+
return right < n && nums[right] === target;
221+
}
222+
```
223+
120224
### **...**
121225

122226
```

solution/1100-1199/1150.Check If a Number Is Majority Element in a Sorted Array/README_EN.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ class Solution:
5050
return right - left > len(nums) // 2
5151
```
5252

53+
```python
54+
class Solution:
55+
def isMajorityElement(self, nums: List[int], target: int) -> bool:
56+
left = bisect_left(nums, target)
57+
right = left + len(nums) // 2
58+
return right < len(nums) and nums[right] == target
59+
```
60+
5361
### **Java**
5462

5563
```java
@@ -75,6 +83,30 @@ class Solution {
7583
}
7684
```
7785

86+
```java
87+
class Solution {
88+
public boolean isMajorityElement(int[] nums, int target) {
89+
int n = nums.length;
90+
int left = search(nums, target);
91+
int right = left + n / 2;
92+
return right < n && nums[right] == target;
93+
}
94+
95+
private int search(int[] nums, int x) {
96+
int left = 0, right = nums.length;
97+
while (left < right) {
98+
int mid = (left + right) >> 1;
99+
if (nums[mid] >= x) {
100+
right = mid;
101+
} else {
102+
left = mid + 1;
103+
}
104+
}
105+
return left;
106+
}
107+
}
108+
```
109+
78110
### **C++**
79111

80112
```cpp
@@ -88,6 +120,18 @@ public:
88120
};
89121
```
90122
123+
```cpp
124+
class Solution {
125+
public:
126+
bool isMajorityElement(vector<int>& nums, int target) {
127+
int n = nums.size();
128+
int left = lower_bound(nums.begin(), nums.end(), target) - nums.begin();
129+
int right = left + n / 2;
130+
return right < n && nums[right] == target;
131+
}
132+
};
133+
```
134+
91135
### **Go**
92136

93137
```go
@@ -99,6 +143,60 @@ func isMajorityElement(nums []int, target int) bool {
99143
}
100144
```
101145

146+
```go
147+
func isMajorityElement(nums []int, target int) bool {
148+
n := len(nums)
149+
left := sort.Search(n, func(i int) bool { return nums[i] >= target })
150+
right := left + n/2
151+
return right < n && nums[right] == target
152+
}
153+
```
154+
155+
### **TypeScript**
156+
157+
```ts
158+
function isMajorityElement(nums: number[], target: number): boolean {
159+
const search = (x: number) => {
160+
let left = 0;
161+
let right = nums.length;
162+
while (left < right) {
163+
const mid = (left + right) >> 1;
164+
if (nums[mid] >= x) {
165+
right = mid;
166+
} else {
167+
left = mid + 1;
168+
}
169+
}
170+
return left;
171+
};
172+
const left = search(target);
173+
const right = search(target + 1);
174+
return right - left > nums.length >> 1;
175+
}
176+
```
177+
178+
```ts
179+
function isMajorityElement(nums: number[], target: number): boolean {
180+
const search = (x: number) => {
181+
let left = 0;
182+
let right = n;
183+
while (left < right) {
184+
const mid = (left + right) >> 1;
185+
if (nums[mid] >= x) {
186+
right = mid;
187+
} else {
188+
left = mid + 1;
189+
}
190+
}
191+
return left;
192+
};
193+
const n = nums.length;
194+
const left = search(target);
195+
const right = left + (n >> 1);
196+
return right < n && nums[right] === target;
197+
}
198+
```
199+
102200
### **...**
103201

104202
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function isMajorityElement(nums: number[], target: number): boolean {
2+
const search = (x: number) => {
3+
let left = 0;
4+
let right = nums.length;
5+
while (left < right) {
6+
const mid = (left + right) >> 1;
7+
if (nums[mid] >= x) {
8+
right = mid;
9+
} else {
10+
left = mid + 1;
11+
}
12+
}
13+
return left;
14+
};
15+
const left = search(target);
16+
const right = search(target + 1);
17+
return right - left > nums.length >> 1;
18+
}

0 commit comments

Comments
 (0)