Featured image of post Weekly Report 2024.13

Weekly Report 2024.13

What a lovely world

  • Real life is always out of expectation.
  • Accept it or be more greedy (with plan and action, and feedback).

Entertainment

Itasha Paradise

People are serious dudes, and they truly have showed their love.

. . . . . . .

Learning

Review DDIA

  • The comparison between relational model and NoSQL (JSON) model.
    • Self-contained data is fit to use NoSQL model, say JSON. The first version of Linked Resume was based on JSON (according to the book?). But as the business model grows, it just becomes not as efficient as before.
    • Meanwhile, relational model is fit to query data followed by a many-to-many relation.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
{
  "user_id": "",
  // what if we need to add hyperlink and other descriptions to the previous companies
  // - for nosql, we maybe need to query multiple times, and assemble the data incode
  // - for relational model, those table joins become the redemy
  "positions": [
    {
      "company": "Microsoft"
    }
  ]
}

Sliding Window

Well, I still don’t think I have understood sliding window questions yet, my initial thought was always wrong, and I would choose a more-complicated data-structure as the window, while the perfect solution from @lee215 was so concise, only one or two integers will suffice.

I think the critical part of sliding window is to think clearly when to move the right pointer, when to move the left pointer, also, what should be put into the window, and what should be add to the result, in most times, it’s the range (r-l+1).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution:
  def subarraysWithKDistinct(self, nums, k) -> int:
    """
    Sliding Window
    992. Subarrays with K Different Integers
    https://leetcode.com/problems/subarrays-with-k-different-integers/description/
    """
    return self.at_most(nums, k) - self.at_most(nums, k-1)

  # subarray with at most k distinct numbers
  def at_most(self, nums, k) -> int:
    n = len(nums)
    l = r = 0
    res = 0
    window = Counter()
    while r < n:
      if window[nums[r]] == 0:
        k -= 1
      window[nums[r]] += 1
      # we had used all k slots
      while k < 0:
        window[nums[l]] -= 1
        if window[nums[l]] == 0:
          k += 1
        l += 1
      res += (r-l)+1
      r += 1
    return res

Life

  • Only you can focus on your life.
  • Tokyo’s weather was so weird, rainy day with strong wind, I did sacrifice my umbrella again. And because of this, the sakura season was delayed yet again, and we didn’t get to see the sakura blossom around Chiyoda.
  • Itasha Paradise was really good? People showed their affections by all means, which was what I lacked right now.

Sharing

无聊的解药是好奇心, 而好奇心却无药可解.

Recommend Articles

References

Licensed under CC BY-NC-SA 4.0
Last updated on Jan 09, 2024 11:32 CST
The older I get, the more I realize that most of life is a matter of what we pay attention to, of what we attend to [with focus].
Built with Hugo
Theme Stack designed by Jimmy