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
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>SberJavaHomeWork2</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>


</project>
44 changes: 44 additions & 0 deletions src/main/java/ru/sber/mironov/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ru.sber.mironov;

public class Car {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не переопределил equals/hashcode

private final long carId;
private final String brand;
private final String modelName;
private final int maxVelocity;
private final int power;
private final int ownerId;

public Car(long carId, String brand, String modelName, int maxVelocity, int power, int ownerId) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

разнести по строкам, читается тяжело когда много аргументов у конструктора в одну строку

this.carId = carId;
this.brand = brand;
this.modelName = modelName;
this.maxVelocity = maxVelocity;
this.power = power;
this.ownerId = ownerId;
}


public String getBrand() {
return brand;
}

public String getModelName() {
return modelName;
}

public int getMaxVelocity() {
return maxVelocity;
}

public int getOwnerId() {
return ownerId;
}

public long getCarId() {
return carId;
}

public int getPower() {
return power;
}
}
128 changes: 128 additions & 0 deletions src/main/java/ru/sber/mironov/Garage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package ru.sber.mironov;

import java.util.*;

import static java.util.stream.Collectors.toList;

public interface Garage {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Реализовать интерфейс это значит написать класс реализующий интерфейс, а не сделать дефолтные методы в интерфейсе и константы вместо полей

HashMap<Owner, ArrayList<Car>> mapOwnerToCars = new HashMap<>();
HashMap<Car, Owner> mapCarToOwner = new HashMap<>();
HashMap<String, ArrayList<Car>> mapBrandToCar = new HashMap<>();
HashMap<Long, Car> cars = new HashMap<>();
HashMap<Long, Owner> owners = new HashMap<>();
Comment on lines +8 to +12
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

использовать интерфейс вместо реализации при объявлении полей.


default Collection<Owner> allCarsUniqueOwners() {
return mapOwnerToCars.keySet().stream()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

будет неправильно работать из-за отсутствия equals/hashcode

.filter(o -> mapOwnerToCars.get(o).size() == 1)
.distinct()
.collect(toList());
}

/**
* Complexity should be less than O(n)
*/
default Collection<Car> topThreeCarsByMaxVelocity() {
ArrayList<Car> sorted = new ArrayList<>();
for (Car car : cars.values()) {
sorted.add(car);
sorted.sort(Comparator.comparingInt(Car::getMaxVelocity));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

сортировка минимум O(n log n), а надо меньше чем за O(n)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (sorted.size() > 3) {
sorted.remove(0);
}
Там дальше идет этот код, который не даёт массиву иметь размер больше 3
Значит во врем сортировки мы будем тратить не более константы операций, а именно не более 9 операций. Суммарная сложность никак не O(nlogn).

if (sorted.size() > 3) {
sorted.remove(0);
}
}
return sorted;
}

/**
* Complexity should be O(1)
*/
default Collection<Car> allCarsOfBrand(String brand) {
return mapBrandToCar.get(brand);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

будет неправильно работать из-за отсутствия equals/hashcode

}

/**
* Complexity should be less than O(n)
*/
default Collection<Car> carsWithPowerMoreThan(int power) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это просто полный перебор. Не удовлетворяет условиям задачи

return mapOwnerToCars.values().stream()
.flatMap(Collection::stream)
.filter(car -> car.getPower() >= power)
.distinct()
.collect(toList());

}

/**
* Complexity should be O(1)
*/
default Collection<Car> allCarsOfOwner(Owner owner) {
return mapOwnerToCars.get(owner);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

будет неправильно работать из-за отсутствия equals/hashcode

}

/**
* @return mean value of owner age that has cars with given brand
*/
default int meanOwnersAgeOfCarBrand(String brand) {
return (int) mapBrandToCar.get(brand).stream()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

один и тот же оунер учитывается несколько раз, если у него несколько машин одного бренда

.map(mapCarToOwner::get)
.mapToInt(Owner::getAge)
.average().getAsDouble();
}

/**
* @return mean value of cars for all owners
*/
default int meanCarNumberForEachOwner() {
return (int) mapOwnerToCars.keySet().stream()
.mapToInt(owner -> mapOwnerToCars.get(owner).size())
.average().getAsDouble();
}

/**
* Complexity should be less than O(n)
*
* @return removed car
*/
default Car removeCar(int carId) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

будет неправильно работать из-за отсутствия equals/hashcode

Car deleteCar = cars.get((long) carId);
Owner deleteOwner = owners.get((long) deleteCar.getOwnerId());

mapOwnerToCars.get(deleteOwner).remove(deleteCar);
if (mapOwnerToCars.get(deleteOwner).isEmpty()) {
mapOwnerToCars.remove(deleteOwner);
owners.remove(deleteOwner.getOwnerId());
}

mapBrandToCar.get(deleteCar.getBrand()).remove(deleteCar);
if (mapBrandToCar.get(deleteCar.getBrand()).isEmpty()) {
mapBrandToCar.remove(deleteCar.getBrand());
}

mapCarToOwner.remove(deleteCar);


cars.remove((long)carId);
return deleteCar;
}

/**
* Complexity should be less than O(n)
*/
default void addCar(Car car, Owner owner) {

put(mapOwnerToCars, owner, car);
put(mapBrandToCar, car.getBrand(), car);

mapCarToOwner.put(car, owner);
cars.put(car.getCarId(), car);
owners.put(owner.getOwnerId(), owner);
}

private <K, V> void put(HashMap<K, ArrayList<V>> collectionHashMap, K key, V value) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

см. computeIfAbsent

if (collectionHashMap.containsKey(key)) {
collectionHashMap.get(key).add(value);
} else {
collectionHashMap.put(key, new ArrayList<>(Collections.singletonList(value)));
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/ru/sber/mironov/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.sber.mironov;

public class Main {
public static void main(String[] args) {

}
}
4 changes: 4 additions & 0 deletions src/main/java/ru/sber/mironov/MyGarage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ru.sber.mironov;

public class MyGarage implements Garage{
}
31 changes: 31 additions & 0 deletions src/main/java/ru/sber/mironov/Owner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ru.sber.mironov;

public class Owner {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

нет equals/hashcode

private final long ownerId;
private final String name;
private final String lastName;
private final int age;

public Owner(long ownerId, String name, String lastName, int age) {
this.ownerId = ownerId;
this.name = name;
this.lastName = lastName;
this.age = age;
}

public String getName() {
return name;
}

public String getLastName() {
return lastName;
}

public long getOwnerId() {
return ownerId;
}

public int getAge() {
return age;
}
}
Loading