Skip to content

Commit d333b34

Browse files
committed
Added 4 solutions
1 parent 9ebe6ce commit d333b34

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-0
lines changed

Easy/Fair Candy Swap.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public int[] fairCandySwap(int[] A, int[] B) {
3+
int sumA = 0;
4+
int sumB = 0;
5+
6+
Set<Integer> set = new HashSet<>();
7+
8+
for (int num : A) {
9+
sumA += num;
10+
set.add(num);
11+
}
12+
13+
for (int num : B) {
14+
sumB += num;
15+
}
16+
17+
int diff = (sumA - sumB)/2;
18+
19+
int[] ans = new int[2];
20+
for (int num : B) {
21+
if (set.contains(num + diff)) {
22+
ans[0] = num + diff;
23+
ans[1] = num;
24+
break;
25+
}
26+
}
27+
28+
return ans;
29+
}
30+
}

Easy/Positions of Large Groups.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public List<List<Integer>> largeGroupPositions(String S) {
3+
List<List<Integer>> positions = new ArrayList<>();
4+
5+
int i = 1;
6+
int count = 1;
7+
int start = 0;
8+
9+
while (i < S.length()) {
10+
if (S.charAt(i) == S.charAt(i-1)) {
11+
count++;
12+
}
13+
else {
14+
if (count >= 3) {
15+
List<Integer> temp = Arrays.asList(start, i-1);
16+
positions.add(temp);
17+
}
18+
19+
start = i;
20+
count = 1;
21+
}
22+
23+
i++;
24+
}
25+
26+
if (count >= 3) {
27+
positions.add(Arrays.asList(start, i-1));
28+
}
29+
30+
return positions;
31+
}
32+
}

Easy/Transpose Matrix.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[][] transpose(int[][] A) {
3+
int rows = A.length;
4+
int columns = A[0].length;
5+
6+
int[][] newArr = new int[columns][rows];
7+
8+
for (int i=0; i<columns; i++) {
9+
for (int j=0; j<rows; j++) {
10+
newArr[i][j] = A[j][i];
11+
}
12+
}
13+
14+
return newArr;
15+
}
16+
}

Medium/Design Circular Queue.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
class MyCircularQueue {
2+
3+
/** Initialize your data structure here. Set the size of the queue to be k. */
4+
Node head;
5+
Node curr;
6+
int capacity;
7+
int count = 0;
8+
9+
public MyCircularQueue(int k) {
10+
head = new Node(-1);
11+
curr = head;
12+
capacity = k;
13+
}
14+
15+
/** Insert an element into the circular queue. Return true if the operation is successful. */
16+
public boolean enQueue(int value) {
17+
if (count == capacity) {
18+
return false;
19+
}
20+
21+
curr.next = new Node(value);
22+
curr = curr.next;
23+
count++;
24+
25+
return true;
26+
}
27+
28+
/** Delete an element from the circular queue. Return true if the operation is successful. */
29+
public boolean deQueue() {
30+
if (count <= 0) {
31+
return false;
32+
}
33+
34+
if (head.next == curr) {
35+
curr = head;
36+
head.next = null;
37+
}
38+
else {
39+
head.next = head.next.next;
40+
}
41+
42+
count--;
43+
44+
return true;
45+
}
46+
47+
/** Get the front item from the queue. */
48+
public int Front() {
49+
if (count == 0) {
50+
return -1;
51+
}
52+
53+
return head.next.value;
54+
}
55+
56+
/** Get the last item from the queue. */
57+
public int Rear() {
58+
if (count == 0) {
59+
return -1;
60+
}
61+
62+
return curr.value;
63+
}
64+
65+
/** Checks whether the circular queue is empty or not. */
66+
public boolean isEmpty() {
67+
return count == 0;
68+
}
69+
70+
/** Checks whether the circular queue is full or not. */
71+
public boolean isFull() {
72+
return count == capacity;
73+
}
74+
75+
private void printList(Node node) {
76+
while (node != null) {
77+
System.out.print(node.value + " ");
78+
}
79+
80+
System.out.println();
81+
}
82+
83+
class Node {
84+
int value;
85+
Node next;
86+
Node(int value) {
87+
this.value = value;
88+
}
89+
}
90+
}
91+
92+
/**
93+
* Your MyCircularQueue object will be instantiated and called as such:
94+
* MyCircularQueue obj = new MyCircularQueue(k);
95+
* boolean param_1 = obj.enQueue(value);
96+
* boolean param_2 = obj.deQueue();
97+
* int param_3 = obj.Front();
98+
* int param_4 = obj.Rear();
99+
* boolean param_5 = obj.isEmpty();
100+
* boolean param_6 = obj.isFull();
101+
*/

0 commit comments

Comments
 (0)