Skip to content

Commit 78450b0

Browse files
committed
feat: add solutions to lc problem: No.1533. Find the Index of the Largest Integer
1 parent 74cc0dc commit 78450b0

File tree

6 files changed

+403
-5
lines changed

6 files changed

+403
-5
lines changed

solution/1500-1599/1533.Find the Index of the Large Integer/README.md

+140-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
<ul>
1212
<li><code>int compareSub(int l, int r, int x, int y)</code>:其中&nbsp;<code>0 &lt;= l, r, x, y &lt;&nbsp;ArrayReader.length()</code>,&nbsp;<code>l &lt;= r</code>&nbsp;且&nbsp;<code>x &lt;= y</code>。这个函数比较子数组&nbsp;<code>arr[l..r]</code>&nbsp;与子数组&nbsp;<code>arr[x..y]</code>&nbsp;的和。该函数返回:
13-
1413
<ul>
1514
<li><strong>1</strong>&nbsp;若&nbsp;<code>arr[l]+arr[l+1]+...+arr[r] &gt; arr[x]+arr[x+1]+...+arr[y]</code>&nbsp;。</li>
1615
<li><strong>0</strong>&nbsp;若&nbsp;<code>arr[l]+arr[l+1]+...+arr[r] == arr[x]+arr[x+1]+...+arr[y]</code>&nbsp;。</li>
@@ -61,27 +60,165 @@ reader.compareSub(4, 4, 5, 5) // 返回 1。因此,可以确定 arr[4] 是数
6160
<li><code>arr</code>&nbsp;中除一个最大元素外,其余所有元素都相等。</li>
6261
</ul>
6362

64-
6563
## 解法
6664

6765
<!-- 这里可写通用的实现逻辑 -->
6866

67+
三分查找。
68+
69+
前两部分数量相等,进行 `compareSub` 比较。
70+
6971
<!-- tabs:start -->
7072

7173
### **Python3**
7274

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

7577
```python
76-
78+
# """
79+
# This is ArrayReader's API interface.
80+
# You should not implement it, or speculate about its implementation
81+
# """
82+
#class ArrayReader(object):
83+
# # Compares the sum of arr[l..r] with the sum of arr[x..y]
84+
# # return 1 if sum(arr[l..r]) > sum(arr[x..y])
85+
# # return 0 if sum(arr[l..r]) == sum(arr[x..y])
86+
# # return -1 if sum(arr[l..r]) < sum(arr[x..y])
87+
# def compareSub(self, l: int, r: int, x: int, y: int) -> int:
88+
#
89+
# # Returns the length of the array
90+
# def length(self) -> int:
91+
#
92+
93+
94+
class Solution:
95+
def getIndex(self, reader: 'ArrayReader') -> int:
96+
left, right = 0, reader.length() - 1
97+
while left < right:
98+
t1, t2, t3 = left, left + (right - left) // 3, left + ((right - left) // 3) * 2 + 1
99+
cmp = reader.compareSub(t1, t2, t2 + 1, t3)
100+
if cmp == 0:
101+
left = t3 + 1
102+
elif cmp == 1:
103+
right = t2
104+
else:
105+
left, right = t2 + 1, t3
106+
return left
77107
```
78108

79109
### **Java**
80110

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

