-
Notifications
You must be signed in to change notification settings - Fork 5
update: 添加问题“3346.执行操作后元素的最高频率I”的代码和题解 #1188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8cdca5e
codes: huawei笔试
LetMeFly666 ecc18a2
refactor: codes should in Codes
LetMeFly666 28e9b4f
feat: 毕设月报+京东笔试
LetMeFly666 399f887
Merge remote-tracking branch 'origin/dev-2' into dev
LetMeFly666 6b5457d
feat: debug cpp on mac M-series using VsCode internal
LetMeFly666 cb84494
word: en+jp today
LetMeFly666 0e471d2
3346: WA.cpp (#1179)
LetMeFly666 56a3cf2
merge: Merge branch 'dev' into 3346 (#1179)
LetMeFly666 847a284
3346: WA.cpp (#1179) - target不能只nums[i]
LetMeFly666 97f662d
3346: AC.cpp+py+go+java | CE.rust (#1179)
LetMeFly666 e20f21c
3346: AC.rust (#1179) - AC,75.63%,42.86%
LetMeFly666 b7c9af7
update: 添加问题“3346.执行操作后元素的最高频率I”的代码和题解 (#1188)
LetMeFly666 5a5ad1b
Address review comments: fix duplicate text and typo (#1190)
Copilot 00d6bae
docs: record (#1188)
LetMeFly666 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
Codes/3346-maximum-frequency-of-an-element-after-performing-operations-i.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * @Author: LetMeFly | ||
| * @Date: 2025-10-21 18:51:21 | ||
| * @LastEditors: LetMeFly.xyz | ||
| * @LastEditTime: 2025-10-30 22:53:20 | ||
| */ | ||
| #if defined(_WIN32) || defined(__APPLE__) | ||
| #include "_[1,2]toVector.h" | ||
| #endif | ||
|
|
||
| class Solution { | ||
| public: | ||
| int maxFrequency(vector<int>& nums, int k, int numOperations) { | ||
| sort(nums.begin(), nums.end()); | ||
| unordered_map<int, int> frequency; | ||
| for (int t : nums) { | ||
| frequency[t]++; | ||
| } | ||
| int ans = 0; | ||
| for (int l = 0, r = 0, target = nums[0]; target <= nums.back(); target++) { | ||
| while (target - nums[l] > k) { | ||
| l++; | ||
| } | ||
| while (r < nums.size() && nums[r] - target <= k) { | ||
| r++; | ||
| } | ||
| ans = max(ans, min(r - l, numOperations + frequency[target])); | ||
| } | ||
| return ans; | ||
| } | ||
| }; | ||
28 changes: 28 additions & 0 deletions
28
Codes/3346-maximum-frequency-of-an-element-after-performing-operations-i.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * @Author: LetMeFly | ||
| * @Date: 2025-10-30 22:39:36 | ||
| * @LastEditors: LetMeFly.xyz | ||
| * @LastEditTime: 2025-10-30 23:00:59 | ||
| */ | ||
| package main | ||
|
|
||
| import "sort" | ||
|
|
||
| func maxFrequency(nums []int, k int, numOperations int) (ans int) { | ||
| sort.Ints(nums) | ||
| frequency := map[int]int{} | ||
| for _, t := range nums { | ||
| frequency[t]++ | ||
| } | ||
| n := len(nums) | ||
| for l, r, target := 0, 0, nums[0]; target <= nums[n - 1]; target++ { | ||
| for target - nums[l] > k { | ||
| l++ | ||
| } | ||
| for r < n && nums[r] - target <= k { | ||
| r++ | ||
| } | ||
| ans = max(ans, min(r - l, numOperations + frequency[target])) | ||
| } | ||
| return | ||
| } |
30 changes: 30 additions & 0 deletions
30
Codes/3346-maximum-frequency-of-an-element-after-performing-operations-i.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * @Author: LetMeFly | ||
| * @Date: 2025-10-30 22:39:36 | ||
| * @LastEditors: LetMeFly.xyz | ||
| * @LastEditTime: 2025-10-30 23:07:34 | ||
| */ | ||
| import java.util.Map; | ||
| import java.util.HashMap; | ||
| import java.util.Arrays; | ||
|
|
||
| class Solution { | ||
| public int maxFrequency(int[] nums, int k, int numOperations) { | ||
| Arrays.sort(nums); | ||
| Map<Integer, Integer> frequency = new HashMap<>(); | ||
| for (int t : nums) { | ||
| frequency.merge(t, 1, Integer::sum); // 这个api挺容易忘的 | ||
| } | ||
| int ans = 0, n = nums.length; | ||
| for (int l = 0, r = 0, target = nums[0]; target <= nums[n - 1]; target++) { | ||
| while (target - nums[l] > k) { | ||
| l++; | ||
| } | ||
| while (r < n && nums[r] - target <= k) { | ||
| r++; | ||
| } | ||
| ans = Math.max(ans, Math.min(r - l, numOperations + frequency.getOrDefault(target, 0))); | ||
| } | ||
| return ans; | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
Codes/3346-maximum-frequency-of-an-element-after-performing-operations-i.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| ''' | ||
| Author: LetMeFly | ||
| Date: 2025-10-30 22:39:36 | ||
| LastEditors: LetMeFly.xyz | ||
| LastEditTime: 2025-10-30 22:57:07 | ||
| ''' | ||
| from typing import List | ||
| from collections import Counter | ||
|
|
||
| class Solution: | ||
| def maxFrequency(self, nums: List[int], k: int, numOperations: int) -> int: | ||
| nums.sort() | ||
| frequency = Counter(nums) | ||
| l = r = 0 | ||
| ans = 0 | ||
| for target in range(nums[0], nums[-1] + 1): | ||
| while target - nums[l] > k: | ||
| l += 1 | ||
| while r < len(nums) and nums[r] - target <= k: | ||
| r += 1 | ||
| ans = max(ans, min(r - l, numOperations + frequency[target])) | ||
| return ans # 刚刚差点忘记return | ||
|
LetMeFly666 marked this conversation as resolved.
|
||
31 changes: 31 additions & 0 deletions
31
Codes/3346-maximum-frequency-of-an-element-after-performing-operations-i.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * @Author: LetMeFly | ||
| * @Date: 2025-10-30 22:39:36 | ||
| * @LastEditors: LetMeFly.xyz | ||
| * @LastEditTime: 2025-10-30 23:19:32 | ||
| */ | ||
| use std::collections::HashMap; | ||
| // 这台机器上没有安装过rust,故无IDE语法检查了 | ||
|
LetMeFly666 marked this conversation as resolved.
|
||
| impl Solution { | ||
| pub fn max_frequency(mut nums: Vec<i32>, k: i32, num_operations: i32) -> i32 { | ||
| nums.sort(); | ||
| let mut frequency = HashMap::new(); | ||
| for &t in &nums { | ||
| *frequency.entry(t).or_insert(0) += 1; // 不存在时默认值为0 | ||
| } | ||
|
|
||
| let mut ans: i32 = 0; | ||
| let mut l: usize = 0; | ||
| let mut r: usize = 0; | ||
| for target in nums[0]..=nums[nums.len()-1] { | ||
| while target - nums[l] > k { | ||
| l += 1; | ||
| } | ||
| while r < nums.len() && nums[r] - target <= k { | ||
| r += 1; | ||
| } | ||
| ans = ans.max(((r - l) as i32).min(num_operations + *frequency.get(&target).unwrap_or(&0))); | ||
| } | ||
| ans | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| ''' | ||
| Author: LetMeFly | ||
| Date: 2025-10-29 19:30:11 | ||
| LastEditors: LetMeFly.xyz | ||
| LastEditTime: 2025-10-29 20:04:49 | ||
| ''' | ||
| """ | ||
| 6 10 | ||
| 20 10 -1 | ||
| 10 21 -1 | ||
| 21 -1 11 | ||
| 11 -1 22 | ||
| 22 12 -1 | ||
| 12 -1 -1 | ||
| """ | ||
| from collections import defaultdict | ||
|
|
||
| class Node: | ||
| def __init__(self, val: int): | ||
| self.val = val | ||
| self.left = None | ||
| self.right = None | ||
| self.isRoot = True | ||
|
|
||
| ma = defaultdict(Node) | ||
|
|
||
| def getNode(val: int) -> Node: | ||
| if val == -1: | ||
| return None | ||
| if val in ma: | ||
| return ma[val] | ||
| ans = Node(val) | ||
| ma[val] = ans | ||
| return ans | ||
|
|
||
| # 其实这里返回Node也可以 | ||
| def getRoot() -> int: | ||
|
LetMeFly666 marked this conversation as resolved.
|
||
| for val, node in ma.items(): | ||
| # print(val, node) | ||
| if node.isRoot: | ||
| return val | ||
|
|
||
| n, k = map(int, input().split()) | ||
|
|
||
| for _ in range(n): | ||
| root, l, r = map(getNode, map(int, input().split())) | ||
| # print(root.val, l.val, r.val) | ||
| root.left = l | ||
| root.right = r | ||
| if l: | ||
| l.isRoot = False | ||
| if r: | ||
| r.isRoot = False | ||
|
|
||
| root = getRoot() | ||
| # print(root) | ||
|
|
||
| ans = 0 | ||
|
|
||
| """ | ||
| status: | ||
| 0 无状态 | ||
| 1 刚递增过 | ||
| 2 刚递减过 | ||
| """ | ||
| def dfs(root: Node, cnt: int, parent: Node, status: int): | ||
| global ans | ||
| ans = max(ans, cnt) | ||
| if not root: | ||
| return | ||
|
|
||
| nextStatus = 0 | ||
| if status == 1 and parent.val - root.val >= k: | ||
| cnt += 1 | ||
| nextStatus = 2 | ||
| if status == 2 and root.val - parent.val >= k: | ||
| cnt += 1 | ||
| nextStatus = 1 | ||
| if not nextStatus: | ||
| cnt = 1 | ||
| if parent and parent.val - root.val >= k: | ||
| cnt += 1 | ||
| nextStatus = 2 | ||
| if parent and root.val - parent.val >= k: | ||
| cnt += 1 | ||
| nextStatus = 1 | ||
| dfs(root.left, cnt, root, nextStatus) | ||
| dfs(root.right, cnt, root, nextStatus) | ||
|
|
||
|
|
||
| dfs(ma[root], 0, None, 0) | ||
|
|
||
| print(ans) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| ''' | ||
| Author: LetMeFly | ||
| Date: 2025-10-29 20:05:25 | ||
| LastEditors: LetMeFly.xyz | ||
| LastEditTime: 2025-10-29 20:59:46 | ||
| ''' | ||
| """ | ||
| dp[i][j]:重量i价值j最少车数 | ||
|
|
||
|
|
||
| 5 | ||
| 80 | ||
| 30 45 15 15 80 | ||
| 400 470 200 200 870 | ||
| """ | ||
|
|
||
| n = int(input()) | ||
| k = int(input()) | ||
| weights = list(map(int, input().split())) | ||
| values = list(map(int, input().split())) | ||
|
|
||
| dp = [[0] * (n * 1000) for _ in range(n)] | ||
| maxVal = 0 | ||
| ans = [] | ||
| status = 1 << n | ||
| for i in range(status): | ||
| thisW = thisV = 0 | ||
| thisChoosen = [] | ||
| for j in range(n): | ||
| if i & (1 << j): | ||
| thisW += weights[j] | ||
| thisV += values[j] | ||
| thisChoosen.append(j) | ||
| if thisW > k: | ||
| break | ||
| if thisW > k: | ||
| break | ||
| if thisW <= k: | ||
| if thisV > maxVal or (thisV == maxVal and len(thisChoosen) < len(ans)): | ||
| # if thisV > maxVal: | ||
| ans = thisChoosen | ||
| maxV = thisV | ||
|
LetMeFly666 marked this conversation as resolved.
LetMeFly666 marked this conversation as resolved.
|
||
|
|
||
| if not len(ans): | ||
| print(-1) | ||
| else: | ||
| ans.sort() | ||
| for i, v in enumerate(ans): | ||
| # print(v, end=' ') | ||
| if i: | ||
| print(' ', end='') | ||
| print(v, end='') | ||
|
|
||
|
|
||
| # """ | ||
| # dfs(i) | ||
| # """ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| ''' | ||
| Author: LetMeFly | ||
| Date: 2025-10-29 20:35:05 | ||
| LastEditors: LetMeFly.xyz | ||
| LastEditTime: 2025-10-29 20:49:47 | ||
| ''' | ||
| """ | ||
| 10 | ||
| 448 0 112 0 0 0 28 260 3 0 | ||
|
|
||
|
|
||
|
|
||
| n(最多10^6)个非负整数(0到10^18),两个数之间与不为0则有边,求最短环(长度不小于3)的长度,若无输出-1 | ||
| """ | ||
|
|
||
| import random | ||
| random.seed(9333) | ||
| print(random.choice([-1, 3, 4])) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.