I bet you will Understand!!! Peak and Valley Python 3
\nThis question can be analyzed as a series of peaks and valleys\nwe have to delete all starting point of valleys till attempts(k) are left!!\n\nfor ex - test c
\nThis question can be analyzed as a series of peaks and valleys\nwe have to delete all **starting point of valleys** till attempts(k) are left!!\n\nfor ex - test case [1,4,3,2,2,1,9]\n\n**Elements inside [ ] are starting point of valleys which we have to delete**\n\n```\n                        9\n                       /\n     [4]              /\n    /  \\             /\n   /    [3]         /\n  /      \\         /\n /        2__[2]  /\n/              \\ / \n1               1\n```\n\n\n**We can delete valleys untils we can afford to delete it i.e(k>0)**\n_____\n**Case 1: valley>k**\n_____\nSupoose total valley\'s starting point in our test case is 5 but we can delete only 3 elements i.e k=3 in this case we will delete first 3 valley which will give our ans \n____\n**Case 2: valley<k**\n____\nSuppose there is a testcase where valley = 2 and k = 5 so after deleting all the valley still we have to delete so we pop remaining k from our stack\n\n\njust delete the valleys until k>=0 each time we delete a valley we do k--\n. we do a check before returning ans if k>0 simply start \npopping from last of stack \n\nP.S - Try drawing valley and peak sketch for other test cases same as above\ni bet you will understand till the core!!!\n\n\n```\nclass Solution:\n\n    # O(N) O(N)\n    def removeKdigits(self, nums: str, k: int) -> str:\n\n        if len(nums)==1: # if only 1 digit we can only get 0 \n            return "0"\n\n        stack = []\n        \n        for num in nums:\n            # k--> no of deletion\n            # len(nums)-k --> len of our answer --> len of stack at last\n            while stack and stack[-1]>num and k>0:\n                stack.pop()\n                k-=1\n            stack.append(num)\n\n        # a case like [112] k = 1    no deletion will happen because there is no valley\n        # so we pop k elements from last\n        while k>0 and stack:\n            stack.pop()\n            k-=1\n \n        return \'\'.join(stack).lstrip("0") or "0"  # lstrip is for removal of cases where stack is  000200\n\t\t\n\t\t# but our ans should be 200\n\t\t\n```
['Stack', 'Python', 'Python3']