Skip to content

Commit 1984deb

Browse files
Fixes.
1 parent d780e3a commit 1984deb

4 files changed

Lines changed: 44 additions & 86 deletions

File tree

1 Leetcode Easy Problems/Leetcode 278 First Bad Version.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ public static void main(String[] args) {
4747
}
4848

4949
public int firstBadVersion(int n) {
50-
int beg = 1, end = n;
50+
long beg = 1, end = n;
5151
while (beg <= end) {
52-
int mid = (beg + end) / 2;
52+
int mid = (int) ((beg + end) / 2);
5353
if (isBadVersion(mid)) {
5454
end = mid - 1;
5555
} else {
5656
beg = mid + 1;
5757
}
5858
}
59-
return end + 1;
59+
return (int) end + 1;
6060
}
6161

6262
public int firstBadVersionLinearSearch(int n) {

1 Leetcode Easy Problems/Leetcode 374 Guess Number Higher or Lower.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@ public static void main(String[] args) {
4646
}
4747

4848
public int guessNumber(int n) {
49-
int beg = 1, end = n;
49+
long beg = 1, end = n;
5050
while (beg <= end) {
51-
int mid = (beg + end) / 2;
52-
int res = guess(mid);
53-
if (res == 0)
54-
return mid;
55-
if (res == -1)
56-
end = mid - 1;
57-
else
58-
beg = mid + 1;
51+
int mid = (int) ((beg + end) / 2);
52+
int res = guess(mid);
53+
if (res == 0)
54+
return mid;
55+
if (res == -1)
56+
end = mid - 1;
57+
else
58+
beg = mid + 1;
5959
}
6060
return -1;
6161
}

1 Leetcode Easy Problems/Leetcode 441 Arranging Coins.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@ public static void main(String[] args) {
3131
}
3232

3333
private int arrangeCoins(int n) {
34-
int begin = 0;
35-
int end = n;
34+
long begin = 0;
35+
long end = (int) Math.sqrt(2 * (long) n); // Adjust the initial end value
3636
while (begin <= end) {
37-
int mid = (begin + end) / 2;
38-
int coinsUsed = (mid * (mid + 1)) / 2;
39-
if (coinsUsed == n)
40-
return mid;
41-
if (coinsUsed < n) {
42-
begin = mid + 1;
43-
} else {
44-
end = mid - 1;
45-
}
37+
long mid = (begin + end) / 2;
38+
long coinsUsed = (mid * (mid + 1)) / 2;
39+
if (coinsUsed == n)
40+
return (int) mid;
41+
if (coinsUsed < n) {
42+
begin = mid + 1;
43+
} else {
44+
end = mid - 1;
45+
}
4646
}
47-
return end;
47+
return (int) end;
4848
}
4949
}
Lines changed: 20 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,34 @@
1-
2-
/**
3-
* There is an integer array nums sorted in ascending
4-
* order (with distinct values).
5-
*
6-
* Prior to being passed to your function, nums is
7-
* possibly rotated at an unknown pivot index
8-
* k (1 <= k < nums.length) such that the resulting
9-
* array is [nums[k], nums[k+1], ..., nums[n-1],
10-
* nums[0], nums[1], ..., nums[k-1]] (0-indexed).
11-
* For example, [0,1,2,4,5,6,7] might be rotated
12-
* at pivot index 3 and become [4,5,6,7,0,1,2].
13-
*
14-
* Given the array nums after the possible rotation
15-
* and an integer target, return the index of
16-
* target if it is in nums, or -1 if it is not in
17-
* nums.
18-
*
19-
* You must write an algorithm with O(log n) runtime
20-
* complexity.
21-
*
22-
*
23-
* Example 1: 0,1,2,3,4,5,6
24-
* Input: nums = [4,5,6,7,0,1,2], target = 0
25-
* Output: 4
26-
*
27-
* Example 2:
28-
* Input: nums = [4,5,6,7,0,1,2], target = 3
29-
* Output: -1
30-
*
31-
* Example 3:
32-
* Input: nums = [1], target = 0
33-
* Output: -1
34-
*
35-
*
36-
* Constraints:
37-
* 1 <= nums.length <= 5000
38-
* -104 <= nums[i] <= 104
39-
* All values of nums are unique.
40-
* nums is an ascending array that is possibly rotated.
41-
* -104 <= target <= 104
42-
*/
43-
class SearchInRotatedSortedArray {
44-
public static void main(String[] args) {
45-
SearchInRotatedSortedArray search = new SearchInRotatedSortedArray();
46-
int[] nums = new int[] { 4, 5, 6, 7, 0, 1, 2 };
47-
System.out.println("Found at:"
48-
+ search.search(nums, 3));
49-
}
50-
51-
int search(int[] nums, int target) {
1+
class Solution {
2+
public int search(int[] nums, int target) {
523
int beg = 0, end = nums.length - 1;
53-
while (beg <= end) {
4+
5+
// First, find the smallest element's index (pivot index)
6+
while (beg < end) {
547
int mid = (beg + end) / 2;
55-
if (nums[mid] > nums[end])
56-
beg = mid + 1;
57-
else
58-
end = mid - 1;
8+
if (nums[mid] > nums[end]) {
9+
beg = mid + 1; // pivot must be to the right of mid
10+
} else {
11+
end = mid; // pivot could be at mid, or to the left of mid
12+
}
5913
}
60-
int shift = end + 1;
14+
// Now, beg == end == pivot index (smallest element index)
15+
int pivot = beg;
6116

17+
// Reset beg and end for standard binary search, adjusted by pivot
6218
beg = 0;
6319
end = nums.length - 1;
6420
while (beg <= end) {
6521
int mid = (beg + end) / 2;
66-
int realMid = (mid + shift) % nums.length;
67-
if (nums[realMid] == target)
22+
int realMid = (mid + pivot) % nums.length; // Adjust mid to account for rotation
23+
24+
if (nums[realMid] == target) {
6825
return realMid;
69-
else if (nums[realMid] < target)
26+
} else if (nums[realMid] < target) {
7027
beg = mid + 1;
71-
else
28+
} else {
7229
end = mid - 1;
30+
}
7331
}
74-
return -1;
32+
return -1;
7533
}
7634
}

0 commit comments

Comments
 (0)