Skip to content

Commit 334d551

Browse files
committed
test: [20251104] Add (3318)
1 parent d71890e commit 334d551

File tree

15 files changed

+274
-5
lines changed

15 files changed

+274
-5
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ members = [
368368
"problems/problems_3217",
369369
"problems/problems_2257",
370370
"problems/problems_1578",
371+
"problems/problems_3318",
371372
]
372373

373374
[package]
@@ -758,3 +759,4 @@ solution_3289 = { path = "problems/problems_3289", features = ["solution_3289"]
758759
solution_3217 = { path = "problems/problems_3217", features = ["solution_3217"] }
759760
solution_2257 = { path = "problems/problems_2257", features = ["solution_2257"] }
760761
solution_1578 = { path = "problems/problems_1578", features = ["solution_1578"] }
762+
solution_3318 = { path = "problems/problems_3318", features = ["solution_3318"] }

daily-problems.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"daily": "1578",
2+
"daily": "3318",
33
"plans": ["3683", "problems", "3684", "problems", "3685", "problems", "3686", "problems"]
44
}

golang/solution_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package golang
22

33
import (
4-
problem "leetCode/problems/problems_1578"
4+
problem "leetCode/problems/problems_3318"
55
"testing"
66
)
77

88
func TestSolution(t *testing.T) {
9-
TestEach(t, "1578", "problems", problem.Solve)
9+
TestEach(t, "3318", "problems", problem.Solve)
1010
}

