Skip to content

Commit af02f4e

Browse files
committed
algorithms lesson #8 added
1 parent bdc7c3a commit af02f4e

File tree

3 files changed

+107
-35
lines changed

3 files changed

+107
-35
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package lesson8;
2+
3+
public class Item implements Comparable<Item>{
4+
private final String name;
5+
private final int weight;
6+
private final float cost;
7+
8+
public Item(String name, int weight, float cost) {
9+
this.name = name;
10+
this.weight = weight;
11+
this.cost = cost;
12+
}
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public int getWeight() {
19+
return weight;
20+
}
21+
22+
public float getCost() {
23+
return cost;
24+
}
25+
26+
@Override
27+
public int compareTo(Item o) {
28+
return Float.compare(o.cost/o.weight, cost/weight);
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return "Item{" +
34+
"name='" + name + '\'' +
35+
", weight=" + weight +
36+
", cost=" + cost +
37+
'}';
38+
}
39+
}
Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package lesson8;
22

3-
import java.util.ArrayList;
43
import java.util.Arrays;
5-
import java.util.Collections;
6-
import java.util.LinkedList;
74
import java.util.List;
85

96
/**
@@ -18,43 +15,18 @@ public static void main(String[] args) {
1815
// 1 задача - выбор задач
1916
List<Integer> tasks = Arrays.asList(3, 2, 1, 4, 5, 3);
2017
int maxTime = 10;
21-
List<Integer> resolved = taskSelection(tasks, maxTime);
18+
List<Integer> resolved = Lesson8Methods.taskSelection(tasks, maxTime);
2219
System.out.println(resolved);
2320

2421
// 2 задача - размен монет
2522
List<Integer> coins = Arrays.asList(1, 5, 10, 25);
2623
int amount = 63;
27-
System.out.println(coinChange(coins, amount));
28-
}
29-
30-
static List<Integer> taskSelection(List<Integer> tasks, int maxTime) {
31-
List<Integer> result = new ArrayList<>();
32-
int resultTime = 0;
33-
34-
LinkedList<Integer> sortedTasks = new LinkedList<>(tasks);
35-
Collections.sort(sortedTasks);
36-
while (resultTime < maxTime && maxTime - resultTime >= sortedTasks.peek()) {
37-
int taskTime = sortedTasks.pop();
38-
result.add(taskTime);
39-
resultTime = resultTime + taskTime;
40-
}
41-
return result;
42-
}
43-
44-
static List<Integer> coinChange(List<Integer> coins, int amount) {
45-
List<Integer> result = new ArrayList<>();
46-
47-
Collections.sort(coins);
48-
for (int i = coins.size() - 1; i >= 0; i--) {
49-
while (amount >= coins.get(i)) {
50-
amount = amount - coins.get(i);
51-
result.add(coins.get(i));
52-
}
53-
}
24+
System.out.println(Lesson8Methods.coinChange(coins, amount));
5425

55-
if (amount == 0) {
56-
return result;
57-
}
58-
return Collections.EMPTY_LIST;
26+
// 3 задача - упаковка рюкзака
27+
List<Item> items = Arrays.asList(new Item("A", 7, 100), new Item("B", 5, 80), new Item("C", 3, 50));
28+
int capacity = 10;
29+
List<Item> result = Lesson8Methods.fractionalKnapsack(items, capacity);
30+
System.out.println(result);
5931
}
6032
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package lesson8;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
8+
public class Lesson8Methods {
9+
10+
// 1я задача: выбор задач
11+
static List<Integer> taskSelection(List<Integer> tasks, int maxTime) {
12+
List<Integer> result = new ArrayList<>();
13+
int resultTime = 0;
14+
15+
LinkedList<Integer> sortedTasks = new LinkedList<>(tasks);
16+
Collections.sort(sortedTasks);
17+
while (resultTime < maxTime && maxTime - resultTime >= sortedTasks.peek()) {
18+
int taskTime = sortedTasks.pop();
19+
result.add(taskTime);
20+
resultTime = resultTime + taskTime;
21+
}
22+
return result;
23+
}
24+
25+
// 2я задача: размен монет
26+
static List<Integer> coinChange(List<Integer> coins, int amount) {
27+
List<Integer> result = new ArrayList<>();
28+
29+
Collections.sort(coins);
30+
for (int i = coins.size() - 1; i >= 0; i--) {
31+
while (amount >= coins.get(i)) {
32+
amount = amount - coins.get(i);
33+
result.add(coins.get(i));
34+
}
35+
}
36+
37+
if (amount == 0) {
38+
return result;
39+
}
40+
return new ArrayList<>();
41+
}
42+
43+
// 3я задача: упаковка рюкзака
44+
static List<Item> fractionalKnapsack(List<Item> items, int capacity) {
45+
List<Item> result = new ArrayList<>();
46+
Collections.sort(items); // сортировка по убыванию вес/стоимость
47+
48+
int currentWeight = 0;
49+
50+
for (Item item : items) {
51+
if (currentWeight + item.getWeight() <= capacity) {
52+
currentWeight = currentWeight + item.getWeight();
53+
result.add(item);
54+
} else {
55+
break;
56+
}
57+
}
58+
59+
return result;
60+
}
61+
}

0 commit comments

Comments
 (0)