Skip to content

Commit 4085845

Browse files
committed
add 34 java, progress
1 parent 7947894 commit 4085845

File tree

6 files changed

+253
-10
lines changed

6 files changed

+253
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@
855855
| # | Title | Solution | Time | Space | Difficulty | Tag, Note | Status|
856856
|-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----|
857857
033| [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [Python](./leetcode_python/Binary_Search/search-in-rotated-sorted-array.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BinarySearch/SearchInRotatedSortedArray.java) ||| Medium |Curated Top 75, `binary search`,`good basic` ,check `# 81 Search in Rotated Sorted Array II`, binary search, rotation array, `UBER`,`amazon`,`fb`| AGAIN************ (6) (MUST)
858-
034 | [Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/)| [Python](./leetcode_python/Binary_Search/find-first-and-last-position-of-element-in-sorted-array.py)| _O(n)_ | _O(1)_ | Medium | binary search, LC top 100 like, `amazon`, `fb`, google, apple, uber | AGAIN************** (4) (MUST)
858+
034 | [Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/)| [Python](./leetcode_python/Binary_Search/find-first-and-last-position-of-element-in-sorted-array.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BinarySearch/FindFirstAndLastPositionOfElementInSortedArray.java)| _O(n)_ | _O(1)_ | Medium | binary search, LC top 100 like, `amazon`, `fb`, google, apple, uber | AGAIN************** (4) (MUST)
859859
034| [Search for a Range](https://leetcode.com/problems/search-for-a-range/) |[Python](./leetcode_python/Binary_Search/search-for-a-range.py) | _O(logn)_ | _O(1)_ | Medium || AGAIN*
860860
035| [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [Python](./leetcode_python/Binary_Search/search-insert-position.py) | _O(logn)_ | _O(1)_ | Medium || AGAIN
861861
069| [Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [Python](./leetcode_python/Binary_Search/sqrtx.py) | _O(logn)_ | _O(1)_ | Medium |math, binary search,`amazon`,`fb`| OK* (4)

data/progress.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Progress
22

3+
# 2024-11-30
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-11-26
47
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
58

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20241130: 34
12
20241126: 722,380
23
20241125: 33,81
34
20241124: 153

data/to_review.txt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
2025-01-24 -> ['34']
12
2025-01-20 -> ['722,380']
23
2025-01-19 -> ['33,81']
34
2025-01-17 -> ['253']
@@ -7,7 +8,7 @@
78
2025-01-11 -> ['394']
89
2025-01-10 -> ['833,950']
910
2025-01-04 -> ['53,210,207']
10-
2025-01-03 -> ['444']
11+
2025-01-03 -> ['34', '444']
1112
2025-01-02 -> ['1188,130,855(again)']
1213
2024-12-30 -> ['722,380']
1314
2024-12-29 -> ['33,81']
@@ -16,23 +17,24 @@
1617
2024-12-26 -> ['776,31']
1718
2024-12-25 -> ['004(todo),34(todo),162(todo),275(todo)']
1819
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
19-
2024-12-21 -> ['394', '855,846']
20+
2024-12-21 -> ['34', '394', '855,846']
2021
2024-12-20 -> ['833,950', '932']
2122
2024-12-18 -> ['951,792']
2223
2024-12-17 -> ['722,380']
2324
2024-12-16 -> ['33,81']
2425
2024-12-14 -> ['253', '53,210,207', '163,1048']
25-
2024-12-13 -> ['776,31', '444', '298,729']
26+
2024-12-13 -> ['34', '776,31', '444', '298,729']
2627
2024-12-12 -> ['004(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
2728
2024-12-11 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2829
2024-12-09 -> ['722,380']
29-
2024-12-08 -> ['33,81', '394', '737']
30+
2024-12-08 -> ['34', '33,81', '394', '737']
3031
2024-12-07 -> ['833,950', '900', '686,734,737']
3132
2024-12-06 -> ['253', '26,27', '802,1197,26', '353']
32-
2024-12-05 -> ['776,31', '528,334']
33+
2024-12-05 -> ['34', '776,31', '528,334']
3334
2024-12-04 -> ['722,380', '004(todo),34(todo),162(todo),275(todo)']
34-
2024-12-03 -> ['33,81', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
35-
2024-12-01 -> ['722,380', '253', '53,210,207']
35+
2024-12-03 -> ['34', '33,81', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
36+
2024-12-02 -> ['34']
37+
2024-12-01 -> ['34', '722,380', '253', '53,210,207']
3638
2024-11-30 -> ['33,81', '776,31', '394', '444', '855,846', '1145,1219']
3739
2024-11-29 -> ['722,380', '004(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
3840
2024-11-28 -> ['722,380', '33,81', '253', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package LeetCodeJava.BinarySearch;
2+
3+
// https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/description/
4+
/**
5+
* 34. Find First and Last Position of Element in Sorted Array
6+
* Solved
7+
* Medium
8+
* Topics
9+
* Companies
10+
* Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.
11+
*
12+
* If target is not found in the array, return [-1, -1].
13+
*
14+
* You must write an algorithm with O(log n) runtime complexity.
15+
*
16+
*
17+
*
18+
* Example 1:
19+
*
20+
* Input: nums = [5,7,7,8,8,10], target = 8
21+
* Output: [3,4]
22+
* Example 2:
23+
*
24+
* Input: nums = [5,7,7,8,8,10], target = 6
25+
* Output: [-1,-1]
26+
* Example 3:
27+
*
28+
* Input: nums = [], target = 0
29+
* Output: [-1,-1]
30+
*
31+
*
32+
* Constraints:
33+
*
34+
* 0 <= nums.length <= 105
35+
* -109 <= nums[i] <= 109
36+
* nums is a non-decreasing array.
37+
* -109 <= target <= 109
38+
*
39+
*/
40+
public class FindFirstAndLastPositionOfElementInSortedArray {
41+
42+
// V0
43+
// TODO : implement
44+
// public int[] searchRange(int[] nums, int target) {
45+
//
46+
// }
47+
48+
// V1
49+
// IDEA : BINARY SEARCH
50+
// https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/solutions/14734/easy-java-o-logn-solution/
51+
public int[] searchRange_1(int[] nums, int target) {
52+
int[] result = new int[2];
53+
result[0] = findFirst(nums, target);
54+
result[1] = findLast(nums, target);
55+
return result;
56+
}
57+
58+
private int findFirst(int[] nums, int target) {
59+
int idx = -1;
60+
int start = 0;
61+
int end = nums.length - 1;
62+
while (start <= end) {
63+
int mid = (start + end) / 2;
64+
if (nums[mid] >= target) {
65+
end = mid - 1;
66+
} else {
67+
start = mid + 1;
68+
}
69+
if (nums[mid] == target)
70+
idx = mid;
71+
}
72+
return idx;
73+
}
74+
75+
private int findLast(int[] nums, int target) {
76+
int idx = -1;
77+
int start = 0;
78+
int end = nums.length - 1;
79+
while (start <= end) {
80+
int mid = (start + end) / 2;
81+
if (nums[mid] <= target) {
82+
start = mid + 1;
83+
} else {
84+
end = mid - 1;
85+
}
86+
if (nums[mid] == target)
87+
idx = mid;
88+
}
89+
return idx;
90+
}
91+
92+
// V2
93+
// IDEA : Binary Tree
94+
// https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/solutions/14701/a-very-simple-java-solution-with-only-one-binary-search-algorithm/
95+
public int[] searchRange_2(int[] A, int target) {
96+
int start = firstGreaterEqual(A, target);
97+
if (start == A.length || A[start] != target) {
98+
return new int[]{-1, -1};
99+
}
100+
return new int[]{start, firstGreaterEqual(A, target + 1) - 1};
101+
}
102+
103+
//find the first number that is greater than or equal to target.
104+
//could return A.length if target is greater than A[A.length-1].
105+
//actually this is the same as lower_bound in C++ STL.
106+
private static int firstGreaterEqual(int[] A, int target) {
107+
int low = 0, high = A.length;
108+
while (low < high) {
109+
int mid = low + ((high - low) >> 1);
110+
//low <= mid < high
111+
if (A[mid] < target) {
112+
low = mid + 1;
113+
} else {
114+
//should not be mid-1 when A[mid]==target.
115+
//could be mid even if A[mid]>target because mid<high.
116+
high = mid;
117+
}
118+
}
119+
return low;
120+
}
121+
122+
// V3
123+
// IDEA : binary Search
124+
// https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/solutions/5378191/video-binary-search-solution/
125+
public int[] searchRange_3(int[] nums, int target) {
126+
int[] result = { -1, -1 };
127+
int left = binarySearch(nums, target, true);
128+
int right = binarySearch(nums, target, false);
129+
result[0] = left;
130+
result[1] = right;
131+
return result;
132+
}
133+
134+
private int binarySearch(int[] nums, int target, boolean isSearchingLeft) {
135+
int left = 0;
136+
int right = nums.length - 1;
137+
int idx = -1;
138+
139+
while (left <= right) {
140+
int mid = left + (right - left) / 2;
141+
142+
if (nums[mid] > target) {
143+
right = mid - 1;
144+
} else if (nums[mid] < target) {
145+
left = mid + 1;
146+
} else {
147+
idx = mid;
148+
if (isSearchingLeft) {
149+
right = mid - 1;
150+
} else {
151+
left = mid + 1;
152+
}
153+
}
154+
}
155+
156+
return idx;
157+
}
158+
}

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3537,6 +3537,7 @@ private String getMultiplyStr(String cur, Integer multiply) {
35373537
// IDEA : DFS
35383538
TreeNode leftTree = new TreeNode();
35393539
TreeNode rightTree = new TreeNode();
3540+
35403541
public TreeNode[] splitBST(TreeNode root, int target) {
35413542
TreeNode[] res = new TreeNode[2];
35423543

@@ -3563,7 +3564,7 @@ private TreeNode[] buildTreeDfs(TreeNode root, int target){
35633564
leftTree.left = root.left;
35643565
buildTreeDfs(leftTree, target);
35653566
}
3566-
return null;
3567+
return null;
35673568
}
35683569

35693570
// public TreeNode[] splitBST(TreeNode root, int target) {
@@ -3612,6 +3613,7 @@ private TreeNode buildTree(List<Integer> input){
36123613
// LC 31
36133614
// https://leetcode.com/problems/next-permutation/description/
36143615
// 5.39 pm - 6.15 pm
3616+
36153617
/**
36163618
*
36173619
* exp 1) [1,2,3] -> [1,3,2]
@@ -3666,6 +3668,7 @@ public void nextPermutation(int[] nums) {
36663668
// LC 253
36673669
// https://leetcode.ca/all/253.html
36683670
// 4.57 pm - 5.10 pm
3671+
36693672
/**
36703673
* [[s1,e1],[s2,e2],...]
36713674
*
@@ -3711,6 +3714,7 @@ public String getStatus() {
37113714
return status;
37123715
}
37133716
}
3717+
37143718
public int minMeetingRooms(int[][] intervals) {
37153719
if (intervals.length <= 1){
37163720
return intervals.length;
@@ -3759,6 +3763,7 @@ public int minMeetingRooms(int[][] intervals) {
37593763

37603764
// LC 153
37613765
// 5.07 PM - 5.20 PM
3766+
37623767
/**
37633768
*
37643769
* Input : [4,5,6,7,0,1,2]
@@ -3814,6 +3819,7 @@ public int findMin(int[] nums){
38143819

38153820
return -1;
38163821
}
3822+
38173823
/**
38183824
*
38193825
* [4,5,6,7,0,1,2]
@@ -4010,7 +4016,7 @@ public boolean search(int[] nums, int target) {
40104016
}
40114017
// else
40124018
else{
4013-
left = mid + 1;
4019+
left = mid + 1;
40144020
}
40154021

40164022
}
@@ -4023,6 +4029,7 @@ public boolean search(int[] nums, int target) {
40234029
// LC 722
40244030
// https://leetcode.com/problems/remove-comments/
40254031
// 7.41 pm - 8.15 pm
4032+
40264033
/**
40274034
* Idea 1) string op
40284035
* a tmp array that collect "Not comment" element,
@@ -4104,6 +4111,78 @@ public int getRandom() {
41044111
}
41054112
}
41064113

4114+
// LC 34
4115+
// https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
4116+
// 3.10 - 3.20 pm
4117+
4118+
/**
4119+
* Idea 1) : // binary search
4120+
* <p>
4121+
* -> find mid == target
4122+
* -> then check start, end idx
4123+
*/
4124+
4125+
public int[] searchRange(int[] nums, int target) {
4126+
4127+
if (nums.length == 0) {
4128+
return new int[]{-1, -1};
4129+
}
4130+
4131+
if (nums.length == 1) {
4132+
if (nums[0] == target) {
4133+
return new int[]{0, 0};
4134+
}
4135+
return new int[]{-1, -1};
4136+
}
4137+
4138+
// binary search
4139+
int left = 0;
4140+
int right = nums.length - 1;
4141+
int mid = -1;
4142+
while (right >= left) {
4143+
mid = (left + right) / 2;
4144+
if (nums[mid] == target) {
4145+
break;
4146+
}
4147+
if (nums[mid] < target) {
4148+
left = mid + 1;
4149+
} else {
4150+
right = mid - 1;
4151+
}
4152+
}
4153+
4154+
if (nums[mid] != target) {
4155+
//mid = -1;
4156+
return new int[]{-1, -1};
4157+
}
4158+
System.out.println(">>> mid = " + mid);
4159+
4160+
// check start, end idx
4161+
int[] res = new int[2];
4162+
int start = mid;
4163+
int end = mid;
4164+
while (start >= 0 && nums[start] == target) {
4165+
start -= 1;
4166+
}
4167+
4168+
while (end < nums.length - 1 && nums[end] == target) {
4169+
end += 1;
4170+
}
4171+
4172+
// todo : optimize
4173+
if(nums[start] != target){
4174+
start += 1;
4175+
}
4176+
if (nums[end] != target){
4177+
end -= 1;
4178+
}
4179+
4180+
res[0] = start;
4181+
res[1] = end;
4182+
4183+
return res;
4184+
}
4185+
41074186
}
41084187

41094188

0 commit comments

Comments
 (0)