| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | namespace LeetCode.Algorithms.DesignMovieRentalSystem; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class DesignMovieRentalSystemDictionary : IDesignMovieRentalSystem |
| | | 16 | | { |
| | | 17 | | private const int ResultsCount = 5; |
| | | 18 | | private readonly Dictionary<int, SortedSet<MovieOffer>> _movieToMovieOffersDictionary; |
| | 1 | 19 | | private readonly SortedSet<RentalRecord> _rentalRecordsSortedSet = []; |
| | | 20 | | private readonly Dictionary<(int Shop, int Movie), int> _shopMovieToPriceDictionary; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Time complexity - O(m log m), where m is the length of entries |
| | | 24 | | /// Space complexity - O(m) |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="n"></param> |
| | | 27 | | /// <param name="entries"></param> |
| | 1 | 28 | | public DesignMovieRentalSystemDictionary(int n, int[][] entries) |
| | 1 | 29 | | { |
| | 1 | 30 | | var entriesLength = entries.Length; |
| | | 31 | | |
| | 1 | 32 | | _movieToMovieOffersDictionary = new Dictionary<int, SortedSet<MovieOffer>>(entriesLength); |
| | 1 | 33 | | _shopMovieToPriceDictionary = new Dictionary<(int Shop, int Movie), int>(entriesLength); |
| | | 34 | | |
| | 14 | 35 | | for (var i = 0; i < entriesLength; i++) |
| | 6 | 36 | | { |
| | 6 | 37 | | var entry = entries[i]; |
| | 6 | 38 | | var shop = entry[0]; |
| | 6 | 39 | | var movie = entry[1]; |
| | 6 | 40 | | var price = entry[2]; |
| | | 41 | | |
| | 6 | 42 | | _shopMovieToPriceDictionary.Add((shop, movie), price); |
| | | 43 | | |
| | 6 | 44 | | if (!_movieToMovieOffersDictionary.TryGetValue(movie, out var movieOffers)) |
| | 3 | 45 | | { |
| | 3 | 46 | | movieOffers = []; |
| | | 47 | | |
| | 3 | 48 | | _movieToMovieOffersDictionary[movie] = movieOffers; |
| | 3 | 49 | | } |
| | | 50 | | |
| | 6 | 51 | | movieOffers.Add(new MovieOffer(price, shop)); |
| | 6 | 52 | | } |
| | 1 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Time complexity - O(1) |
| | | 57 | | /// Space complexity - O(1) |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="movie"></param> |
| | | 60 | | /// <returns></returns> |
| | | 61 | | public IList<int> Search(int movie) |
| | 2 | 62 | | { |
| | 2 | 63 | | if (!_movieToMovieOffersDictionary.TryGetValue(movie, out var movieOffers)) |
| | 0 | 64 | | { |
| | 0 | 65 | | return []; |
| | | 66 | | } |
| | | 67 | | |
| | 2 | 68 | | var movieOffersCount = movieOffers.Count; |
| | | 69 | | |
| | 2 | 70 | | if (movieOffersCount == 0) |
| | 0 | 71 | | { |
| | 0 | 72 | | return []; |
| | | 73 | | } |
| | | 74 | | |
| | 2 | 75 | | var searchResultsCount = Math.Min(movieOffersCount, ResultsCount); |
| | | 76 | | |
| | 2 | 77 | | var searchResults = new int[searchResultsCount]; |
| | | 78 | | |
| | 2 | 79 | | var searchResultsIndex = 0; |
| | | 80 | | |
| | 14 | 81 | | foreach (var movieOffer in movieOffers) |
| | 5 | 82 | | { |
| | 5 | 83 | | searchResults[searchResultsIndex] = movieOffer.Shop; |
| | | 84 | | |
| | 5 | 85 | | searchResultsIndex++; |
| | | 86 | | |
| | 5 | 87 | | if (searchResultsIndex == searchResultsCount) |
| | 2 | 88 | | { |
| | 2 | 89 | | break; |
| | | 90 | | } |
| | 3 | 91 | | } |
| | | 92 | | |
| | 2 | 93 | | return searchResults; |
| | 2 | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Time complexity - O(1) |
| | | 98 | | /// Space complexity - O(1) |
| | | 99 | | /// </summary> |
| | | 100 | | /// <returns></returns> |
| | | 101 | | public IList<IList<int>> Report() |
| | 1 | 102 | | { |
| | 1 | 103 | | var rentalItemsCount = _rentalRecordsSortedSet.Count; |
| | | 104 | | |
| | 1 | 105 | | if (rentalItemsCount == 0) |
| | 0 | 106 | | { |
| | 0 | 107 | | return []; |
| | | 108 | | } |
| | | 109 | | |
| | 1 | 110 | | var reportResultsCount = Math.Min(rentalItemsCount, ResultsCount); |
| | | 111 | | |
| | 1 | 112 | | var reportResults = new IList<int>[reportResultsCount]; |
| | | 113 | | |
| | 1 | 114 | | var reportResultsIndex = 0; |
| | | 115 | | |
| | 6 | 116 | | foreach (var rentalItem in _rentalRecordsSortedSet) |
| | 2 | 117 | | { |
| | 2 | 118 | | reportResults[reportResultsIndex] = rentalItem.GetReport(); |
| | | 119 | | |
| | 2 | 120 | | reportResultsIndex++; |
| | | 121 | | |
| | 2 | 122 | | if (reportResultsIndex == reportResultsCount) |
| | 1 | 123 | | { |
| | 1 | 124 | | break; |
| | | 125 | | } |
| | 1 | 126 | | } |
| | | 127 | | |
| | 1 | 128 | | return reportResults; |
| | 1 | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Time complexity - O(log m), where m is the number of offers for the movie |
| | | 133 | | /// Space complexity - O(1) |
| | | 134 | | /// </summary> |
| | | 135 | | /// <param name="shop"></param> |
| | | 136 | | /// <param name="movie"></param> |
| | | 137 | | public void Rent(int shop, int movie) |
| | 2 | 138 | | { |
| | 2 | 139 | | var price = _shopMovieToPriceDictionary[(shop, movie)]; |
| | | 140 | | |
| | 2 | 141 | | _rentalRecordsSortedSet.Add(new RentalRecord(shop, movie, price)); |
| | | 142 | | |
| | 2 | 143 | | var movieOffers = _movieToMovieOffersDictionary[movie]; |
| | | 144 | | |
| | 2 | 145 | | movieOffers.Remove(new MovieOffer(price, shop)); |
| | 2 | 146 | | } |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// Time complexity - O(log m), where m is the number of offers for the movie |
| | | 150 | | /// Space complexity - O(1) |
| | | 151 | | /// </summary> |
| | | 152 | | /// <param name="shop"></param> |
| | | 153 | | /// <param name="movie"></param> |
| | | 154 | | public void Drop(int shop, int movie) |
| | 1 | 155 | | { |
| | 1 | 156 | | var price = _shopMovieToPriceDictionary[(shop, movie)]; |
| | | 157 | | |
| | 1 | 158 | | _rentalRecordsSortedSet.Remove(new RentalRecord(shop, movie, price)); |
| | | 159 | | |
| | 1 | 160 | | var movieOffers = _movieToMovieOffersDictionary[movie]; |
| | | 161 | | |
| | 1 | 162 | | movieOffers.Add(new MovieOffer(price, shop)); |
| | 1 | 163 | | } |
| | | 164 | | |
| | 25 | 165 | | public readonly record struct MovieOffer(int Price, int Shop) : IComparable<MovieOffer> |
| | | 166 | | { |
| | | 167 | | /// <summary> |
| | | 168 | | /// Time complexity - O(1) |
| | | 169 | | /// Space complexity - O(1) |
| | | 170 | | /// </summary> |
| | | 171 | | /// <param name="movieOffer"></param> |
| | | 172 | | /// <returns></returns> |
| | | 173 | | public int CompareTo(MovieOffer movieOffer) |
| | 7 | 174 | | { |
| | 7 | 175 | | var priceComparison = CompareToPrice(movieOffer.Price); |
| | | 176 | | |
| | 7 | 177 | | return priceComparison != 0 ? priceComparison : CompareToShop(movieOffer.Shop); |
| | 7 | 178 | | } |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Time complexity - O(1) |
| | | 182 | | /// Space complexity - O(1) |
| | | 183 | | /// </summary> |
| | | 184 | | /// <param name="price"></param> |
| | | 185 | | /// <returns></returns> |
| | | 186 | | private int CompareToPrice(int price) |
| | 7 | 187 | | { |
| | 7 | 188 | | return Price.CompareTo(price); |
| | 7 | 189 | | } |
| | | 190 | | |
| | | 191 | | /// <summary> |
| | | 192 | | /// Time complexity - O(1) |
| | | 193 | | /// Space complexity - O(1) |
| | | 194 | | /// </summary> |
| | | 195 | | /// <param name="shop"></param> |
| | | 196 | | /// <returns></returns> |
| | | 197 | | private int CompareToShop(int shop) |
| | 3 | 198 | | { |
| | 3 | 199 | | return Shop.CompareTo(shop); |
| | 3 | 200 | | } |
| | | 201 | | } |
| | | 202 | | |
| | 14 | 203 | | private readonly record struct RentalRecord(int Shop, int Movie, int Price) : IComparable<RentalRecord> |
| | | 204 | | { |
| | 3 | 205 | | private readonly int[] _report = new int[2]; |
| | | 206 | | |
| | | 207 | | /// <summary> |
| | | 208 | | /// Time complexity - O(1) |
| | | 209 | | /// Space complexity - O(1) |
| | | 210 | | /// </summary> |
| | | 211 | | /// <param name="rentalRecord"></param> |
| | | 212 | | /// <returns></returns> |
| | | 213 | | public int CompareTo(RentalRecord rentalRecord) |
| | 3 | 214 | | { |
| | 3 | 215 | | var priceComparison = CompareToPrice(rentalRecord.Price); |
| | | 216 | | |
| | 3 | 217 | | if (priceComparison != 0) |
| | 2 | 218 | | { |
| | 2 | 219 | | return priceComparison; |
| | | 220 | | } |
| | | 221 | | |
| | 1 | 222 | | var shopComparison = CompareToShop(rentalRecord.Shop); |
| | | 223 | | |
| | 1 | 224 | | return shopComparison != 0 ? shopComparison : CompareToMovie(rentalRecord.Movie); |
| | 3 | 225 | | } |
| | | 226 | | |
| | | 227 | | /// <summary> |
| | | 228 | | /// Time complexity - O(1) |
| | | 229 | | /// Space complexity - O(1) |
| | | 230 | | /// </summary> |
| | | 231 | | /// <returns></returns> |
| | | 232 | | public int[] GetReport() |
| | 2 | 233 | | { |
| | 2 | 234 | | _report[0] = Shop; |
| | 2 | 235 | | _report[1] = Movie; |
| | | 236 | | |
| | 2 | 237 | | return _report; |
| | 2 | 238 | | } |
| | | 239 | | |
| | | 240 | | /// <summary> |
| | | 241 | | /// Time complexity - O(1) |
| | | 242 | | /// Space complexity - O(1) |
| | | 243 | | /// </summary> |
| | | 244 | | /// <param name="price"></param> |
| | | 245 | | /// <returns></returns> |
| | | 246 | | private int CompareToPrice(int price) |
| | 3 | 247 | | { |
| | 3 | 248 | | return Price.CompareTo(price); |
| | 3 | 249 | | } |
| | | 250 | | |
| | | 251 | | /// <summary> |
| | | 252 | | /// Time complexity - O(1) |
| | | 253 | | /// Space complexity - O(1) |
| | | 254 | | /// </summary> |
| | | 255 | | /// <param name="shop"></param> |
| | | 256 | | /// <returns></returns> |
| | | 257 | | private int CompareToShop(int shop) |
| | 1 | 258 | | { |
| | 1 | 259 | | return Shop.CompareTo(shop); |
| | 1 | 260 | | } |
| | | 261 | | |
| | | 262 | | /// <summary> |
| | | 263 | | /// Time complexity - O(1) |
| | | 264 | | /// Space complexity - O(1) |
| | | 265 | | /// </summary> |
| | | 266 | | /// <param name="movie"></param> |
| | | 267 | | /// <returns></returns> |
| | | 268 | | private int CompareToMovie(int movie) |
| | 1 | 269 | | { |
| | 1 | 270 | | return Movie.CompareTo(movie); |
| | 1 | 271 | | } |
| | | 272 | | } |
| | | 273 | | } |