๐๐ฆ ๐๐น๐ด๐ผ ๐ป ๐ ใ๐๐ผ๐บ๐ฝ๐ฒ๐๐ถ๐๐ถ๐๐ฒ ๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ดใ
Photo
public static int solution(int N, int K, int[] cost, int[] sell) {
int maxProfit = 0;
List<Item> items = new ArrayList<>();
for (int i = 0; i < N; i++) {
items.add(new Item(cost[i], sell[i]));
}
Collections.sort(items, Comparator.comparingInt(Item::getSellPrice));
int low = 0, high = items.size() - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
int buyingPrice = items.get(mid).getBuyPrice();
if (K >= buyingPrice) {
maxProfit = Math.max(maxProfit, items.get(mid).getSellPrice() - buyingPrice);
low = mid + 1;
} else {
high = mid - 1;
}
}
return maxProfit;
}
}
class Item {
private int buyPrice;
private int sellPrice;
public Item(int buyPrice, int sellPrice) {
this.buyPrice = buyPrice;
this.sellPrice = sellPrice;
}
public int getBuyPrice() {
return buyPrice;
}
public int getSellPrice() {
return sellPrice;
}
}
Profits โ
int maxProfit = 0;
List<Item> items = new ArrayList<>();
for (int i = 0; i < N; i++) {
items.add(new Item(cost[i], sell[i]));
}
Collections.sort(items, Comparator.comparingInt(Item::getSellPrice));
int low = 0, high = items.size() - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
int buyingPrice = items.get(mid).getBuyPrice();
if (K >= buyingPrice) {
maxProfit = Math.max(maxProfit, items.get(mid).getSellPrice() - buyingPrice);
low = mid + 1;
} else {
high = mid - 1;
}
}
return maxProfit;
}
}
class Item {
private int buyPrice;
private int sellPrice;
public Item(int buyPrice, int sellPrice) {
this.buyPrice = buyPrice;
this.sellPrice = sellPrice;
}
public int getBuyPrice() {
return buyPrice;
}
public int getSellPrice() {
return sellPrice;
}
}
Profits โ
๐1
Critical Reasoning
Accenture 2pm slot โ
Accenture 2pm slot โ