Partition Labels (LeetCode 763) | Solution with animations and examples | Study Algorithms

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 พ.ย. 2024

ความคิดเห็น • 17

  • @himanyukubov1624
    @himanyukubov1624 10 หลายเดือนก่อน +1

    its like fresh breath after neetcode videos) thank you

    • @srinathm4899
      @srinathm4899 9 หลายเดือนก่อน

      plus one!!!

  • @gauravkumar-ek8mr
    @gauravkumar-ek8mr 2 ปีที่แล้ว +1

    your new look is as nice as your videos man. Really awesome. your sorting algo videos really help me in my technical interview.

  • @longtreegreen
    @longtreegreen 2 ปีที่แล้ว +1

    we can also iterate from start, all occurrences will be updated in the map, so we will get last indexes of all chars.

  • @sozkaya
    @sozkaya 2 หลายเดือนก่อน

    My observation: If we fill a List of intervals with [first appearance, last appearance] for each char. Then the question suddenly turns into the 'merge intervals'

  • @udayagiriswetha5604
    @udayagiriswetha5604 2 ปีที่แล้ว

    You Explained very well, thank you, and make more videos like this.

  • @YogendraRajA
    @YogendraRajA 2 ปีที่แล้ว

    great explanation! Thank you.

  • @vcs649
    @vcs649 ปีที่แล้ว

    fantastic explanation 👍

  • @subee128
    @subee128 11 หลายเดือนก่อน +1

    Thank you

  • @akhileshgoel339
    @akhileshgoel339 ปีที่แล้ว +1

    idk why this channel has so less subcribers

    • @nikoo28
      @nikoo28  ปีที่แล้ว

      i wonder the same too 😅, please share as much as possible.

  • @sakshiarora8379
    @sakshiarora8379 4 หลายเดือนก่อน

    what is this pen and notebook that you write into ? I like your teaching style , Can you please share about how you make it so easy to highlight

  • @ARPITJAIN-k1y
    @ARPITJAIN-k1y 8 หลายเดือนก่อน

    why did u not put any iteration expression like I++ in the for loop?

    • @nikoo28
      @nikoo28  7 หลายเดือนก่อน

      That gets confusing when you review your code

  • @mock1112
    @mock1112 ปีที่แล้ว

    The time complexity isn’t O(n^2)?

    • @nikoo28
      @nikoo28  ปีที่แล้ว

      Observe carefully, we iterate through the string only once.

    • @jst8922
      @jst8922 4 หลายเดือนก่อน

      Q: Can worst-case time complexity be reduced in any way ?
      Yes, the worst-case time complexity can be significantly reduced from O(n^2) to O(n). Here's how we can optimize the algorithm:
      1. Use an array to store the last occurrence of each character.
      2. Make a single pass through the string to find partitions.
      Here's an optimized version of the code:
      ```java
      class PartitionLabels {
      public List partitionLabels(String s) {
      List partitions = new ArrayList();
      if (s == null || s.length() == 0) return partitions;
      // Step 1: Create an array to store the last index of each character
      int[] lastIndex = new int[26];
      for (int i = 0; i < s.length(); i++) {
      lastIndex[s.charAt(i) - 'a'] = i;
      }
      // Step 2: Find partitions in a single pass
      int start = 0, end = 0;
      for (int i = 0; i < s.length(); i++) {
      end = Math.max(end, lastIndex[s.charAt(i) - 'a']);
      if (i == end) {
      partitions.add(end - start + 1);
      start = i + 1;
      }
      }
      return partitions;
      }
      }
      ```
      Time Complexity Analysis:
      1. The first loop to fill the lastIndex array is O(n).
      2. The second loop to find partitions is also O(n).
      3. All operations inside the loops are O(1).
      Therefore, the overall time complexity is O(n), where n is the length of the input string.
      Space Complexity:
      1. We use an additional array of size 26 (constant space).
      2. The output list in the worst case could be of size n/2 if every other character forms a partition.
      The space complexity remains O(n).
      This optimized version eliminates the nested loops and repeated calls to lastIndexOf, significantly improving the time complexity while maintaining the same space complexity. It's much more efficient, especially for larger inputs.