Skip to content

Commit 1ef5194

Browse files
committed
Added 1 solution & modified 2 solutions
1 parent 0eedd07 commit 1ef5194

File tree

3 files changed

+57
-43
lines changed

3 files changed

+57
-43
lines changed

Easy/Flipping an Image.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
class Solution {
2-
public int[][] flipAndInvertImage(int[][] A) {
3-
int[][] ans = new int[A.length][A[0].length];
4-
5-
for (int i=0; i<A.length; i++) {
6-
List<Integer> temp = Arrays.stream(A[i]).
7-
boxed().
8-
collect(Collectors.toList());
9-
10-
Collections.reverse(temp);
11-
ans[i] = temp.stream().mapToInt(e -> e).toArray();
12-
}
13-
14-
for (int i=0;i<ans.length;i++) {
15-
for (int j=0;j<ans[i].length;j++) {
16-
ans[i][j] = ans[i][j] == 1 ? 0 : 1;
17-
}
18-
}
19-
20-
return ans;
2+
public int[][] flipAndInvertImage(int[][] A) {
3+
for (int[] arr : A) {
4+
reverse(arr);
5+
invert(arr);
216
}
7+
return A;
8+
}
9+
10+
private void reverse(int[] arr) {
11+
int start = 0;
12+
int end = arr.length - 1;
13+
while (start < end) {
14+
int temp = arr[start];
15+
arr[start] = arr[end];
16+
arr[end] = temp;
17+
start++;
18+
end--;
19+
}
20+
}
21+
22+
private void invert(int[] arr) {
23+
for (int i = 0; i < arr.length; i++) {
24+
arr[i] = arr[i] ^ 1;
25+
}
26+
}
2227
}

Easy/Last Stone Weight.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
class Solution {
2-
public int lastStoneWeight(int[] stones) {
3-
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>() {
4-
@Override
5-
public int compare(Integer o1, Integer o2) {
6-
return o2.compareTo(o1);
7-
}
8-
});
9-
10-
for (int stone : stones) {
11-
pq.add(stone);
12-
}
13-
14-
while (pq.size() > 1) {
15-
int stone1 = pq.poll();
16-
int stone2 = pq.poll();
17-
18-
int resSize = Math.max(stone1, stone2) - Math.min(stone1, stone2);
19-
20-
if (resSize > 0) {
21-
pq.add(resSize);
22-
}
23-
}
24-
25-
return pq.size() == 1 ? pq.poll() : 0;
2+
public int lastStoneWeight(int[] stones) {
3+
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>(){
4+
public int compare(Integer o1, Integer o2) {
5+
return o2 - o1;
6+
}
7+
});
8+
for (int stone : stones) {
9+
pq.add(stone);
2610
}
11+
while(pq.size() > 1) {
12+
int y = pq.poll();
13+
int x = pq.poll();
14+
if (x != y) {
15+
pq.add(y - x);
16+
}
17+
}
18+
return pq.isEmpty() ? 0 : pq.poll();
19+
}
2720
}

Easy/Sort Array By Parity II.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int[] sortArrayByParityII(int[] A) {
3+
int start = 1;
4+
for (int i = 0; i < A.length; i += 2) {
5+
if (A[i] % 2 == 1) {
6+
while (A[start] % 2 != 0) {
7+
start += 2;
8+
}
9+
int temp = A[i];
10+
A[i] = A[start];
11+
A[start] = temp;
12+
}
13+
}
14+
return A;
15+
}
16+
}

0 commit comments

Comments
 (0)