mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-02 05:13:29 +08:00
update
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
<p>An integer <code>n</code> is <strong>strictly palindromic</strong> if, for <strong>every</strong> base <code>b</code> between <code>2</code> and <code>n - 2</code> (<strong>inclusive</strong>), the string representation of the integer <code>n</code> in base <code>b</code> is <strong>palindromic</strong>.</p>
|
||||
|
||||
<p>Given an integer <code>n</code>, return <code>true</code> <em>if </em><code>n</code><em> is <strong>strictly palindromic</strong> and </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 9
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> In base 2: 9 = 1001 (base 2), which is palindromic.
|
||||
In base 3: 9 = 100 (base 3), which is not palindromic.
|
||||
Therefore, 9 is not strictly palindromic so we return false.
|
||||
Note that in bases 4, 5, 6, and 7, n = 9 is also not palindromic.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> We only consider base 2: 4 = 100 (base 2), which is not palindromic.
|
||||
Therefore, we return false.
|
||||
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>4 <= n <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>Given a <strong>0-indexed</strong> integer array <code>nums</code>, determine whether there exist <strong>two</strong> subarrays of length <code>2</code> with <strong>equal</strong> sum. Note that the two subarrays must begin at <strong>different</strong> indices.</p>
|
||||
|
||||
<p>Return <code>true</code><em> if these subarrays exist, and </em><code>false</code><em> otherwise.</em></p>
|
||||
|
||||
<p>A <b>subarray</b> is a contiguous non-empty sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,2,4]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The subarrays with elements [4,2] and [2,4] have the same sum of 6.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> No two subarrays of size 2 have the same sum.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,0,0]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The subarrays [nums[0],nums[1]] and [nums[1],nums[2]] have the same sum of 0.
|
||||
Note that even though the subarrays have the same content, the two subarrays are considered different because they are in different positions in the original array.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 1000</code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,27 @@
|
||||
<p>Given two strings <code>needle</code> and <code>haystack</code>, return the index of the first occurrence of <code>needle</code> in <code>haystack</code>, or <code>-1</code> if <code>needle</code> is not part of <code>haystack</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> haystack = "sadbutsad", needle = "sad"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> "sad" occurs at index 0 and 6.
|
||||
The first occurrence is at index 0, so we return 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> haystack = "leetcode", needle = "leeto"
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> "leeto" did not occur in "leetcode", so we return -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= haystack.length, needle.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>haystack</code> and <code>needle</code> consist of only lowercase English characters.</li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> binary matrix <code>mat</code> and an integer <code>cols</code>, which denotes the number of columns you must choose.</p>
|
||||
|
||||
<p>A row is <strong>covered</strong> by a set of columns if each cell in the row that has a value of <code>1</code> also lies in one of the columns of the chosen set.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of rows that can be <strong>covered</strong> by a set of </em><code>cols</code><em> columns.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2022/07/14/rowscovered.png" style="width: 250px; height: 417px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[0,0,0],[1,0,1],[0,1,1],[0,0,1]], cols = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
As shown in the diagram above, one possible way of covering 3 rows is by selecting the 0th and 2nd columns.
|
||||
It can be shown that no more than 3 rows can be covered, so we return 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2022/07/14/rowscovered2.png" style="width: 83px; height: 247px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[1],[0]], cols = 1
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
Selecting the only column will result in both rows being covered, since the entire matrix is selected.
|
||||
Therefore, we return 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == mat.length</code></li>
|
||||
<li><code>n == mat[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 12</code></li>
|
||||
<li><code>mat[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
<li><code>1 <= cols <= n</code></li>
|
||||
</ul>
|
@@ -0,0 +1,35 @@
|
||||
<p>You have <code>n</code> robots. You are given two <strong>0-indexed</strong> integer arrays, <code>chargeTimes</code> and <code>runningCosts</code>, both of length <code>n</code>. The <code>i<sup>th</sup></code> robot costs <code>chargeTimes[i]</code> units to charge and costs <code>runningCosts[i]</code> units to run. You are also given an integer <code>budget</code>.</p>
|
||||
|
||||
<p>The <strong>total cost</strong> of running <code>k</code> chosen robots is equal to <code>max(chargeTimes) + k * sum(runningCosts)</code>, where <code>max(chargeTimes)</code> is the largest charge cost among the <code>k</code> robots and <code>sum(runningCosts)</code> is the sum of running costs among the <code>k</code> robots.</p>
|
||||
|
||||
<p>Return<em> the <strong>maximum</strong> number of <strong>consecutive</strong> robots you can run such that the total cost <strong>does not</strong> exceed </em><code>budget</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> chargeTimes = [3,6,1,3,4], runningCosts = [2,1,3,4,5], budget = 25
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
It is possible to run all individual and consecutive pairs of robots within budget.
|
||||
To obtain answer 3, consider the first 3 robots. The total cost will be max(3,6,1) + 3 * sum(2,1,3) = 6 + 3 * 6 = 24 which is less than 25.
|
||||
It can be shown that it is not possible to run more than 3 consecutive robots within budget, so we return 3.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> chargeTimes = [11,12,19], runningCosts = [10,8,7], budget = 19
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> No robot can be run that does not exceed the budget, so we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>chargeTimes.length == runningCosts.length == n</code></li>
|
||||
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= chargeTimes[i], runningCosts[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= budget <= 10<sup>15</sup></code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user