DEV Community

vishal.codes
vishal.codes

Posted on

Day 4/366

πŸš€ Today's Learning:

🌟 DSA

  • Two Sum - Leetcode 1
  • Reverse Linked List - Leetcode 206

🌟 Dev

  • Difference between inline and block elements.
  • What is difference between display:none and display:hidden.
  • Flex and Grid which layout approach should you use and when to use.
  • Media Queries.

πŸ” Some Key Highlights:

DSA

Two Sum - Leetcode 1

Brute force β†’ Iterate through each element β†’ for each element check for the compliment (target - nums[i]) in rest of the elements β†’ if found push both the current element’s index and the complement’s index in ans array. (T.C. O(n^2) | S.C. O(1))

Optimized Approach β†’ take an unordered map and fill it with element and index key-value pairs. Iterate through the nums array β†’ for each element find the compliment (target - nums[i]) in map, which takes constant time to search β†’ If compliment found push both current element index and compliment index in ans array (Note: Also while finding compliment ensure that compliment’s index should not be same as current element’s index. (T.C. O(n) | S.C. O(n))

Image description

Reverse Linked List - Leetcode 206

Approach 1 (Brute Force)

Take an extra auxiliary array β†’ copy all linked list data in this array β†’ reverse travel the array and replace linked list data. (Time: O(n) | Space: O(n) - coz of extra array)

Approach 2 (Optimized for space - In place solution - Reversing links)

Take 3 pointers p, q and r β†’ these are sliding pointers (order: r, q, p) β†’ at each pass slide all three to one step right β†’ use q to reverse the link β†’ do this untill p reaches null β†’ At this time q would be at last node β†’ make head point this last node.
(Time: O(n) | Space: O(1) )

Image description

DEV

  1. Inline vs. Block Elements:
    • Block elements are like big building blocks, stacking on top of each other and taking up the full width.
    • Inline elements are smaller and fit within a line of text, like links or emphasis.
  2. Background Image with Opacity and Text Overlay:
    • Use CSS to set a background image on a container and add text on top of it.
    • Apply opacity to the background image without affecting the text by using RGBA colors or the opacity property.
  3. Display:none vs. Display:hidden:
    • display:none completely removes an element from the layout.
    • display:hidden hides an element from view but still occupies space in the layout.
  4. Flexbox vs. CSS Grid Layout:
    • Flexbox is great for simpler, one-dimensional layouts like navigation menus.
    • CSS Grid is perfect for more complex, two-dimensional layouts with rows and columns.
  5. Media Queries:
    • Media queries allow you to adapt your website's layout and styles based on different devices and screen sizes.
    • They help ensure your website looks fantastic across various devices, from laptops to smartphones.

Top comments (0)