problems/problems_3318/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "solution_3318"
3+
version = "0.1.0"
4+
edition = "2021"
5+
rust-version = "1.79.0"
6+
authors = ["benhao"]
7+
description = "LeetCode Solution 3318 in Rust"
8+
readme = "../../README.md"
9+
10+
[features]
11+
solution_3318 = []
12+
13+
[dependencies]
14+
serde_json = "1.0"
15+
rand = "0.8.4"
16+
regex = "1.10.5"
17+
library = { path = "../../rust/library", features = ["model"] }
18+
19+
[lib]
20+
name = "solution_3318"
21+
path = "solution.rs"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//go:build ignore
2+
#include "cpp/common/Solution.h"
3+
4+
5+
using namespace std;
6+
using json = nlohmann::json;
7+
8+
class Solution {
9+
public:
10+
vector<int> findXSum(vector<int>& nums, int k, int x) {
11+
12+
}
13+
};
14+
15+
json leetcode::qubh::Solve(string input_json_values) {
16+
vector<string> inputArray;
17+
size_t pos = input_json_values.find('\n');
18+
while (pos != string::npos) {
19+
inputArray.push_back(input_json_values.substr(0, pos));
20+
input_json_values = input_json_values.substr(pos + 1);
21+
pos = input_json_values.find('\n');
22+
}
23+
inputArray.push_back(input_json_values);
24+
25+
Solution solution;
26+
vector<int> nums = json::parse(inputArray.at(0));
27+
int k = json::parse(inputArray.at(1));
28+
int x = json::parse(inputArray.at(2));
29+
return solution.findXSum(nums, k, x);
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package problems.problems_3318;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import java.util.*;
5+
import qubhjava.BaseSolution;
6+
7+
8+
public class Solution extends BaseSolution {
9+
public int[] findXSum(int[] nums, int k, int x) {
10+
11+
}
12+
13+
@Override
14+
public Object solve(String[] inputJsonValues) {
15+
int[] nums = jsonArrayToIntArray(inputJsonValues[0]);
16+
int k = Integer.parseInt(inputJsonValues[1]);
17+
int x = Integer.parseInt(inputJsonValues[2]);
18+
return JSON.toJSON(findXSum(nums, k, x));
19+
}
20+
}

problems/problems_3318/problem.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 3318. Find X-Sum of All K-Long Subarrays I [Rating: 1457.51]
2+
3+
<p>You are given an array <code>nums</code> of <code>n</code> integers and two integers <code>k</code> and <code>x</code>.</p>
4+
5+
<p>The <strong>x-sum</strong> of an array is calculated by the following procedure:</p>
6+
7+
<ul>
8+
<li>Count the occurrences of all elements in the array.</li>
9+
<li>Keep only the occurrences of the top <code>x</code> most frequent elements. If two elements have the same number of occurrences, the element with the <strong>bigger</strong> value is considered more frequent.</li>
10+
<li>Calculate the sum of the resulting array.</li>
11+
</ul>
12+
13+
<p><strong>Note</strong> that if an array has less than <code>x</code> distinct elements, its <strong>x-sum</strong> is the sum of the array.</p>
14+
15+
<p>Return an integer array <code>answer</code> of length <code>n - k + 1</code> where <code>answer[i]</code> is the <strong>x-sum</strong> of the <span data-keyword="subarray-nonempty">subarray</span> <code>nums[i..i + k - 1]</code>.</p>
16+
17+
<p>&nbsp;</p>
18+
<p><strong class="example">Example 1:</strong></p>
19+
20+
<div class="example-block">
21+
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,2,2,3,4,2,3], k = 6, x = 2</span></p>
22+
23+
<p><strong>Output:</strong> <span class="example-io">[6,10,12]</span></p>
24+
25+
<p><strong>Explanation:</strong></p>
26+
27+
<ul>
28+
<li>For subarray <code>[1, 1, 2, 2, 3, 4]</code>, only elements 1 and 2 will be kept in the resulting array. Hence, <code>answer[0] = 1 + 1 + 2 + 2</code>.</li>
29+
<li>For subarray <code>[1, 2, 2, 3, 4, 2]</code>, only elements 2 and 4 will be kept in the resulting array. Hence, <code>answer[1] = 2 + 2 + 2 + 4</code>. Note that 4 is kept in the array since it is bigger than 3 and 1 which occur the same number of times.</li>
30+
<li>For subarray <code>[2, 2, 3, 4, 2, 3]</code>, only elements 2 and 3 are kept in the resulting array. Hence, <code>answer[2] = 2 + 2 + 2 + 3 + 3</code>.</li>
31+
</ul>
32+
</div>
33+
34+
<p><strong class="example">Example 2:</strong></p>
35+
36+
<div class="example-block">
37+
<p><strong>Input:</strong> <span class="example-io">nums = [3,8,7,8,7,5], k = 2, x = 2</span></p>
38+
39+
<p><strong>Output:</strong> <span class="example-io">[11,15,15,15,12]</span></p>
40+
41+
<p><strong>Explanation:</strong></p>
42+
43+
<p>Since <code>k == x</code>, <code>answer[i]</code> is equal to the sum of the subarray <code>nums[i..i + k - 1]</code>.</p>
44+
</div>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= n == nums.length &lt;= 50</code></li>
51+
<li><code>1 &lt;= nums[i] &lt;= 50</code></li>
52+
<li><code>1 &lt;= x &lt;= k &lt;= nums.length</code></li>
53+
</ul>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# 3318. 计算子数组的 x-sum I [难度分: 1457.51]
2+
3+
<p>给你一个由 <code>n</code> 个整数组成的数组 <code>nums</code>,以及两个整数 <code>k</code> 和 <code>x</code>。</p>
4+
5+
<p>数组的 <strong>x-sum</strong> 计算按照以下步骤进行:</p>
6+
7+
<ul>
8+
<li>统计数组中所有元素的出现次数。</li>
9+
<li>仅保留出现次数最多的前 <code>x</code> 个元素的每次出现。如果两个元素的出现次数相同,则数值<strong> 较大 </strong>的元素被认为出现次数更多。</li>
10+
<li>计算结果数组的和。</li>
11+
</ul>
12+
13+
<p><strong>注意</strong>,如果数组中的不同元素少于 <code>x</code> 个,则其 <strong>x-sum</strong> 是数组的元素总和。</p>
14+
15+
<p>返回一个长度为 <code>n - k + 1</code> 的整数数组 <code>answer</code>,其中 <code>answer[i]</code> 是 <span data-keyword="subarray-nonempty">子数组</span> <code>nums[i..i + k - 1]</code> 的 <strong>x-sum</strong>。</p>
16+
17+
<p><strong>子数组</strong> 是数组内的一个连续<b> 非空</b> 的元素序列。</p>
18+
19+
<p>&nbsp;</p>
20+
21+
<p><strong class="example">示例 1:</strong></p>
22+
23+
<div class="example-block">
24+
<p><strong>输入:</strong><span class="example-io">nums = [1,1,2,2,3,4,2,3], k = 6, x = 2</span></p>
25+
26+
<p><strong>输出:</strong><span class="example-io">[6,10,12]</span></p>
27+
28+
<p><strong>解释:</strong></p>
29+
30+
<ul>
31+
<li>对于子数组 <code>[1, 1, 2, 2, 3, 4]</code>,只保留元素 1 和 2。因此,<code>answer[0] = 1 + 1 + 2 + 2</code>。</li>
32+
<li>对于子数组 <code>[1, 2, 2, 3, 4, 2]</code>,只保留元素 2 和 4。因此,<code>answer[1] = 2 + 2 + 2 + 4</code>。注意 4 被保留是因为其数值大于出现其他出现次数相同的元素(3 和 1)。</li>
33+
<li>对于子数组 <code>[2, 2, 3, 4, 2, 3]</code>,只保留元素 2 和 3。因此,<code>answer[2] = 2 + 2 + 2 + 3 + 3</code>。</li>
34+
</ul>
35+
</div>
36+
37+
<p><strong class="example">示例 2:</strong></p>
38+
39+
<div class="example-block">
40+
<p><strong>输入:</strong><span class="example-io">nums = [3,8,7,8,7,5], k = 2, x = 2</span></p>
41+
42+
<p><strong>输出:</strong><span class="example-io">[11,15,15,15,12]</span></p>
43+
44+
<p><strong>解释:</strong></p>
45+
46+
<p>由于 <code>k == x</code>,<code>answer[i]</code> 等于子数组 <code>nums[i..i + k - 1]</code> 的总和。</p>
47+
</div>
48+
49+
<p>&nbsp;</p>
50+
51+
<p><strong>提示:</strong></p>
52+
53+
<ul>
54+
<li><code>1 &lt;= n == nums.length &lt;= 50</code></li>
55+
<li><code>1 &lt;= nums[i] &lt;= 50</code></li>
56+
<li><code>1 &lt;= x &lt;= k &lt;= nums.length</code></li>
57+
</ul>

problems/problems_3318/solution.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package problem3318
2+
3+
import (
4+
"encoding/json"
5+
"log"
6+
"strings"
7+
)
8+
9+
func findXSum(nums []int, k int, x int) []int {
10+
11+
}
12+
13+
func Solve(inputJsonValues string) any {
14+
inputValues := strings.Split(inputJsonValues, "\n")
15+
var nums []int
16+
var k int
17+
var x int
18+
19+
if err := json.Unmarshal([]byte(inputValues[0]), &nums); err != nil {
20+
log.Fatal(err)
21+
}
22+
if err := json.Unmarshal([]byte(inputValues[1]), &k); err != nil {
23+
log.Fatal(err)
24+
}
25+
if err := json.Unmarshal([]byte(inputValues[2]), &x); err != nil {
26+
log.Fatal(err)
27+
}
28+
29+
return findXSum(nums, k, x)
30+
}

problems/problems_3318/solution.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import solution
2+
from typing import *
3+
4+
5+
class Solution(solution.Solution):
6+
def solve(self, test_input=None):
7+
return self.findXSum(*test_input)
8+
9+
def findXSum(self, nums: List[int], k: int, x: int) -> List[int]:
10+
pass
11+

0 commit comments

Comments
 (0)