Skip to content

Commit cadc923

Browse files
committed
Added 2 solutions & modified 2 solutions
1 parent 69f2038 commit cadc923

File tree

4 files changed

+103
-52
lines changed

4 files changed

+103
-52
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int[] sortByBits(int[] arr) {
3+
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>(){
4+
public int compare(Integer o1, Integer o2) {
5+
int c = Integer.bitCount(o1) - Integer.bitCount(o2);
6+
if (c != 0) {
7+
return c;
8+
}
9+
return o1 - o2;
10+
}
11+
});
12+
for (int num : arr) {
13+
pq.add(num);
14+
}
15+
int[] ans = new int[arr.length];
16+
int idx = 0;
17+
while (!pq.isEmpty()) {
18+
ans[idx++] = pq.poll();
19+
}
20+
return ans;
21+
}
22+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Cashier {
2+
int n;
3+
int discount;
4+
int currCount;
5+
Map<Integer, Integer> priceMap;
6+
public Cashier(int n, int discount, int[] products, int[] prices) {
7+
this.n = n;
8+
this.currCount = 0;
9+
this.discount = discount;
10+
priceMap = new HashMap<>();
11+
for (int i = 0; i < products.length; i++) {
12+
priceMap.put(products[i], prices[i]);
13+
}
14+
}
15+
16+
public double getBill(int[] product, int[] amount) {
17+
currCount++;
18+
double totalBill = 0;
19+
for (int i = 0; i < product.length; i++) {
20+
totalBill += amount[i] * priceMap.get(product[i]);
21+
}
22+
if (currCount % n == 0) {
23+
totalBill -= getDiscount(totalBill);
24+
}
25+
return totalBill;
26+
}
27+
28+
private double getDiscount(double totalBill) {
29+
return (totalBill * discount) / 100;
30+
}
31+
}
32+
33+
/**
34+
* Your Cashier object will be instantiated and called as such:
35+
* Cashier obj = new Cashier(n, discount, products, prices);
36+
* double param_1 = obj.getBill(product,amount);
37+
*/

Medium/Remove K Digits.java

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
11
class Solution {
2-
public String removeKdigits(String num, int k) {
3-
Stack<Integer> stack = new Stack<>();
4-
int idx = 0;
5-
int n = num.length();
6-
while (idx < n) {
7-
while (k > 0 && !stack.isEmpty()) {
8-
if (stack.peek() <= Character.getNumericValue(num.charAt(idx))) {
9-
break;
10-
}
11-
stack.pop();
12-
k--;
13-
}
14-
15-
stack.push(Character.getNumericValue(num.charAt(idx++)));
16-
}
17-
18-
while (k-- > 0) {
19-
stack.pop();
20-
}
21-
22-
StringBuilder sb = new StringBuilder();
23-
while (!stack.isEmpty()) {
24-
sb.append(stack.pop());
25-
}
26-
27-
String ans = sb.reverse().toString();
28-
idx = -1;
29-
while (idx + 1 < ans.length() && ans.charAt(idx + 1) == '0') {
30-
idx++;
31-
}
32-
33-
return ans.substring(idx + 1).length() == 0 ? "0" : ans.substring(idx + 1);
2+
public String removeKdigits(String num, int k) {
3+
LinkedList<Character> stack = new LinkedList<>();
4+
for (char c : num.toCharArray()) {
5+
while (!stack.isEmpty() && k > 0 && stack.peekLast() > c) {
6+
stack.removeLast();
7+
k--;
8+
}
9+
stack.addLast(c);
3410
}
11+
while (k-- > 0) {
12+
stack.removeLast();
13+
}
14+
StringBuilder sb = new StringBuilder();
15+
boolean currZero = true;
16+
for (char c : stack) {
17+
if (currZero && c == '0') {
18+
continue;
19+
}
20+
currZero = false;
21+
sb.append(c);
22+
}
23+
return sb.length() == 0 ? "0" : sb.toString();
24+
}
3525
}

Medium/Sort Colors.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
class Solution {
2-
public void sortColors(int[] nums) {
3-
int idx1 = 0;
4-
int idx2 = nums.length - 1;
5-
int idx = 0;
6-
7-
while (idx <= idx2) {
8-
if (nums[idx] == 0) {
9-
nums[idx] = nums[idx1];
10-
nums[idx1] = 0;
11-
idx1++;
12-
}
13-
if (nums[idx] == 2) {
14-
nums[idx] = nums[idx2];
15-
nums[idx2] = 2;
16-
idx2--;
17-
idx--;
18-
}
19-
20-
idx++;
21-
}
2+
public void sortColors(int[] nums) {
3+
int zeroIdx = 0;
4+
int curr = 0;
5+
int n = nums.length - 1;
6+
int twoIdx = n;
7+
while (curr <= twoIdx) {
8+
if (nums[curr] == 0) {
9+
swap(nums, curr++, zeroIdx++);
10+
}
11+
else if (nums[curr] == 2) {
12+
swap(nums, curr, twoIdx--);
13+
}
14+
else {
15+
curr++;
16+
}
2217
}
18+
}
19+
20+
private void swap(int[] nums, int idx1, int idx2) {
21+
int temp = nums[idx1];
22+
nums[idx1] = nums[idx2];
23+
nums[idx2] = temp;
24+
}
2325
}

0 commit comments

Comments
 (0)