<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>O(k) Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/ok/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/ok/</link>
	<description></description>
	<lastBuildDate>Sun, 26 Apr 2020 06:57:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>O(k) Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/ok/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 1423. Maximum Points You Can Obtain from Cards</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1423-maximum-points-you-can-obtain-from-cards/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1423-maximum-points-you-can-obtain-from-cards/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 26 Apr 2020 06:47:08 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[O(k)]]></category>
		<category><![CDATA[sliding window]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6659</guid>

					<description><![CDATA[<p>There are several cards&#160;arranged in a row, and each card has an associated number of points&#160;The points are given in the integer array&#160;cardPoints. In one&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1423-maximum-points-you-can-obtain-from-cards/">花花酱 LeetCode 1423. Maximum Points You Can Obtain from Cards</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>There are several cards&nbsp;<strong>arranged in a row</strong>, and each card has an associated number of points&nbsp;The points are given in the integer array&nbsp;<code>cardPoints</code>.</p>



<p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly&nbsp;<code>k</code>&nbsp;cards.</p>



<p>Your score is the sum of the points of the cards you have taken.</p>



<p>Given the integer array&nbsp;<code>cardPoints</code>&nbsp;and the integer&nbsp;<code>k</code>, return the&nbsp;<em>maximum score</em>&nbsp;you can obtain.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> cardPoints = [1,2,3,4,5,6,1], k = 3
<strong>Output:</strong> 12
<strong>Explanation:</strong> After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> cardPoints = [2,2,2], k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> Regardless of which two cards you take, your score will always be 4.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> cardPoints = [9,7,7,9,7,7,9], k = 7
<strong>Output:</strong> 55
<strong>Explanation:</strong> You have to take all the cards. Your score is the sum of points of all cards.
</pre>



<p><strong>Example 4:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> cardPoints = [1,1000,1], k = 1
<strong>Output:</strong> 1
<strong>Explanation:</strong> You cannot take the card in the middle. Your best score is 1. 
</pre>



<p><strong>Example 5:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> cardPoints = [1,79,80,1,1,1,200,1], k = 3
<strong>Output:</strong> 202
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= cardPoints.length &lt;= 10^5</code></li><li><code>1 &lt;= cardPoints[i] &lt;= 10^4</code></li><li><code>1 &lt;= k &lt;= cardPoints.length</code></li></ul>



<p><strong>Solution: Sliding Window</strong></p>



<p>Time complexity: O(k)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int maxScore(vector&lt;int&gt;&amp; p, int k) {
    const int n = p.size();
    int cur = accumulate(begin(p), begin(p) + k, 0);
    int ans = cur;
    for (int i = 0; i &lt; k; ++i) {
      cur = cur - p[k - i - 1] + p[n - i - 1];
      ans = max(ans, cur);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1423-maximum-points-you-can-obtain-from-cards/">花花酱 LeetCode 1423. Maximum Points You Can Obtain from Cards</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-1423-maximum-points-you-can-obtain-from-cards/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