83113
```java
114+
/**
115+
* // This is ArrayReader's API interface.
116+
* // You should not implement it, or speculate about its implementation
117+
* interface ArrayReader {
118+
* // Compares the sum of arr[l..r] with the sum of arr[x..y]
119+
* // return 1 if sum(arr[l..r]) > sum(arr[x..y])
120+
* // return 0 if sum(arr[l..r]) == sum(arr[x..y])
121+
* // return -1 if sum(arr[l..r]) < sum(arr[x..y])
122+
* public int compareSub(int l, int r, int x, int y) {}
123+
*
124+
* // Returns the length of the array
125+
* public int length() {}
126+
* }
127+
*/
128+
129+
class Solution {
130+
public int getIndex(ArrayReader reader) {
131+
int left = 0, right = reader.length() - 1;
132+
while (left < right) {
133+
int t1 = left, t2 = left + (right - left) / 3, t3 = left + (right - left) / 3 * 2 + 1;
134+
int cmp = reader.compareSub(t1, t2, t2 + 1, t3);
135+
if (cmp == 0) {
136+
left = t3 + 1;
137+
} else if (cmp == 1) {
138+
right = t2;
139+
} else {
140+
left = t2 + 1;
141+
right = t3;
142+
}
143+
}
144+
return left;
145+
}
146+
}
147+
```
148+
149+
### **C++**
150+
151+
```cpp
152+
/**
153+
* // This is the ArrayReader's API interface.
154+
* // You should not implement it, or speculate about its implementation
155+
* class ArrayReader {
156+
* public:
157+
* // Compares the sum of arr[l..r] with the sum of arr[x..y]
158+
* // return 1 if sum(arr[l..r]) > sum(arr[x..y])
159+
* // return 0 if sum(arr[l..r]) == sum(arr[x..y])
160+
* // return -1 if sum(arr[l..r]) < sum(arr[x..y])
161+
* int compareSub(int l, int r, int x, int y);
162+
*
163+
* // Returns the length of the array
164+
* int length();
165+
* };
166+
*/
167+
168+
class Solution {
169+
public:
170+
int getIndex(ArrayReader &reader) {
171+
int left = 0, right = reader.length() - 1;
172+
while (left < right) {
173+
int t1 = left, t2 = left + (right - left) / 3, t3 = left + (right - left) / 3 * 2 + 1;
174+
int cmp = reader.compareSub(t1, t2, t2 + 1, t3);
175+
if (cmp == 0) {
176+
left = t3 + 1;
177+
} else if (cmp == 1) {
178+
right = t2;
179+
} else {
180+
left = t2 + 1;
181+
right = t3;
182+
}
183+
}
184+
return left;
185+
}
186+
};
187+
```
84188
189+
### **Go**
190+
191+
```go
192+
/**
193+
* // This is the ArrayReader's API interface.
194+
* // You should not implement it, or speculate about its implementation
195+
* type ArrayReader struct {
196+
* }
197+
* // Compares the sum of arr[l..r] with the sum of arr[x..y]
198+
* // return 1 if sum(arr[l..r]) > sum(arr[x..y])
199+
* // return 0 if sum(arr[l..r]) == sum(arr[x..y])
200+
* // return -1 if sum(arr[l..r]) < sum(arr[x..y])
201+
* func (this *ArrayReader) compareSub(l, r, x, y int) int {}
202+
*
203+
* // Returns the length of the array
204+
* func (this *ArrayReader) length() int {}
205+
*/
206+
207+
func getIndex(reader *ArrayReader) int {
208+
left, right := 0, reader.length()-1
209+
for left < right {
210+
t1, t2, t3 := left, left+(right-left)/3, left+(right-left)/3*2+1
211+
cmp := reader.compareSub(t1, t2, t2+1, t3)
212+
if cmp == 0 {
213+
left = t3 + 1
214+
} else if cmp == 1 {
215+
right = t2
216+
} else {
217+
left, right = t2+1, t3
218+
}
219+
}
220+
return left
221+
}
85222
```
86223

87224
### **...**

solution/1500-1599/1533.Find the Index of the Large Integer/README_EN.md

+136-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
<ul>
1010
<li><code>int compareSub(int l, int r, int x, int y)</code>: where <code>0 &lt;= l, r, x, y &lt;&nbsp;ArrayReader.length()</code>, <code>l &lt;= r and</code>&nbsp;<code>x &lt;= y</code>. The function compares the sum of sub-array <code>arr[l..r]</code> with the sum of the sub-array <code>arr[x..y]</code> and returns:
11-
1211
<ul>
1312
<li><strong>1</strong> if <code>arr[l]+arr[l+1]+...+arr[r] &gt; arr[x]+arr[x+1]+...+arr[y]</code>.</li>
1413
<li><strong>0</strong> if <code>arr[l]+arr[l+1]+...+arr[r] == arr[x]+arr[x+1]+...+arr[y]</code>.</li>
@@ -67,13 +66,148 @@ Notice that we made only 3 calls, so the answer is valid.
6766
### **Python3**
6867

6968
```python
70-
69+
# """
70+
# This is ArrayReader's API interface.
71+
# You should not implement it, or speculate about its implementation
72+
# """
73+
#class ArrayReader(object):
74+
# # Compares the sum of arr[l..r] with the sum of arr[x..y]
75+
# # return 1 if sum(arr[l..r]) > sum(arr[x..y])
76+
# # return 0 if sum(arr[l..r]) == sum(arr[x..y])
77+
# # return -1 if sum(arr[l..r]) < sum(arr[x..y])
78+
# def compareSub(self, l: int, r: int, x: int, y: int) -> int:
79+
#
80+
# # Returns the length of the array
81+
# def length(self) -> int:
82+
#
83+
84+
85+
class Solution:
86+
def getIndex(self, reader: 'ArrayReader') -> int:
87+
left, right = 0, reader.length() - 1
88+
while left < right:
89+
t1, t2, t3 = left, left + (right - left) // 3, left + ((right - left) // 3) * 2 + 1
90+
cmp = reader.compareSub(t1, t2, t2 + 1, t3)
91+
if cmp == 0:
92+
left = t3 + 1
93+
elif cmp == 1:
94+
right = t2
95+
else:
96+
left, right = t2 + 1, t3
97+
return left
7198
```
7299

73100
### **Java**
74101

