feat: add solutions to lc problem: No.3307 (#4545)

No.3307.Find the K-th Character in String Game II
This commit is contained in:
Libin YANG 2025-07-04 06:25:47 +08:00 committed by GitHub
parent c692eef044
commit db8cc79baf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 194 additions and 38 deletions

View File

@ -251,6 +251,61 @@ impl Solution {
}
```
#### C#
```cs
public class Solution {
public char KthCharacter(long k, int[] operations) {
long n = 1;
int i = 0;
while (n < k) {
n *= 2;
++i;
}
int d = 0;
while (n > 1) {
if (k > n / 2) {
k -= n / 2;
d += operations[i - 1];
}
n /= 2;
--i;
}
return (char)('a' + (d % 26));
}
}
```
#### PHP
```php
class Solution {
/**
* @param Integer $k
* @param Integer[] $operations
* @return String
*/
function kthCharacter($k, $operations) {
$n = 1;
$i = 0;
while ($n < $k) {
$n *= 2;
++$i;
}
$d = 0;
while ($n > 1) {
if ($k > $n / 2) {
$k -= $n / 2;
$d += $operations[$i - 1];
}
$n /= 2;
--$i;
}
return chr(ord('a') + ($d % 26));
}
}
```
<!-- tabs:end -->
<!-- solution:end -->

View File

@ -248,6 +248,61 @@ impl Solution {
}
```
#### C#
```cs
public class Solution {
public char KthCharacter(long k, int[] operations) {
long n = 1;
int i = 0;
while (n < k) {
n *= 2;
++i;
}
int d = 0;
while (n > 1) {
if (k > n / 2) {
k -= n / 2;
d += operations[i - 1];
}
n /= 2;
--i;
}
return (char)('a' + (d % 26));
}
}
```
#### PHP
```php
class Solution {
/**
* @param Integer $k
* @param Integer[] $operations
* @return String
*/
function kthCharacter($k, $operations) {
$n = 1;
$i = 0;
while ($n < $k) {
$n *= 2;
++$i;
}
$d = 0;
while ($n > 1) {
if ($k > $n / 2) {
$k -= $n / 2;
$d += $operations[$i - 1];
}
$n /= 2;
--$i;
}
return chr(ord('a') + ($d % 26));
}
}
```
<!-- tabs:end -->
<!-- solution:end -->

View File

@ -0,0 +1,20 @@
public class Solution {
public char KthCharacter(long k, int[] operations) {
long n = 1;
int i = 0;
while (n < k) {
n *= 2;
++i;
}
int d = 0;
while (n > 1) {
if (k > n / 2) {
k -= n / 2;
d += operations[i - 1];
}
n /= 2;
--i;
}
return (char)('a' + (d % 26));
}
}

View File

@ -0,0 +1,25 @@
class Solution {
/**
* @param Integer $k
* @param Integer[] $operations
* @return String
*/
function kthCharacter($k, $operations) {
$n = 1;
$i = 0;
while ($n < $k) {
$n *= 2;
++$i;
}
$d = 0;
while ($n > 1) {
if ($k > $n / 2) {
$k -= $n / 2;
$d += $operations[$i - 1];
}
$n /= 2;
--$i;
}
return chr(ord('a') + ($d % 26));
}
}

View File

@ -8,7 +8,7 @@ tags:
<!-- problem:start -->
# [3601. Find Drivers with Improved Fuel Efficiency](https://leetcode.cn/problems/find-drivers-with-improved-fuel-efficiency)
# [3601. 寻找燃油效率提升的驾驶员](https://leetcode.cn/problems/find-drivers-with-improved-fuel-efficiency)
[English Version](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README_EN.md)
@ -16,7 +16,7 @@ tags:
<!-- description:start -->
<p>Table: <code>drivers</code></p>
<p>表:<code>drivers</code></p>
<pre>
+-------------+---------+
@ -25,11 +25,11 @@ tags:
| driver_id | int |
| driver_name | varchar |
+-------------+---------+
driver_id is the unique identifier for this table.
Each row contains information about a driver.
driver_id 是这张表的唯一主键。
每一行都包含一个司机的信息。
</pre>
<p>Table: <code>trips</code></p>
<p>表:<code>trips</code></p>
<pre>
+---------------+---------+
@ -41,31 +41,32 @@ Each row contains information about a driver.
| distance_km | decimal |
| fuel_consumed | decimal |
+---------------+---------+
trip_id is the unique identifier for this table.
Each row represents a trip made by a driver, including the distance traveled and fuel consumed for that trip.
trip_id 是这张表的唯一主键。
每一行表示一名司机完成的一次行程,包括该次行程行驶的距离和消耗的燃油量。
</pre>
<p>Write a solution to find drivers whose <strong>fuel efficiency has improved</strong> by <strong>comparing</strong> their average fuel efficiency in the<strong> first half</strong> of the year with the <strong>second half</strong> of the year.</p>
<p>编写一个解决方案,通过 <strong>比较</strong> 司机在 <strong>上半年</strong><strong>下半年</strong><strong>平均燃油效率</strong> 来找出 <strong>燃油效率有所提高</strong> 的司机。</p>
<ul>
<li>Calculate <strong>fuel efficiency</strong> as <code>distance_km / fuel_consumed</code> for <strong>each</strong> trip</li>
<li><strong>First half</strong>: January to June, <strong>Second half</strong>: July to December</li>
<li>Only include drivers who have trips in <strong>both halves</strong> of the year</li>
<li>Calculate the <strong>efficiency improvement</strong> as (<code>second_half_avg - first_half_avg</code>)</li>
<li><strong>Round </strong>all<strong> </strong>results<strong> </strong>to<strong> <code>2</code> </strong>decimal<strong> </strong>places</li>
<li>通过&nbsp;<code>distance_km / fuel_consumed</code>&nbsp;计算 <strong>每次</strong>&nbsp;行程的 <strong>燃油效率</strong></li>
<li><strong>上半年:</strong>一月到六月,<strong>下半年:</strong>七月到十二月</li>
<li>只包含在上半年和下半年都有行程的司机</li>
<li>通过(<code>second_half_avg - first_half_avg</code>)计算 <strong>提升效率</strong></li>
<li>将所有结果 <strong>四舍五入</strong> 到小数点后 <code>2</code>&nbsp;</li>
</ul>
<p>Return <em>the result table ordered by efficiency improvement in <strong>descending</strong> order, then by driver name in <strong>ascending</strong> order</em>.</p>
<p>返回结果表按提升效率&nbsp;<strong>降序</strong> 排列,然后按司机姓名 <strong>升序</strong> 排列。</p>
<p>The result format is in the following example.</p>
<p>结果格式如下所示。</p>
<p>&nbsp;</p>
<p><strong class="example">Example:</strong></p>
<p><strong class="example">示例:</strong></p>
<div class="example-block">
<p><strong>Input:</strong></p>
<p><strong>输入:</strong></p>
<p>drivers table:</p>
<p>drivers 表:</p>
<pre class="example-io">
+-----------+---------------+
@ -79,7 +80,7 @@ Each row represents a trip made by a driver, including the distance traveled and
+-----------+---------------+
</pre>
<p>trips table:</p>
<p>trips 表:</p>
<pre class="example-io">
+---------+-----------+------------+-------------+---------------+
@ -100,7 +101,7 @@ Each row represents a trip made by a driver, including the distance traveled and
+---------+-----------+------------+-------------+---------------+
</pre>
<p><strong>Output:</strong></p>
<p><strong>输出:</strong></p>
<pre class="example-io">
+-----------+---------------+------------------+-------------------+------------------------+
@ -111,39 +112,39 @@ Each row represents a trip made by a driver, including the distance traveled and
+-----------+---------------+------------------+-------------------+------------------------+
</pre>
<p><strong>Explanation:</strong></p>
<p><strong>解释:</strong></p>
<ul>
<li><strong>Alice Johnson (driver_id = 1):</strong>
<ul>
<li>First half trips (Jan-Jun): Feb 15 (120.5/10.2 = 11.81), Mar 20 (200.0/16.5 = 12.12)</li>
<li>First half average efficiency: (11.81 + 12.12) / 2 = 11.97</li>
<li>Second half trips (Jul-Dec): Aug 10 (150.0/11.0 = 13.64), Sep 25 (180.0/12.5 = 14.40)</li>
<li>Second half average efficiency: (13.64 + 14.40) / 2 = 14.02</li>
<li>Efficiency improvement: 14.02 - 11.97 = 2.05</li>
<li>上半年行程(一月到六月):Feb 15 (120.5/10.2 = 11.81), Mar 20 (200.0/16.5 = 12.12)</li>
<li>上半年平均效率:(11.81 + 12.12) / 2 = 11.97</li>
<li>下半年行程(七月到十二月):Aug 10 (150.0/11.0 = 13.64), Sep 25 (180.0/12.5 = 14.40)</li>
<li>下半年平均效率:(13.64 + 14.40) / 2 = 14.02</li>
<li>效率提升:14.02 - 11.97 = 2.05</li>
</ul>
</li>
<li><strong>Bob Smith (driver_id = 2):</strong>
<ul>
<li>First half trips: Jan 10 (100.0/9.0 = 11.11), Apr 15 (250.0/22.0 = 11.36)</li>
<li>First half average efficiency: (11.11 + 11.36) / 2 = 11.24</li>
<li>Second half trips: Oct 5 (200.0/15.0 = 13.33)</li>
<li>Second half average efficiency: 13.33</li>
<li>Efficiency improvement: 13.33 - 11.24 = 2.09</li>
<li>上半年行程:Jan 10 (100.0/9.0 = 11.11), Apr 15 (250.0/22.0 = 11.36)</li>
<li>上半年平均效率:(11.11 + 11.36) / 2 = 11.24</li>
<li>下半年行程:Oct 5 (200.0/15.0 = 13.33)</li>
<li>下半年平均效率:13.33</li>
<li>效率提升:13.33 - 11.24 = 2.09</li>
</ul>
</li>
<li><strong>Drivers not included:</strong>
<li><strong>未包含的司机:</strong>
<ul>
<li>Carol Davis (driver_id = 3): Only has trips in first half (Mar, May)</li>
<li>David Wilson (driver_id = 4): Only has trips in second half (Jul, Nov)</li>
<li>Emma Brown (driver_id = 5): Only has trips in first half (Feb)</li>
<li>Carol Davis (driver_id = 3):只有上半年的行程(三月,五月)</li>
<li>David Wilson (driver_id = 4):只有下半年的行程(七月,十一月)</li>
<li>Emma Brown (driver_id = 5):只有上半年的行程(二月)</li>
</ul>
</li>
</ul>
<p>The output table is ordered by efficiency improvement in descending order then by name in ascending order.</p>
<p>输出表按提升效率降序排列,然后按司机名字升序排列。</p>
</div>
<!-- description:end -->

View File

@ -320,7 +320,7 @@
| 3570 | [查找无可用副本的书籍](/solution/3500-3599/3570.Find%20Books%20with%20No%20Available%20Copies/README.md) | `数据库` | 简单 | |
| 3580 | [寻找持续进步的员工](/solution/3500-3599/3580.Find%20Consistently%20Improving%20Employees/README.md) | `数据库` | 中等 | |
| 3586 | [寻找 COVID 康复患者](/solution/3500-3599/3586.Find%20COVID%20Recovery%20Patients/README.md) | `数据库` | 中等 | |
| 3601 | [Find Drivers with Improved Fuel Efficiency](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README.md) | | 中等 | |
| 3601 | [寻找燃油效率提升的驾驶员](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README.md) | | 中等 | |
## 版权

View File

@ -3611,7 +3611,7 @@
| 3598 | [相邻字符串之间的最长公共前缀](/solution/3500-3599/3598.Longest%20Common%20Prefix%20Between%20Adjacent%20Strings%20After%20Removals/README.md) | `数组`,`字符串` | 中等 | 第 456 场周赛 |
| 3599 | [划分数组得到最小 XOR](/solution/3500-3599/3599.Partition%20Array%20to%20Minimize%20XOR/README.md) | `位运算`,`数组`,`动态规划`,`前缀和` | 中等 | 第 456 场周赛 |
| 3600 | [升级后最大生成树稳定性](/solution/3600-3699/3600.Maximize%20Spanning%20Tree%20Stability%20with%20Upgrades/README.md) | `贪心`,`并查集`,`图`,`二分查找`,`最小生成树` | 困难 | 第 456 场周赛 |
| 3601 | [Find Drivers with Improved Fuel Efficiency](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README.md) | | 中等 | |
| 3601 | [寻找燃油效率提升的驾驶员](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README.md) | | 中等 | |
## 版权