diff --git a/posts/algorithms/leetcode-daily.html b/posts/algorithms/leetcode-daily.html index e828c32..9e10231 100644 --- a/posts/algorithms/leetcode-daily.html +++ b/posts/algorithms/leetcode-daily.html @@ -97,37 +97,35 @@ items with a linear scan will take \(O(nlg(n))\) time, meeting the constraints.

- -

fleshing out the approach

-

- A few specifics need to be understood before coding up the approach: -

- -

- carrying out the plan -

-
-
vector maximumBeauty(vector>& items, vector& queries) {
+            

carrying out the plan

+

+ A few specifics need to be understood before coding up the + approach: +

+
    +
  • + Re-ordering the queries: couple query[i] with + i, then sort. When responding to queries in sorted + order, we know where to place them in an output + container—index i. +
  • +
  • + The linear scan: accumulate a running maximal beauty, starting + at index 0. For some query query, we + want to consider all items with price less than or equal to + query. Therefore, loop until this condition is + violated— the previous index will represent the + last considered item. +
  • +
  • + Edge cases: it's perfectly possible the last considered + item is invalid (consider a query cheaper than the cheapest + item). Return 0 as specified by the problem + constraints. +
  • +
+
+
vector<int> maximumBeauty(vector<vector<int>>& items, vector<int>& queries) {
   std::sort(items.begin(), items.end());
   std::vector<pair<int, int>> sorted_queries;
   sorted_queries.reserve(queries.size());
@@ -139,7 +137,7 @@
 
   int beauty = items[0][1];
   size_t i = 0;
-  std::vector ans(queries.size());
+  std::vector<int> ans(queries.size());
 
   for (const auto [query, index] : sorted_queries) {
     while (i < items.size() && items[i][0] <= query) {
@@ -152,6 +150,30 @@
 
   return std::move(ans);
+

asymptotic complexity

+

+ Let n=len(items) and m=len(queries). + There may be more items than queries, or vice versa. Note that a + “looser” upper bound can be found by analyzing the + runtime in terms of \(max\{n,m\}\). +

+

+ Time Complexity: \(O(nlg(n)+mlg(m)+m)=O(nlg(n)+mlg(m))\). + An argument can be made that because + queries[i],items[i][{0,1}]\(\leq10^9\), radix sort + can be leveraged to achieve a time complexity of \(O(d \cdot (n + + k + m + k)) = O(9 \cdot (n + m + 20)) = O(9n+9m + 180) = O(9n+9m) + = O(n+m)\). +

+

+ Space Complexity: \(\Theta(1)\), considering that \(O(m)\) + space must be allocated. If queries/items + cannot be modified in-place, increase the space complexity by + \(m\)/\(n\) respectively. +

+

shortest subarray with or at least k ii - — 9/12/24 + — 9/11/24

@@ -341,7 +363,7 @@ href="https://leetcode.com/problems/minimum-array-end/" >minimum array end - — 9/11/24 + — 9/10/24