75102
```java
103+
/**
104+
* // This is ArrayReader's API interface.
105+
* // You should not implement it, or speculate about its implementation
106+
* interface ArrayReader {
107+
* // Compares the sum of arr[l..r] with the sum of arr[x..y]
108+
* // return 1 if sum(arr[l..r]) > sum(arr[x..y])
109+
* // return 0 if sum(arr[l..r]) == sum(arr[x..y])
110+
* // return -1 if sum(arr[l..r]) < sum(arr[x..y])
111+
* public int compareSub(int l, int r, int x, int y) {}
112+
*
113+
* // Returns the length of the array
114+
* public int length() {}
115+
* }
116+
*/
117+
118+
class Solution {
119+
public int getIndex(ArrayReader reader) {
120+
int left = 0, right = reader.length() - 1;
121+
while (left < right) {
122+
int t1 = left, t2 = left + (right - left) / 3, t3 = left + (right - left) / 3 * 2 + 1;
123+
int cmp = reader.compareSub(t1, t2, t2 + 1, t3);
124+
if (cmp == 0) {
125+
left = t3 + 1;
126+
} else if (cmp == 1) {
127+
right = t2;
128+
} else {
129+
left = t2 + 1;
130+
right = t3;
131+
}
132+
}
133+
return left;
134+
}
135+
}
136+
```
137+
138+
### **C++**
139+
140+
```cpp
141+
/**
142+
* // This is the ArrayReader's API interface.
143+
* // You should not implement it, or speculate about its implementation
144+
* class ArrayReader {
145+
* public:
146+
* // Compares the sum of arr[l..r] with the sum of arr[x..y]
147+
* // return 1 if sum(arr[l..r]) > sum(arr[x..y])
148+
* // return 0 if sum(arr[l..r]) == sum(arr[x..y])
149+
* // return -1 if sum(arr[l..r]) < sum(arr[x..y])
150+
* int compareSub(int l, int r, int x, int y);
151+
*
152+
* // Returns the length of the array
153+
* int length();
154+
* };
155+
*/
156+
157+
class Solution {
158+
public:
159+
int getIndex(ArrayReader &reader) {
160+
int left = 0, right = reader.length() - 1;
161+
while (left < right) {
162+
int t1 = left, t2 = left + (right - left) / 3, t3 = left + (right - left) / 3 * 2 + 1;
163+
int cmp = reader.compareSub(t1, t2, t2 + 1, t3);
164+
if (cmp == 0) {
165+
left = t3 + 1;
166+
} else if (cmp == 1) {
167+
right = t2;
168+
} else {
169+
left = t2 + 1;
170+
right = t3;
171+
}
172+
}
173+
return left;
174+
}
175+
};
176+
```
76177
178+
### **Go**
179+
180+
```go
181+
/**
182+
* // This is the ArrayReader's API interface.
183+
* // You should not implement it, or speculate about its implementation
184+
* type ArrayReader struct {
185+
* }
186+
* // Compares the sum of arr[l..r] with the sum of arr[x..y]
187+
* // return 1 if sum(arr[l..r]) > sum(arr[x..y])
188+
* // return 0 if sum(arr[l..r]) == sum(arr[x..y])
189+
* // return -1 if sum(arr[l..r]) < sum(arr[x..y])
190+
* func (this *ArrayReader) compareSub(l, r, x, y int) int {}
191+
*
192+
* // Returns the length of the array
193+
* func (this *ArrayReader) length() int {}
194+
*/
195+
196+
func getIndex(reader *ArrayReader) int {
197+
left, right := 0, reader.length()-1
198+
for left < right {
199+
t1, t2, t3 := left, left+(right-left)/3, left+(right-left)/3*2+1
200+
cmp := reader.compareSub(t1, t2, t2+1, t3)
201+
if cmp == 0 {
202+
left = t3 + 1
203+
} else if cmp == 1 {
204+
right = t2
205+
} else {
206+
left, right = t2+1, t3
207+
}
208+
}
209+
return left
210+
}
77211
```
78212

79213
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* // This is the ArrayReader's API interface.
3+
* // You should not implement it, or speculate about its implementation
4+
* class ArrayReader {
5+
* public:
6+
* // Compares the sum of arr[l..r] with the sum of arr[x..y]
7+
* // return 1 if sum(arr[l..r]) > sum(arr[x..y])
8+
* // return 0 if sum(arr[l..r]) == sum(arr[x..y])
9+
* // return -1 if sum(arr[l..r]) < sum(arr[x..y])
10+
* int compareSub(int l, int r, int x, int y);
11+
*
12+
* // Returns the length of the array
13+
* int length();
14+
* };
15+
*/
16+
17+
class Solution {
18+
public:
19+
int getIndex(ArrayReader &reader) {
20+
int left = 0, right = reader.length() - 1;
21+
while (left < right) {
22+
int t1 = left, t2 = left + (right - left) / 3, t3 = left + (right - left) / 3 * 2 + 1;
23+
int cmp = reader.compareSub(t1, t2, t2 + 1, t3);
24+
if (cmp == 0) {
25+
left = t3 + 1;
26+
} else if (cmp == 1) {
27+
right = t2;
28+
} else {
29+
left = t2 + 1;
30+
right = t3;
31+
}
32+
}
33+
return left;
34+
}
35+
};

0 commit comments

Comments
 (0)