Sliding Window : 3

Sliding Window : 3

Day 2/75:

Question: Count Subarrays With Score Less Than K- (Hard)

Problem Statement: The score of an array is defined as the product of its sum and its length.

For example, the score of [1, 2, 3, 4, 5] is (1 + 2 + 3 + 4 + 5)*5 = 75.

Given a positive integer array nums and an integer k, return the number of non-empty subarrays of nums whose score is strictly less than k.

Input: nums = [2,1,4,3,5], k = 10
Output: 6
Explanation:
The 6 subarrays having scores less than 10 are:
- [2] with score 2 * 1 = 2.
- [1] with score 1 * 1 = 1.
- [4] with score 4 * 1 = 4.
- [3] with score 3 * 1 = 3. 
- [5] with score 5 * 1 = 5.
- [2,1] with score (2 + 1) * 2 = 6.
Note that subarrays such as [1,4] and [4,3,5] are not considered because their scores are 10 and 36 respectively, while we need scores strictly less than 10.        

Approach:

  • Initialization:We initialize two pointers, start and end, to mark the boundaries of our window.Initialize the sum variable to 0, the sum of elements of current window.Initialize the count variable to 0, which will keep track of the number of valid subarrays.
  • Sliding Window Technique:
  • Start with two pointers, one at the beginning and one at the end of the array.
  • Move the end pointer forward, adding each element to the sum.
  • If the sum of the current window multiplied by its size is greater than or equal to K, shrink the window from the left until it's within the target range.
  • Count the number of subarrays formed by the valid windows.
  • Repeat this process until you reach the end of the array.Result: Return the total count of valid subarrays.

Time Complexity: The time complexity of this approach is O(N), where N is the length of the input array. We traverse the array only once with our sliding window, making it a linear time solution.

Space Complexity: The space complexity is O(1) since we use a constant amount of extra space for variables, regardless of the input size. Hence, our solution is memory-efficient.






要查看或添加评论,请登录

Suyash Awathe的更多文章

  • Tree: Add One Row to Tree

    Tree: Add One Row to Tree

    Day 17: Problem Statement: Given the root of a binary tree and two integers val and depth, add a row of nodes with…

  • Monotonic Array / TwoPointer :Trapping Rain Water: Most Asked !!

    Monotonic Array / TwoPointer :Trapping Rain Water: Most Asked !!

    Day 16/75 : Problem : Trapping Rain Water -(Hard) Given n non-negative integers representing an elevation map where the…

  • Stack: April POTD

    Stack: April POTD

    Question: Remove K Digits -(Medium) Given string num representing a non-negative integer num, and an integer k, return…

  • Queue-Array: April POTD

    Queue-Array: April POTD

    Day 14/75: Question: Reveal Cards In Increasing Order -(Medium) You are given an integer array . There is a deck of…

    1 条评论
  • Array: 9 April POTD

    Array: 9 April POTD

    Day 13/75: Question: Time Needed to Buy Tickets -(Easy) There are people in a line queuing to buy tickets, where the…

  • Queue : April POTD

    Queue : April POTD

    Day 12/75: Question: Number of Students Unable to Eat Lunch -(Easy) The school cafeteria offers circular and square…

    1 条评论
  • String : Parentheses : April-POTD

    String : Parentheses : April-POTD

    Day 11/75: Question: Minimum Remove to Make Valid Parentheses -(Medium) Given a string s of , and lowercase English…

    3 条评论
  • String : 5April POTD

    String : 5April POTD

    Day 10/75: Question: Make The String Great -(Easy) Given a string of lower and upper case English letters. A good…

  • String Parentheses : 4April POTD

    String Parentheses : 4April POTD

    Day 9/75: Question: Maximum Nesting Depth of the Parentheses -(Easy) A string is a valid parentheses string (denoted…

  • BackTracking : WordSearch :4April POTD

    BackTracking : WordSearch :4April POTD

    Day 9/75: Question: Word Search -(Medium) Given an m x n grid of characters board and a string word, return true if…

    1 条评论

社区洞察

其他会员也浏览了