Skip to content

Commit a238ee1

Browse files
committed
add 932 java
1 parent 212b1a0 commit a238ee1

File tree

6 files changed

+179
-8
lines changed

6 files changed

+179
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@
299299
921 | [Minimum Add to Make Parentheses Valid](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/) | [Python](./leetcode_python/Array/minimum-add-to-make-parentheses-valid.py) | _O(n)_ | _O(1)_ | Medium |`basic`, `trick`| AGAIN**
300300
922 | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [Python](./leetcode_python/Array/sort-array-by-parity-ii.py) | _O(n)_ | _O(1)_ | Easy |`basic`| AGAIN*
301301
923 | [3Sum With Multiplicity](https://leetcode.com/problems/3sum-with-multiplicity/) | [Python](./leetcode_python/Array/3sum-with-multiplicity.py) | _O(n^2)_ | _O(n)_ | Medium || AGAIN (not start)
302-
932 | [Beautiful Array](https://leetcode.com/problems/beautiful-array/) | [Python](./leetcode_python/Array/beautiful-array.py) | _O(n)_ | _O(n)_ | Medium |`basic`, `trick`| AGAIN (not start)
302+
932 | [Beautiful Array](https://leetcode.com/problems/beautiful-array/) | [Python](./leetcode_python/Array/beautiful-array.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/BeautifulArray.java) | _O(n)_ | _O(n)_ | Medium |`basic`, `trick`, google| AGAIN** (not start) (1)
303303
941 | [Valid Mountain Array](https://leetcode.com/problems/valid-mountain-array/) | [Python](./leetcode_python/Array/valid-mountain-array.py) | _O(n)_ | _O(1)_ | Easy |`basic`, `good basic`| AGAIN*
304304
945 | [Minimum Increment to Make Array Unique](https://leetcode.com/problems/minimum-increment-to-make-array-unique/) | [Python](./leetcode_python/Array/minimum-increment-to-make-array-unique.py) | _O(nlogn)_ | _O(n)_ | Medium |`trick`, `good`| AGAIN**
305305
947 | [Most Stones Removed with Same Row or Column](https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/) | [Python](./leetcode_python/Array/most-stones-removed-with-same-row-or-column.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/MostStonesRemovedWithSameRowOrColumn.java) | _O(n)_ | _O(n)_ | Medium | Union Find , dfs, trick,google| AGAIN** (1) (not start)

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-10-26
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-10-24
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+
20241026: 932
12
20241024: 951,792
23
20241020: 163,1048
34
20241019: 298,729

data/to_review.txt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
2024-12-20 -> ['932']
12
2024-12-18 -> ['951,792']
23
2024-12-14 -> ['163,1048']
34
2024-12-13 -> ['298,729']
@@ -8,6 +9,7 @@
89
2024-12-05 -> ['528,334']
910
2024-12-03 -> ['1145']
1011
2024-11-30 -> ['1145,1219']
12+
2024-11-29 -> ['932']
1113
2024-11-27 -> ['951,792', '524,221,889']
1214
2024-11-26 -> ['743,889']
1315
2024-11-25 -> ['837']
@@ -17,25 +19,25 @@
1719
2024-11-20 -> ['939']
1820
2024-11-18 -> ['430']
1921
2024-11-17 -> ['737', '363']
20-
2024-11-16 -> ['686,734,737', '1032,844,1011']
22+
2024-11-16 -> ['932', '686,734,737', '1032,844,1011']
2123
2024-11-15 -> ['353', '947']
2224
2024-11-14 -> ['951,792', '528,334']
2325
2024-11-12 -> ['1145', '753']
2426
2024-11-11 -> ['727']
2527
2024-11-10 -> ['163,1048']
2628
2024-11-09 -> ['298,729', '1145,1219']
27-
2024-11-08 -> ['1146']
29+
2024-11-08 -> ['932', '1146']
2830
2024-11-06 -> ['951,792', '524,221,889']
2931
2024-11-05 -> ['743,889']
3032
2024-11-04 -> ['737', '837', '659']
31-
2024-11-03 -> ['686,734,737', '801,552']
33+
2024-11-03 -> ['932', '686,734,737', '801,552']
3234
2024-11-02 -> ['163,1048', '353', '981', '1057,1066,1110']
3335
2024-11-01 -> ['951,792', '298,729', '528,334', '1087']
34-
2024-10-31 -> ['1146']
36+
2024-10-31 -> ['932', '1146']
3537
2024-10-30 -> ['1145', '939']
36-
2024-10-29 -> ['951,792']
37-
2024-10-28 -> ['163,1048', '430']
38-
2024-10-27 -> ['951,792', '298,729', '737', '1145,1219', '363']
38+
2024-10-29 -> ['932', '951,792']
39+
2024-10-28 -> ['932', '163,1048', '430']
40+
2024-10-27 -> ['932', '951,792', '298,729', '737', '1145,1219', '363']
3941
2024-10-26 -> ['951,792', '1146', '686,734,737', '1032,844,1011']
4042
2024-10-25 -> ['951,792', '163,1048', '353', '947', '1110, 1055']
4143
2024-10-24 -> ['298,729', '528,334', '524,221,889']
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package LeetCodeJava.Array;
2+
3+
// https://leetcode.com/problems/beautiful-array/description/
4+
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
/**
10+
* 932. Beautiful Array
11+
* Medium
12+
* Topics
13+
* Companies
14+
* An array nums of length n is beautiful if:
15+
*
16+
* nums is a permutation of the integers in the range [1, n].
17+
* For every 0 <= i < j < n, there is no index k with i < k < j where 2 * nums[k] == nums[i] + nums[j].
18+
* Given the integer n, return any beautiful array nums of length n. There will be at least one valid answer for the given n.
19+
*
20+
*
21+
* Example 1:
22+
*
23+
* Input: n = 4
24+
* Output: [2,1,4,3]
25+
* Example 2:
26+
*
27+
* Input: n = 5
28+
* Output: [3,1,2,5,4]
29+
*
30+
*
31+
* Constraints:
32+
*
33+
* 1 <= n <= 1000
34+
*
35+
*/
36+
public class BeautifulArray {
37+
38+
// V0
39+
// public int[] beautifulArray(int n) {
40+
//
41+
// }
42+
43+
// V1
44+
// IDEA : Divide and Conquer
45+
// https://leetcode.com/problems/beautiful-array/editorial/
46+
Map<Integer, int[]> memo;
47+
public int[] beautifulArray_1(int N) {
48+
memo = new HashMap();
49+
return f(N);
50+
}
51+
52+
public int[] f(int N) {
53+
if (memo.containsKey(N))
54+
return memo.get(N);
55+
56+
int[] ans = new int[N];
57+
if (N == 1) {
58+
ans[0] = 1;
59+
} else {
60+
int t = 0;
61+
for (int x: f((N+1)/2)) // odds
62+
ans[t++] = 2*x - 1;
63+
for (int x: f(N/2)) // evens
64+
ans[t++] = 2*x;
65+
}
66+
memo.put(N, ans);
67+
return ans;
68+
}
69+
70+
71+
// V2
72+
// IDEA : Divide and Conquer
73+
// https://leetcode.com/problems/beautiful-array/solutions/1373064/java-clear-thinking-process-divide-and-conquer/
74+
public int[] beautifulArray(int n) {
75+
int[] ans = new int[n];
76+
for(int i = 0; i < n; i++){
77+
ans[i] = i+1;
78+
}
79+
recursion(ans, 0, n-1);
80+
return ans;
81+
}
82+
83+
public void recursion(int[] arr, int left, int right){
84+
if(left >= right)
85+
return;
86+
ArrayList<Integer> l = new ArrayList<>();
87+
ArrayList<Integer> r = new ArrayList<>();
88+
89+
boolean alt = true;// Not worry about whether the factor of the interval is even or odd too much, they can be grouped by
90+
// just picking one and skip one
91+
92+
for(int i = left; i <= right; i++){ // picking the elements and put them into the two groups
93+
if(alt)
94+
l.add(arr[i]);
95+
else
96+
r.add(arr[i]);
97+
alt = !alt;
98+
}
99+
100+
for(int i = left; i <= right; i++){ // merging them into the final array
101+
if(!l.isEmpty())
102+
arr[i] = l.remove(0);
103+
else
104+
arr[i] = r.remove(0);
105+
}
106+
recursion(arr, left, (right+left)/2);
107+
recursion(arr, (left+right)/2+1, right);
108+
}
109+
110+
// V3-1
111+
// https://leetcode.com/problems/beautiful-array/solutions/1368607/java-0-ms-100-00-fast-explained-2-solutions/
112+
public int[] beautifulArray_3_1(int n) {
113+
int[] answer = new int[n];
114+
if(n == 1) {
115+
answer[0] = 1;
116+
return answer;
117+
}
118+
int[] right =beautifulArray(n/2);
119+
int[] left = beautifulArray((n+1)/2);
120+
121+
for(int i=left.length; i<n; i++) { //This loop adds all even elements at end
122+
answer[i] = right[i-left.length] * 2;
123+
}
124+
for(int i=0; i<left.length; i++) { //This loop adds all odd elements at start
125+
answer[i] = left[i] * 2 - 1;
126+
}
127+
return answer;
128+
}
129+
130+
// V3-2
131+
// https://leetcode.com/problems/beautiful-array/solutions/1368607/java-0-ms-100-00-fast-explained-2-solutions/
132+
public int[] beautifulArray_3_2(int n) {
133+
ArrayList<Integer> answer=new ArrayList<>();
134+
answer.add(1);
135+
while(answer.size()<n){
136+
ArrayList<Integer> temp=new ArrayList<>();
137+
for(int element:answer)
138+
if(2*element-1<=n)
139+
temp.add(element*2-1);
140+
141+
for(int element:answer)
142+
if(2*element<=n)
143+
temp.add(element*2);
144+
145+
//in different loops because we want to maintain order
146+
answer=temp;
147+
}
148+
return answer.stream().mapToInt(i -> i).toArray();
149+
}
150+
151+
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,6 +2065,20 @@ private boolean isSubsequences(String word, String s) {
20652065
// return i == word.length() - 1;
20662066
// }
20672067

2068+
// LC 932
2069+
// https://leetcode.com/problems/beautiful-array/
2070+
/**
2071+
*
2072+
* For every 0 <= i < j < n,
2073+
* there is no index k with i < k < j
2074+
* where 2 * nums[k] == nums[i] + nums[j].
2075+
*
2076+
*/
2077+
public int[] beautifulArray(int n) {
2078+
2079+
return null;
2080+
}
2081+
20682082
}
20692083

20702084

0 commit comments

Comments
 (0)