Skip to content

Commit 2666909

Browse files
committed
add 852 java
1 parent 58decfb commit 2666909

File tree

4 files changed

+170
-29
lines changed

4 files changed

+170
-29
lines changed

.github/workflows/maven.yml

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
2-
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven
1+
# # This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
2+
# # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven
33

4-
# This workflow uses actions that are not certified by GitHub.
5-
# They are provided by a third-party and are governed by
6-
# separate terms of service, privacy policy, and support
7-
# documentation.
4+
# # This workflow uses actions that are not certified by GitHub.
5+
# # They are provided by a third-party and are governed by
6+
# # separate terms of service, privacy policy, and support
7+
# # documentation.
88

9-
name: Java CI with Maven
9+
# name: Java CI with Maven
1010

11-
on:
12-
push:
13-
branches: [ "master" ]
14-
pull_request:
15-
branches: [ "master" ]
11+
# on:
12+
# push:
13+
# branches: [ "master" ]
14+
# pull_request:
15+
# branches: [ "master" ]
1616

17-
jobs:
18-
build:
17+
# jobs:
18+
# build:
1919

20-
runs-on: ubuntu-latest
20+
# runs-on: ubuntu-latest
2121

22-
steps:
23-
- uses: actions/checkout@v4
24-
- name: Set up JDK 17
25-
uses: actions/setup-java@v4
26-
with:
27-
java-version: '17'
28-
distribution: 'temurin'
29-
cache: maven
30-
- name: Build with Maven
31-
run: cd leetcode_java && mvn package #cd leetcode_java && mvn -B package --file pom.xml
22+
# steps:
23+
# - uses: actions/checkout@v4
24+
# - name: Set up JDK 17
25+
# uses: actions/setup-java@v4
26+
# with:
27+
# java-version: '17'
28+
# distribution: 'temurin'
29+
# cache: maven
30+
# - name: Build with Maven
31+
# run: cd leetcode_java && mvn package #cd leetcode_java && mvn -B package --file pom.xml
3232

