Skip to content

Commit 377374c

Browse files
author
yennj12
committed
add 189 java
1 parent c9a9e65 commit 377374c

File tree

3 files changed

+185
-1
lines changed

3 files changed

+185
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@
228228
157 | [Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4/) | [Python](./leetcode_python/Array/read-n-characters-given-read4.py) | _O(n)_ | _O(1)_ | Easy |🔒, buffer, `google`, `amazon`, `fb`| AGAIN**** (3)
229229
163 | [Missing Ranges](https://leetcode.com/problems/missing-ranges/)| [Python](./leetcode_python/Array/missing-ranges.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/MissingRanges.java) | _O(n)_ | _O(1)_ | Medium| two pointer, 🔒, `google`, `amazon` | OK******* (4)
230230
169 | [Majority Element](https://leetcode.com/problems/majority-element/) |[Python](./leetcode_python/Array/majority-element.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/MajorityElement.java) | _O(n)_ | _O(1)_ | Easy |`amazon`| OK*
231-
189 | [Rotate Array](https://leetcode.com/problems/rotate-array/) | [Python](./leetcode_python/Array/rotate-array.py) | _O(n)_ | _O(1)_ | Medium |array, k % len(nums), good basic, `amazon`| OK* (7) (but again)
231+
189 | [Rotate Array](https://leetcode.com/problems/rotate-array/)| [Python](./leetcode_python/Array/rotate-array.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/RotateArray.java)| _O(n)_ | _O(1)_ | Medium |array, k % len(nums), good basic, `amazon`| OK* (7) (but again)
232232
209 | [Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/) | [Python](./leetcode_python/Array/minimum-size-subarray-sum.py) | _O(n)_ | _O(1)_ | Medium | good basic, sliding window, `Binary Search`, `trick`,`2 pointers`, `fb` | OK********** (5) (but again)
233233
215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [Python](./leetcode_python/Array/kth-largest-element-in-an-array.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/KthLargestElementInAnArray.java) | _O(n)_ ~ _O(n^2)_ | _O(1)_ | Medium | EPI, `quick sort`, bubble sort, sort, `amazon`,`fb`| OK* (2)
234234
228 | [Summary Ranges](https://leetcode.com/problems/summary-ranges/) | [Python](./leetcode_python/Array/summary-ranges.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Array/SummaryRanges.java) | _O(n)_ | _O(1)_ | Easy |check `# 163 Missing Ranges`, `basic`, array, `google`| OK*** (4)
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package LeetCodeJava.Array;
2+
3+
// https://leetcode.com/problems/rotate-array/description/
4+
5+
import java.util.ArrayList;
6+
import java.util.Deque;
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
10+
/**
11+
* 189. Rotate Array
12+
* Solved
13+
* Medium
14+
* Topics
15+
* Companies
16+
* Hint
17+
* Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
18+
*
19+
*
20+
*
21+
* Example 1:
22+
*
23+
* Input: nums = [1,2,3,4,5,6,7], k = 3
24+
* Output: [5,6,7,1,2,3,4]
25+
* Explanation:
26+
* rotate 1 steps to the right: [7,1,2,3,4,5,6]
27+
* rotate 2 steps to the right: [6,7,1,2,3,4,5]
28+
* rotate 3 steps to the right: [5,6,7,1,2,3,4]
29+
* Example 2:
30+
*
31+
* Input: nums = [-1,-100,3,99], k = 2
32+
* Output: [3,99,-1,-100]
33+
* Explanation:
34+
* rotate 1 steps to the right: [99,-1,-100,3]
35+
* rotate 2 steps to the right: [3,99,-1,-100]
36+
*
37+
*
38+
* Constraints:
39+
*
40+
* 1 <= nums.length <= 105
41+
* -231 <= nums[i] <= 231 - 1
42+
* 0 <= k <= 105
43+
*
44+
*
45+
* Follow up:
46+
*
47+
* Try to come up with as many solutions as you can. There are at least three different ways to solve this problem.
48+
* Could you do it in-place with O(1) extra space?
49+
*
50+
*
51+
*/
52+
public class RotateArray {
53+
54+
// V0
55+
// IDEA: DEQEUE
56+
public void rotate(int[] nums, int k) {
57+
// edge
58+
if (nums == null || nums.length == 0) {
59+
return;
60+
}
61+
62+
Deque<Integer> dq = new LinkedList<>();
63+
for (int n : nums) {
64+
dq.add(n);
65+
}
66+
67+
int i = 0;
68+
for (i = 0; i < k; i++) {
69+
/**
70+
* NOTE !!! below
71+
*
72+
* we get dequeue last element,
73+
* then append it to first idx
74+
*/
75+
dq.addFirst(dq.pollLast());
76+
}
77+
78+
// System.out.println(">>> dq = " + dq);
79+
80+
int j = 0;
81+
while (!dq.isEmpty()) {
82+
nums[j] = dq.pollFirst();
83+
j += 1;
84+
}
85+
}
86+
87+
// V0-1
88+
// IDEA: ARRAY OP (TLE)
89+
public void rotate_0_1(int[] nums, int k) {
90+
// edge
91+
if (nums == null || nums.length == 0) {
92+
return;
93+
}
94+
95+
List<Integer> list = new ArrayList<>();
96+
for (int n : nums) {
97+
list.add(n);
98+
}
99+
100+
int i = 0;
101+
for (i = 0; i < k; i++) {
102+
// int last = nums[nums.length - 1];
103+
int last = list.get(list.size() - 1);
104+
list.remove(list.size() - 1);
105+
list.add(0, last);
106+
}
107+
108+
for (int j = 0; j < list.size(); j++) {
109+
nums[j] = list.get(j);
110+
}
111+
}
112+
113+
// V1
114+
// IDEA: rotate ( `%` op) (gpt)
115+
public void rotate_1(int[] nums, int k) {
116+
// Edge case
117+
if (nums == null || nums.length == 0) {
118+
return;
119+
}
120+
121+
// Adjust k in case it's greater than the array length
122+
k = k % nums.length;
123+
if (k == 0) {
124+
return;
125+
}
126+
127+
// Reverse the entire array
128+
reverse(nums, 0, nums.length - 1);
129+
130+
// Reverse the first k elements
131+
reverse(nums, 0, k - 1);
132+
133+
// Reverse the remaining elements
134+
reverse(nums, k, nums.length - 1);
135+
}
136+
137+
private void reverse(int[] nums, int start, int end) {
138+
while (start < end) {
139+
int temp = nums[start];
140+
nums[start] = nums[end];
141+
nums[end] = temp;
142+
start++;
143+
end--;
144+
}
145+
}
146+
147+
// V2
148+
149+
}

leetcode_java/src/main/java/dev/workspace8.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9497,4 +9497,39 @@ public void merge(int[] nums1, int m, int[] nums2, int n) {
94979497

94989498
}
94999499

9500+
// Lc 189
9501+
// 11.05 - 11.15 am
9502+
public void rotate(int[] nums, int k) {
9503+
// edge
9504+
if(nums == null || nums.length == 0){
9505+
return;
9506+
}
9507+
9508+
// ?? can use below ??
9509+
Deque<Integer> dq = new LinkedList<>();
9510+
// List<Integer> list = new ArrayList<>();
9511+
for(int n: nums){
9512+
// list.add(n);
9513+
dq.add(n);
9514+
}
9515+
9516+
int i = 0;
9517+
for(i = 0; i < k; i++){
9518+
// int last = nums[nums.length - 1];
9519+
// int last = list.get(list.size()-1);
9520+
// list.remove(list.size()-1);
9521+
// list.add(0, last);
9522+
9523+
dq.addFirst(dq.pollLast()); // ???
9524+
}
9525+
9526+
System.out.println(">>> dq = " + dq);
9527+
9528+
int j = 0;
9529+
while(!dq.isEmpty()){
9530+
nums[j] = dq.pollFirst();
9531+
j += 1;
9532+
}
9533+
}
9534+
95009535
}

0 commit comments

Comments
 (0)