Skip to content

Commit 68ecac5

Browse files
committed
Added 1 solution & modified 1 solution
1 parent 18af62e commit 68ecac5

File tree

2 files changed

+68
-38
lines changed

2 files changed

+68
-38
lines changed

Easy/Degree of an array.java

+20-38
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,24 @@
11
class Solution {
2-
public int findShortestSubArray(int[] nums) {
3-
Map<Integer, Entry> map = new HashMap<>();
4-
5-
for (int i = 0; i < nums.length; i++) {
6-
if (map.containsKey(nums[i])) {
7-
map.get(nums[i]).degree++;
8-
map.get(nums[i]).endIdx = i;
9-
}
10-
else {
11-
map.put(nums[i], new Entry(1, i, i));
12-
}
13-
}
14-
15-
int res = Integer.MAX_VALUE;
16-
int degree = Integer.MIN_VALUE;
17-
18-
for (Entry entry : map.values()) {
19-
if (degree < entry.degree) {
20-
degree = entry.degree;
21-
res = entry.endIdx - entry.startIdx + 1;
22-
}
23-
else if (degree == entry.degree) {
24-
res = Math.min(entry.endIdx - entry.startIdx + 1, res);
25-
}
26-
}
27-
28-
return res;
2+
public int findShortestSubArray(int[] nums) {
3+
Map<Integer, Integer> count = new HashMap<>();
4+
Map<Integer, Integer> left = new HashMap<>();
5+
Map<Integer, Integer> right = new HashMap<>();
6+
int degree = 0;
7+
for (int i = 0; i < nums.length; i++) {
8+
int num = nums[i];
9+
if (!left.containsKey(num)) {
10+
left.put(num, i);
11+
}
12+
right.put(num, i);
13+
count.put(num, count.getOrDefault(num, 0) + 1);
14+
degree = Math.max(degree, count.get(num));
2915
}
30-
31-
class Entry {
32-
int degree;
33-
int startIdx;
34-
int endIdx;
35-
36-
public Entry(int degree, int startIdx, int endIdx) {
37-
this.degree = degree;
38-
this.startIdx = startIdx;
39-
this.endIdx = endIdx;
40-
}
16+
int ans = nums.length;
17+
for (int key : count.keySet()) {
18+
if (count.get(key) == degree) {
19+
ans = Math.min(ans, right.get(key) - left.get(key) + 1);
20+
}
4121
}
22+
return ans;
23+
}
4224
}

Easy/Perform String Shifts.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution {
2+
public String stringShift(String s, int[][] shifts) {
3+
int leftShiftAmount = 0;
4+
int rightShiftAmount = 0;
5+
for (int[] shift : shifts) {
6+
if (shift[0] == 0) {
7+
leftShiftAmount += shift[1];
8+
}
9+
else {
10+
rightShiftAmount += shift[1];
11+
}
12+
}
13+
if (leftShiftAmount > rightShiftAmount) {
14+
return shiftLeft(s, leftShiftAmount - rightShiftAmount);
15+
}
16+
else if (rightShiftAmount > leftShiftAmount) {
17+
return shiftRight(s, rightShiftAmount - leftShiftAmount);
18+
}
19+
else {
20+
return s;
21+
}
22+
}
23+
24+
private String shiftLeft(String s, int n) {
25+
StringBuilder sb = new StringBuilder();
26+
n %= s.length();
27+
for (int i = n; i < s.length(); i++) {
28+
sb.append(s.charAt(i));
29+
}
30+
for (int i = 0; i < n; i++) {
31+
sb.append(s.charAt(i));
32+
}
33+
return sb.toString();
34+
}
35+
36+
private String shiftRight(String s, int n) {
37+
StringBuilder sb = new StringBuilder();
38+
n %= s.length();
39+
int len = s.length();
40+
for (int i = 0; i < len - n; i++) {
41+
sb.append(s.charAt(i));
42+
}
43+
for (int i = s.length() - 1; i >= len - n; i--) {
44+
sb.insert(0, s.charAt(i));
45+
}
46+
return sb.toString();
47+
}
48+
}

0 commit comments

Comments
 (0)