33-
# Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
34-
- name: Update dependency graph
35-
uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6
33+
# # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
34+
# - name: Update dependency graph
35+
# uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@
877877
540|[Single Element in a Sorted Array](https://leetcode.com/problems/dsingle-element-in-a-sorted-array/)| [Python](./leetcode_python/Binary_Search/single-element-in-a-sorted-array.py) | _O(logn)_ | _O(1)_ | Medium | | OK*
878878
658 | [Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements/) | [Python](./leetcode_python/Binary_Search/find-k-closest-elements.py) | _O(logn + k)_ | _O(1)_ | Medium |`good trick`, sorting, stack, `two pointers`,`binary search`,`amazon`,`fb`, google| AGAIN ************** (5) (MUST)
879879
744 | [Find Smallest Letter Greater Than Target](https://leetcode.com/problems/find-smallest-letter-greater-than-target/) | [Python](./leetcode_python/Binary_Search/find-smallest-letter-greater-than-target.py) | _O(logn)_ | _O(1)_ | Easy | | OK*
880-
852 | [Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/) | [Python](./leetcode_python/Binary_Search/peak-index-in-a-mountain-array.py) | _O(logn)_ | _O(1)_ | Medium | LC 162 Find Peak Element, `amazon`| OK* (2)
880+
852 | [Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/) | [Python](./leetcode_python/Binary_Search/peak-index-in-a-mountain-array.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BinarySearch/PeakIndexInAMountainArray.java) | _O(logn)_ | _O(1)_ | Medium | LC 162 Find Peak Element, `amazon`, google| OK* (2)
881881
875 | [Koko Eating Bananas](https://leetcode.com/problems/koko-eating-bananas/) | [Python](./leetcode_python/Binary_Search/koko-eating-bananas.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BinarySearch/KokoEatingBananas.java) | _O(nlogr)_ | _O(1)_ | Medium | binary search, left boundary, good basic| AGAIN******* (4)(MUST)
882882
894| [All Possible Full Binary Trees](https://leetcode.com/problems/all-possible-full-binary-trees/) | [Python](./leetcode_python/Binary_Search/all-possible-full-binary-trees.py) | _O(n * 4^n / n^(3/2))_ | _O(n * 4^n / n^(3/2))_ | Medium || AGAIN (not start)
883883
911| [Online Election](https://leetcode.com/problems/online-election/) | [Python](./leetcode_python/Binary_Search/online-election.py)| ctor: _O(n)_<br> query : _O(logn)_ | _O(n)_ | Medium || AGAIN (not start)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package LeetCodeJava.BinarySearch;
2+
3+
// https://leetcode.com/problems/peak-index-in-a-mountain-array/description/
4+
// https://leetcode.cn/problems/peak-index-in-a-mountain-array/description/
5+
/**
6+
* 852. Peak Index in a Mountain Array
7+
* Solved
8+
* Medium
9+
* Topics
10+
* Companies
11+
* You are given an integer mountain array arr of length n where the values increase to a peak element and then decrease.
12+
*
13+
* Return the index of the peak element.
14+
*
15+
* Your task is to solve it in O(log(n)) time complexity.
16+
*
17+
*
18+
*
19+
* Example 1:
20+
*
21+
* Input: arr = [0,1,0]
22+
*
23+
* Output: 1
24+
*
25+
* Example 2:
26+
*
27+
* Input: arr = [0,2,1,0]
28+
*
29+
* Output: 1
30+
*
31+
* Example 3:
32+
*
33+
* Input: arr = [0,10,5,2]
34+
*
35+
* Output: 1
36+
*
37+
*
38+
*
39+
* Constraints:
40+
*
41+
* 3 <= arr.length <= 105
42+
* 0 <= arr[i] <= 106
43+
* arr is guaranteed to be a mountain array.
44+
* Seen this question in a real interview before?
45+
* 1/5
46+
* Yes
47+
* No
48+
* Accepted
49+
* 928.3K
50+
* Submissions
51+
* 1.4M
52+
* Acceptance Rate
53+
* 67.9%
54+
* Topics
55+
* Companies
56+
* Similar Questions
57+
* Discussion (183)
58+
*
59+
*
60+
*/
61+
public class PeakIndexInAMountainArray {
62+
63+
// V0
64+
// public int peakIndexInMountainArray(int[] arr) {
65+
//
66+
// }
67+
// V1
68+
// V2
69+
}

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2244,4 +2244,76 @@ private boolean isStrobogrammatic(String x){
22442244
return true;
22452245
}
22462246

2247+
// LC 852
2248+
// https://leetcode.com/problems/peak-index-in-a-mountain-array/
2249+
/**
2250+
*
2251+
*
2252+
* Example 1:
2253+
*
2254+
* Input: arr = [0,1,0]
2255+
*
2256+
* Output: 1
2257+
*
2258+
* Example 2:
2259+
*
2260+
* Input: arr = [0,2,1,0]
2261+
*
2262+
* Output: 1
2263+
*
2264+
* Example 3:
2265+
*
2266+
* Input: arr = [0,10,5,2]
2267+
*
2268+
* Output: 1
2269+
*
2270+
* Exp 1 : [0,3,2,1,0] -> 1
2271+
*
2272+
* IDEA: BINARY SEARCH
2273+
*
2274+
* -> if there is a peak at index i
2275+
* then 0 - i-1 MUST be increasing
2276+
*
2277+
*/
2278+
public int peakIndexInMountainArray(int[] arr) {
2279+
2280+
// edge
2281+
int maxIdx = -1;
2282+
int maxVal = -1;
2283+
if (arr.length == 3){
2284+
for(int i = 0; i < arr.length; i++){
2285+
if(arr[i] > maxVal){
2286+
maxVal = arr[i];
2287+
maxIdx = i;
2288+
}
2289+
}
2290+
return maxIdx;
2291+
}
2292+
2293+
// binary search
2294+
int l = 0;
2295+
int r = arr.length - 1;
2296+
while (r >= l){
2297+
int mid = (l + r) / 2;
2298+
2299+
// case 1) cur > left and cur > right (find peak)
2300+
if (arr[mid] > arr[mid-1] && arr[mid] > arr[mid+1]){
2301+
return mid;
2302+
}
2303+
// Exp 1 : [0,0,0, 3,2,1,0] -> 1
2304+
// case 2) cur < left && cur > left most
2305+
else if (arr[mid] < arr[mid-1] && arr[mid] >= arr[l]){
2306+
l = mid + 1;
2307+
}
2308+
// case 3) cur < right and cur > right most
2309+
else if (arr[mid] < arr[mid-1] && arr[mid] >= arr[r]){
2310+
r = mid - 1;
2311+
}
2312+
2313+
return 0;
2314+
}
2315+
2316+
return -1;
2317+
}
2318+
22472319
}

0 commit comments

Comments
 (0)