< Nafees />
github
  • Assignments - DSA
  • Long Integer Operations
  • OOP
    • Introduction
    • Implementation
  • Sorting Algorithms
    • Selection Sort
    • Bubble Sort
    • Insertion Sort
    • Shell Sort
    • Shuffle
    • Merge Sort
    • Convex Hull
    • Quick sort
    • System Sort
    • Heap-Sort
  • Binary Search
  • Binary Search By Recursion
  • Two-Pointer
  • String
  • LeetCode 75
    • Array/String
    • Hash Map
    • BST
    • Binary Search
  • Top Interview 150
    • Linked-List
  • Leetcode Programming Skill
    • Math
  • Leetcode 75
  • 🤡Leet Code Extra
  • Arrays
    • 1-D Array
    • 2-D Arrays
  • 👨‍🍳Basic Math - Codechef
  • ☠️Recursion
  • 😁Public Member Functions
    • Strings Functions
  • 👾Linked List
    • What's Linked List & Implementation?
      • Singly Linked List
      • Doubly Linked List
    • Problems
  • 📚Stack
    • What's Stack & Implementation?
      • Stack Using Array
      • Stack using LL
    • Problems
  • 🏧Queue
    • What's Queue & Implementation?
      • Simple Queue
      • Queue using LL
      • Circular Queue
      • Deque using Linked List
      • STL Deque
    • Problems
  • 🏧Priority Queue
    • What's Priority Queue & Implementation
      • OrderedArrayMaxPQ.Java
      • Maximum-Oriented PQ using Binary Heap
      • Minimum-Oriented PQ using Binary Heap
    • Problems
  • 🗓️Hash Table
    • What's Hash Table & Implementation
      • ST - Seperate Chaining
      • ST - Linear Probing
    • Problems
  • 🎴Symbol Table
    • What's Symbol Table and implementation
      • ST Using Binary search (ordered array)
      • ST Using Binary Search Tree
      • ST Using Left-Leaning Red-Black Tree
      • ST Using Hash Table
    • Problems
  • 🔗Union-Find (Dynamic Connectivity problem)
    • What is a Union Find Data Structure?
    • Implementation
  • 🎋Binary Tree
    • What's Binary Tree & Implementation?
      • Traversal
      • Red-Black BST
  • 🌴Trie
    • What's Trie & Implementation?
    • Problems
  • 😎Project
    • Expression Evaluation
Powered by GitBook
On this page
  • 162. Find Peak Element
  • 1456. Maximum Number of Vowels in a Substring of Given Length

Was this helpful?

  1. LeetCode 75

Binary Search

PreviousBSTNextTop Interview 150

Last updated 10 months ago

Was this helpful?

class Solution {
public:
    void sol(vector<int>& nums, int s, int e, int& res, int& index) {
        if(s >= e) {
            if(res < nums[s]) {
                res = nums[s];
                index = s;
            }
            return;
        }
        int mid = s +(e-s)/2;
        sol(nums, s, mid, res, index);
        sol(nums, mid+1, e, res, index);
    }

    int findPeakElement(vector<int>& nums) {
        int res = INT_MIN;
        int i = 0;
        sol(nums, 0, nums.size()-1, res, i);
        return i;
    }
};
TLE - 102 / 106
class Solution {
public:
    bool checkVowels(char ch) {
        return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
    }

    int maxVowels(string s, int k) {
        int count = 0;
        int res = 0;

        for(int i = 0; i <= s.size()-k; i++) {
            count = 0;
            int j = i;
            while(j < i+k ){
                if(checkVowels(s[j])) count++;
                j++;
            }
            res = max(res, count);
        }
        return res;
    }
};
Memory Limit Exceed - 80/106 (TC Optimized Sol.)
class Solution {
public:
    bool checkVowels(char ch) {
        return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
    }
    void sol(string str, int s, int e, int& count) {
        if(s >= e) {
            if(checkVowels(str[s])) {
                count++;
            }
            return;
        }
        int mid = s +(e-s)/2;
        sol(str, s, mid, count);
        sol(str, mid+1, e, count);
    }

    int maxVowels(string s, int k) {
        int count = 0;
        int res = 0;

        for(int i = 0; i <= s.size()-k; i++) {
            count = 0;
            sol(s, i, i+k-1, count);
            res = max(res, count);
        }
        return res;
    }
};

162. Find Peak Element
1456. Maximum Number of Vowels in a Substring of Given Length