{"id":7736,"date":"2020-11-29T02:11:47","date_gmt":"2020-11-29T10:11:47","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7736"},"modified":"2020-11-29T02:15:36","modified_gmt":"2020-11-29T10:15:36","slug":"leetcode-1673-find-the-most-competitive-subsequence","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/uncategorized\/leetcode-1673-find-the-most-competitive-subsequence\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1673. Find the Most Competitive Subsequence"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>nums<\/code>&nbsp;and a positive integer&nbsp;<code>k<\/code>, return&nbsp;<em>the most<strong>&nbsp;competitive<\/strong>&nbsp;subsequence of&nbsp;<\/em><code>nums<\/code>&nbsp;<em>of size&nbsp;<\/em><code>k<\/code>.<\/p>\n\n\n\n<p>An array&#8217;s subsequence is a resulting sequence obtained by erasing some (possibly zero) elements from the array.<\/p>\n\n\n\n<p>We define that a subsequence&nbsp;<code>a<\/code>&nbsp;is more&nbsp;<strong>competitive<\/strong>&nbsp;than a subsequence&nbsp;<code>b<\/code>&nbsp;(of the same length) if in the first position where&nbsp;<code>a<\/code>&nbsp;and&nbsp;<code>b<\/code>&nbsp;differ, subsequence&nbsp;<code>a<\/code>&nbsp;has a number&nbsp;<strong>less<\/strong>&nbsp;than the corresponding number in&nbsp;<code>b<\/code>. For example,&nbsp;<code>[1,3,4]<\/code>&nbsp;is more competitive than&nbsp;<code>[1,3,5]<\/code>&nbsp;because the first position they differ is at the final number, and&nbsp;<code>4<\/code>&nbsp;is less than&nbsp;<code>5<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [3,5,2,6], k = 2\n<strong>Output:<\/strong> [2,6]\n<strong>Explanation:<\/strong> Among the set of every possible subsequence: {[3,5], [3,2], [3,6], [5,2], [5,6], [2,6]}, [2,6] is the most competitive.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [2,4,3,3,5,4,9,6], k = 4\n<strong>Output:<\/strong> [2,3,3,4]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= nums[i] &lt;= 10<sup>9<\/sup><\/code><\/li><li><code>1 &lt;= k &lt;= nums.length<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Stack<\/strong><\/h2>\n\n\n\n<p>Use a stack to track the best solution so far, pop if the current number is less than the top of the stack and there are sufficient numbers left. Then push the current number to the stack if not full.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(k)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> mostCompetitive(vector<int>& nums, int k) {\n    const int n = nums.size();\n    vector<int> ans(k);\n    int c = 0;\n    for (int i = 0; i < n; ++i) {\n      while (c &#038;&#038; ans[c - 1] > nums[i] && c + n - i - 1 >= k) \n        --c;\n      if (c < k) ans[c++] = nums[i];\n    }\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"java\">\n\/\/ Author: Huahua\nclass Solution {\n  public int[] mostCompetitive(int[] nums, int k) {\n    int n = nums.length;\n    int[] ans = new int[k];\n    int c = 0;\n    for (int i = 0; i < n; ++i) {\n      while (c > 0 && ans[c - 1] > nums[i] && c + n - i - 1 >= k)\n        --c;\n      if (c < k) ans[c++] = nums[i];\n    }\n    return ans;\n  }\n}\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def mostCompetitive(self, nums: List[int], k: int) -> List[int]:\n    ans = [None] * k\n    n = len(nums)\n    c = 0\n    for i, x in enumerate(nums):\n      while c and ans[c - 1] > x and c + n - i - 1 >= k:\n        c -= 1\n      if c < k: \n        ans[c] = x\n        c += 1\n    return ans\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;nums&nbsp;and a positive integer&nbsp;k, return&nbsp;the most&nbsp;competitive&nbsp;subsequence of&nbsp;nums&nbsp;of size&nbsp;k. An array&#8217;s subsequence is a resulting sequence obtained by erasing some (possibly zero) elements&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7736","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7736"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7736\/revisions"}],"predecessor-version":[{"id":7738,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7736\/revisions\/7738"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}