-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths0029_divide_two_integers.rs
276 lines (233 loc) · 8.44 KB
/
s0029_divide_two_integers.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#![allow(unused)]
pub struct Solution {}
use std::i32::{MAX, MIN};
const HALF_MIN: i32 = MIN / 2;
impl Solution {
//Time O(N) Space O(1)
//Time Limit Exceeded
pub fn divide_bruteforce(mut dividend: i32, mut divisor: i32) -> i32 {
if dividend == MIN && divisor == -1 {
return MAX;
}
let mut negatives = 2;
if dividend > 0 {
negatives -= 1;
dividend = -dividend;
}
if divisor > 0 {
negatives -= 1;
divisor = -divisor;
}
let mut quotient = 0;
while dividend - divisor <= 0 {
dividend -= divisor;
quotient -= 1;
}
if negatives != 1 {
quotient = -quotient;
}
quotient
}
// Time O(log^2 n) space O(1)
pub fn divide_better(mut dividend: i32, mut divisor: i32) -> i32 {
if dividend == MIN && divisor == -1 {
return MAX;
}
let mut negatives = 2;
if dividend > 0 {
negatives -= 1;
dividend = -dividend;
}
if divisor > 0 {
negatives -= 1;
divisor = -divisor;
}
let mut quotient = 0;
/* Once the divisor is bigger than the current dividend,
* we can't fit any more copies of the divisor into it. */
while dividend - divisor <= 0 {
/* We know it'll fit at least once as divivend >= divisor.
* Note: We use a negative powerOfTwo as it's possible we might have
* the case divide(INT_MIN, -1). */
let mut power_two = -1;
let mut value = divisor;
/* Check if double the current value is too big. If not, continue doubling.
* If it is too big, stop doubling and continue with the next step */
while value >= HALF_MIN && value + value >= dividend {
value += value;
power_two += power_two;
}
// Remove value so far so that we can continue the process with remainder.
dividend -= value;
// We have been able to subtract divisor another powerOfTwo times.
quotient += power_two;
}
/* If there was originally one negative sign, then
* the quotient remains negative. Otherwise, switch
* it to positive. */
if negatives != 1 {
quotient = -quotient;
}
quotient
}
// Time O(log n) space O(log n)
// Accepted
pub fn divide_memorize(mut dividend: i32, mut divisor: i32) -> i32 {
if dividend == MIN && divisor == -1 {
return MAX;
}
let mut negatives = 2;
if dividend > 0 {
negatives -= 1;
dividend = -dividend;
}
if divisor > 0 {
negatives -= 1;
divisor = -divisor;
}
let mut doubles = vec![];
let mut powers_two = vec![];
/* Nothing too exciting here, we're just making a list of doubles of 1 and
* the divisor. This is pretty much the same as Approach 2, except we're
* actually storing the values this time. */
let mut power_two = -1;
while dividend <= divisor {
doubles.push(divisor);
powers_two.push(power_two);
if divisor < HALF_MIN {
break;
}
divisor += divisor;
power_two += power_two;
}
let mut quotient = 0;
/* Go from largest double to smallest, checking if the current double fits.
* into the remainder of the dividend. */
for i in (0..doubles.len()).rev() {
if doubles[i] >= dividend {
// If it does fit, add the current powerOfTwo to the quotient.
quotient += powers_two[i];
// Update dividend to take into account the bit we've now removed.
dividend -= doubles[i];
}
}
/* If there was originally one negative sign, then
* the quotient remains negative. Otherwise, switch
* it to positive. */
if negatives != 1 {
quotient = -quotient;
}
quotient
}
// Time O(log n) space O(log 1)
// Accepted
pub fn divide_constantspace(mut dividend: i32, mut divisor: i32) -> i32 {
if dividend == MIN && divisor == -1 {
return MAX;
}
let mut negatives = 2;
if dividend > 0 {
negatives -= 1;
dividend = -dividend;
}
if divisor > 0 {
negatives -= 1;
divisor = -divisor;
}
/* Nothing too exciting here, we're just making a list of doubles of 1 and
* the divisor. This is pretty much the same as Approach 2, except we're
* actually storing the values this time. */
let mut highest_power_two = -1;
let mut highest_double = divisor;
while highest_double >= HALF_MIN && dividend <= highest_double + highest_double {
highest_double += highest_double;
highest_power_two += highest_power_two;
}
/* In the second loop, we work out which powers of two fit in, by
* halving highestDouble and highestPowerOfTwo repeatedly.
* We can do this using bit shifting so that we don't break the
* rules of the question :-) */
let mut quotient = 0;
while dividend <= divisor {
if dividend <= highest_double {
quotient += highest_power_two;
dividend -= highest_double;
}
/* We know that these are always even, so no need to worry about the
* annoying "bit-shift-odd-negative-number" case. */
highest_double >>= 1;
highest_power_two >>= 1;
}
/* If there was originally one negative sign, then
* the quotient remains negative. Otherwise, switch
* it to positive. */
if negatives != 1 {
quotient = -quotient;
}
quotient
}
// O(log n) O(1)
pub fn divide(mut dividend: i32, mut divisor: i32) -> i32 {
if dividend == MIN && divisor == -1 {
return MAX;
}
if dividend == MIN && divisor == 1 {
return MIN;
}
let mut negatives = 2;
if dividend > 0 {
negatives -= 1;
dividend = -dividend;
}
if divisor > 0 {
negatives -= 1;
divisor = -divisor;
}
/* We want to find the largest doubling of the divisor in the negative 32-bit
* integer range that could fit into the dividend.
* Note if it would cause an overflow by being less than HALF_INT_MIN,
* then we just stop as we know double it would not fit into INT_MIN anyway. */
let mut max_bit = 0;
while divisor >= HALF_MIN && divisor + divisor >= dividend {
max_bit += 1;
divisor += divisor;
}
let mut quotient = 0;
/* We start from the biggest bit and shift our divisor to the right
* until we can't shift it any further */
for bit in (0..max_bit+1).rev() {
/* If the divisor fits into the dividend, then we should set the current
* bit to 1. We can do this by subtracting a 1 shifted by the appropriate
* number of bits. */
if divisor >= dividend {
quotient -= (1 << bit);
/* Remove the current divisor from the dividend, as we've now
* considered this part. */
dividend -= divisor;
}
/* Shift the divisor to the right so that it's in the right place
* for the next positon we're checking at. */
// We can no longer assume the divisor, that we're right shifting,
// is always an even number. Therefore, we need to add 1 before doing the right shift.
divisor = (divisor + 1) >> 1;
}
/* If there was originally one negative sign, then
* the quotient remains negative. Otherwise, switch
* it to positive. */
if negatives != 1 {
quotient = -quotient;
}
quotient
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_67() {
assert_eq!(Solution::divide(10, 3), 3);
assert_eq!(Solution::divide(1, 1), 1);
assert_eq!(Solution::divide(0, 1), 0);
assert_eq!(Solution::divide(7, -3), -2);
}
}