Skip to content

Commit 319cab9

Browse files
committed
Added 3 solutions
1 parent 6ec05b8 commit 319cab9

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int movesToMakeZigzag(int[] nums) {
3+
return Math.min(calculateMove(nums, true), calculateMove(nums, false));
4+
}
5+
6+
private int calculateMove(int[] nums, boolean isEven) {
7+
int count = 0;
8+
9+
for (int i = (isEven ? 0 : 1); i < nums.length; i += 2) {
10+
int curr = 0;
11+
if (i + 1 < nums.length) {
12+
if (nums[i] >= nums[i + 1]) {
13+
curr = nums[i] - nums[i + 1] + 1;
14+
}
15+
}
16+
17+
if (i - 1 >= 0) {
18+
if (nums[i] >= nums[i - 1]) {
19+
curr = Math.max(curr, nums[i] - nums[i - 1] + 1);
20+
}
21+
}
22+
23+
count += curr;
24+
}
25+
26+
return count;
27+
}
28+
}

Medium/Snapshot Array.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class SnapshotArray {
2+
3+
int[] arr;
4+
Map<Integer, Map<Integer, Integer>> map;
5+
int count;
6+
Map<Integer, Integer> temp;
7+
public SnapshotArray(int length) {
8+
arr = new int[length];
9+
map = new HashMap<>();
10+
count = 0;
11+
temp = new HashMap<>();
12+
}
13+
14+
public void set(int index, int val) {
15+
arr[index] = val;
16+
temp.put(index, val);
17+
}
18+
19+
public int snap() {
20+
map.put(count, new HashMap<>(temp));
21+
int snapCount = count++;
22+
temp = new HashMap<>();
23+
return snapCount;
24+
}
25+
26+
public int get(int index, int snap_id) {
27+
while (snap_id >= 0) {
28+
if (map.get(snap_id).containsKey(index)) {
29+
return map.get(snap_id).get(index);
30+
}
31+
32+
snap_id--;
33+
}
34+
35+
return 0;
36+
}
37+
}
38+
39+
/**
40+
* Your SnapshotArray object will be instantiated and called as such:
41+
* SnapshotArray obj = new SnapshotArray(length);
42+
* obj.set(index,val);
43+
* int param_2 = obj.snap();
44+
* int param_3 = obj.get(index,snap_id);
45+
*/

Medium/Total Hamming Distance.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int totalHammingDistance(int[] nums) {
3+
int count = 0;
4+
for (int i = 0; i < 32; i++) {
5+
int bitCount = 0;
6+
for (int j = 0; j < nums.length; j++) {
7+
bitCount += (nums[j] >> i) & 1;
8+
}
9+
10+
count += bitCount * (nums.length - bitCount);
11+
}
12+
13+
return count;
14+
}
15+
}

0 commit comments

Comments
 (0)