Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,44 @@
// 2. Сортируем предметы по убыванию удельной ценности.
// 3. Складываем предметы в рюкзак(массив), по суммарный вес не превысит максимально допустимый

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class Backpack {
public static void main(String[] args) {
Item[] items = new Item[]{ // Исходный набор предметов
new Item(),
new Item(),
};
List<Item> items = new ArrayList<>();
items.add(new Item("Phone", 1, 100));
items.add(new Item("Power bank", 2, 50));
items.add(new Item("Macbook", 5, 70));
items.add(new Item("Watermelon", 8, 20));
items.add(new Item("Water", 2, 50));
items.add(new Item("Snacks", 1, 60));
items.add(new Item("Medicine", 1, 200));
System.out.println(items);

int totalWeight = 10; // Максимальная вместимость рюкзака

List<Item> fulledBackpack = fullBackpack(items, totalWeight);
System.out.println(fulledBackpack);
}

public static List<Item> fullBackpack(List<Item> items, int totalWeight) {
ArrayList<Item> sorted = items.stream()
.sorted(Comparator.comparing(Item::getUnit_value).reversed())
.collect(Collectors.toCollection(ArrayList::new));

List<Item> result = new ArrayList<>();
int currentWeight = 0;

for (Item item : sorted) {
if (totalWeight > currentWeight + item.getWeight()) {
currentWeight += item.getWeight();
result.add(item);
}
}

return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
package org.telran.lecture_11_greedy_practice.practice.backpack;

import lombok.Getter;

@Getter
public class Item {
public String name;
public int weight;
public int value;
public int unit_value; // Удельная ценность - цена за единицу веса
private String name;
private int weight;
private int value;
private int unit_value; // Удельная ценность - цена за единицу веса

public Item(String name, int weight, int value) {
this.name = name;
this.weight = weight;
this.value = value;
this.unit_value = value / weight;
}

@Override
public String toString() {
return "\nItem{" +
"name='" + name + '\'' +
", weight=" + weight +
", value=" + value +
", unit_value=" + unit_value +
"}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.telran.lecture_11_greedy_practice.practice.hw;

import java.util.Arrays;

public class App {

public static void main(String[] args) {
Lift liftOne = new Lift(3, 4);
Lift liftTwo = new Lift(2, 8);
Lift liftThree = new Lift(2, 5);

Lift fastestLift = Lift.findFastest(Arrays.asList(liftOne, liftTwo, liftThree).toArray(new Lift[0]), 1);
System.out.println(fastestLift);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,51 @@
// результат: отправляем лифт С


import java.util.NoSuchElementException;

public class Lift {

private static int totalQuantity = 1;

private final int liftNumber;

private final int speed;

private final int currentFloor;

public Lift(int speed, int currentFloor) {
this.speed = speed;
this.currentFloor = currentFloor;
this.liftNumber = totalQuantity++;
}

public int getSpeed() {
return speed;
}

public static Lift findFastest(Lift[] lifts, int targetFlor) {
if (lifts == null || lifts.length == 0) {
throw new NoSuchElementException();
}

Lift result = lifts[0];
for (Lift lift : lifts) {
if (calculateTime(lift, targetFlor) < calculateTime(result, targetFlor)) {
result = lift;
}
}
return result;
}

private static int calculateTime(Lift lift, int targetFloor) {
return Math.abs(lift.currentFloor - targetFloor) * lift.getSpeed();
}

@Override
public String toString() {
return "Lift " + liftNumber + "{" +
"speed=" + speed +
", currentFloor=" + currentFloor +
'}';
}
}
18 changes: 17 additions & 1 deletion src/main/java/org/telran/lecture_12_trees/practice/BST.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public void insert(int key, String value) {
*/
private void insertNode(Node node, Node newNode) {
// TODO-1: Доработайте код, чтобы все ключи были уникальные.
if (newNode.key == node.key) {
node.value = newNode.value;
return;
}
if (newNode.key < node.key) {
if (node.left == null) {
node.left = newNode;
Expand Down Expand Up @@ -153,6 +157,18 @@ private void displayNode(Node node, String prefix, boolean isLeft) {
}

public static void main(String[] args) {

BST bst = new BST();
bst.insert(3, "Roman");
bst.insert(1, "Ivan");
bst.insert(5, "John");
bst.insert(2, "Sam");
bst.insert(22, "One");
bst.insert(11, "Two");
bst.insert(23, "Three");
bst.insert(12, "Four");
bst.insert(10, "Five");
bst.insert(23, "Six");

bst.displayTree();
}
}