Skip to content

Commit 78bef16

Browse files
committed
Added 4 solutions
1 parent a52c0d0 commit 78bef16

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

Easy/Heaters.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int findRadius(int[] houses, int[] heaters) {
3+
Arrays.sort(heaters);
4+
int max = Integer.MIN_VALUE;
5+
6+
for (int house : houses) {
7+
int ind = Arrays.binarySearch(heaters, house);
8+
9+
if (ind < 0) {
10+
ind = - (ind + 1);
11+
}
12+
int d1 = ind - 1 >= 0 ? house - heaters[ind - 1] : Integer.MAX_VALUE;
13+
int d2 = ind < heaters.length ? heaters[ind] - house : Integer.MAX_VALUE;
14+
15+
max = Math.max(max, Math.min(d1, d2));
16+
}
17+
18+
return max;
19+
}
20+
}

Easy/Pascal's Triangle II.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public List<Integer> getRow(int rowIndex) {
3+
List<Integer> res = new ArrayList<Integer>();
4+
5+
for(int i = 0;i<rowIndex+1;i++) {
6+
res.add(1);
7+
for(int j=i-1;j>0;j--) {
8+
res.set(j, res.get(j-1) + res.get(j));
9+
}
10+
}
11+
12+
return res;
13+
}
14+
}

Easy/Pascal's Triangle.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
3+
public List<List<Integer>> generate(int numRows) {
4+
5+
List<List<Integer>> ans = new ArrayList<>();
6+
7+
for (int i=0;i<numRows;i++) {
8+
List<Integer> temp = new ArrayList<>();
9+
for(int j = 0;j<=i;j++) {
10+
if (i == j || j == 0) {
11+
temp.add(1);
12+
}
13+
else {
14+
temp.add(ans.get(i-1).get(j-1) + ans.get(i-1).get(j));
15+
}
16+
}
17+
18+
ans.add(temp);
19+
}
20+
21+
return ans;
22+
}
23+
}

Easy/String Compression.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int compress(char[] chars) {
3+
4+
int ans = 0;
5+
int ind = 0;
6+
7+
while (ind < chars.length) {
8+
char currChar = chars[ind];
9+
int count = 0;
10+
11+
while(ind < chars.length && chars[ind] == currChar) {
12+
ind++;
13+
count++;
14+
}
15+
16+
chars[ans++] = currChar;
17+
18+
if (count != 1) {
19+
for (char c : Integer.toString(count).toCharArray()) {
20+
chars[ans++] = c;
21+
}
22+
}
23+
}
24+
25+
return ans;
26+
}
27+
}

0 commit comments

Comments
 (0)