diff --git "a/leetcode/Study Plan/128. \346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/1.go" "b/leetcode/Study Plan/128. \346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/1.go" new file mode 100644 index 00000000..4ba928fd --- /dev/null +++ "b/leetcode/Study Plan/128. \346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/1.go" @@ -0,0 +1,33 @@ +func longestConsecutive(nums []int) int { + numset := make(map[int]bool) + for _, v := range nums { + numset[v] = true + } + ans := 0 + for x := range numset { + _, existsPrev := numset[x-1] + if existsPrev { + continue + } + currentNum := x + currentLength := 1 + for { + _, existsNext := numset[currentNum+1] + if !existsNext { + break + } + currentNum++ + currentLength++ + } + ans = max(ans, currentLength) + } + return ans +} + +// func max(a,b int)int{ +// if a > b{ +// return a +// }else{ +// return b +// } +// } diff --git "a/leetcode/Study Plan/3433. \347\273\237\350\256\241\347\224\250\346\210\267\350\242\253\346\217\220\345\217\212\346\203\205\345\206\265/1.go" "b/leetcode/Study Plan/3433. \347\273\237\350\256\241\347\224\250\346\210\267\350\242\253\346\217\220\345\217\212\346\203\205\345\206\265/1.go" new file mode 100644 index 00000000..ece1751a --- /dev/null +++ "b/leetcode/Study Plan/3433. \347\273\237\350\256\241\347\224\250\346\210\267\350\242\253\346\217\220\345\217\212\346\203\205\345\206\265/1.go" @@ -0,0 +1,54 @@ +func countMentions(numberOfUsers int, events [][]string) []int { + ans := make([]int, numberOfUsers) + // Event struct: timestamp, type (1=offline, -1=online, 2=HERE mention), user id + type event struct { + timestamp, type_, id int + } + es := []event{} + all := 0 + // Process raw events and separate them into individual event objects + for _, e := range events { + curT, _ := strconv.Atoi(e[1]) + mention := e[2] + if e[0] == "OFFLINE" { + // Create offline(1) and online(-1) events 60 seconds later + i, _ := strconv.Atoi(mention) + es = append(es, event{curT, 1, i}, event{curT + 60, -1, i}) + } else if mention == "ALL" { + all++ + } else if mention == "HERE" { + // Increment all user mentions for HERE events (process later) + all++ + es = append(es, event{curT, 2, -1}) + } else { + // Direct user mentions: add immediately + for _, s := range strings.Split(mention, " ") { + i, _ := strconv.Atoi(s[2:]) + ans[i]++ + } + } + } + + // Sort events by timestamp, then by type (process in correct order) + slices.SortFunc(es, func(a, b event) int { + return cmp.Or(a.timestamp-b.timestamp, a.type_-b.type_) + }) + + // Calculate HERE mentions: count online users at each HERE event + here := 0 + for _, e := range es { + if e.type_ == 2 { + // HERE mention: increment all currently online users by 'here' count + here++ + } else { + // OFFLINE/ONLINE event: update online status (1 removes, -1 adds) + ans[e.id] += e.type_ * here + } + } + + // Add ALL mentions to all users + for i := range ans { + ans[i] += all + } + return ans +} diff --git "a/leetcode/Study Plan/3433. \347\273\237\350\256\241\347\224\250\346\210\267\350\242\253\346\217\220\345\217\212\346\203\205\345\206\265/1.py" "b/leetcode/Study Plan/3433. \347\273\237\350\256\241\347\224\250\346\210\267\350\242\253\346\217\220\345\217\212\346\203\205\345\206\265/1.py" new file mode 100644 index 00000000..649eb5bb --- /dev/null +++ "b/leetcode/Study Plan/3433. \347\273\237\350\256\241\347\224\250\346\210\267\350\242\253\346\217\220\345\217\212\346\203\205\345\206\265/1.py" @@ -0,0 +1,30 @@ +class Solution: + def countMentions(self, numberOfUsers: int, events: List[List[str]]) -> List[int]: + # Sort events by timestamp, then by event type (to process in chronological order) + events.sort(key = lambda e:(int(e[1]),e[0][2])) + + # ans[i] stores the mention count for user i + ans = [0]*numberOfUsers + # online_t[i] stores when user i will be back online (0 means currently online) + online_t = [0]*numberOfUsers + + for type_,timestamp,mention in events: + curr_t = int(timestamp) + # Handle OFFLINE event: user goes offline for 60 seconds + if type_ == "OFFLINE": + online_t[int(mention)] = curr_t + 60 + # Handle ALL mentions: all users are mentioned + elif mention == "ALL": + for i in range(numberOfUsers): + ans[i] += 1 + # Handle HERE mentions: only currently online users are mentioned + elif mention == "HERE": + for i,t in enumerate(online_t): + if curr_t >= t: + ans[i] += 1 + # Handle individual user mentions (e.g., "id0 id1 id2") + else: + for s in mention.split(): + # Extract user ID from "id" format + ans[int(s[2:])] += 1 + return ans diff --git "a/leetcode/Study Plan/3606. \344\274\230\346\203\240\345\210\270\346\240\241\351\252\214\345\231\250/1.cpp" "b/leetcode/Study Plan/3606. \344\274\230\346\203\240\345\210\270\346\240\241\351\252\214\345\231\250/1.cpp" new file mode 100644 index 00000000..316fe2a9 --- /dev/null +++ "b/leetcode/Study Plan/3606. \344\274\230\346\203\240\345\210\270\346\240\241\351\252\214\345\231\250/1.cpp" @@ -0,0 +1,64 @@ +#include +#include +#include +using namespace std; +class Solution { +public: + vector validateCoupons(vector& code, vector& businessLine, vector& isActive) { + int n = code.size(); + // Store pairs of (businessLine priority, code) + vector> ans; + for(int i = 0; i < n; ++i) + { + if(isActive[i] && isValid(code[i], businessLine[i])) + { + int priority = getBusinessPriority(businessLine[i]); + ans.push_back({priority, code[i]}); + } + } + // Sort by priority first, then by code lexicographically + sort(ans.begin(), ans.end()); + + // Extract just the codes for the result + vector result; + for(auto &p : ans) + { + result.push_back(p.second); + } + return result; + } + +private: + vector vaild_business = { + "electronics", + "grocery", + "pharmacy", + "restaurant" + }; + + // Get priority of business line (0 = highest priority) + int getBusinessPriority(const string &b) + { + auto it = find(vaild_business.begin(), vaild_business.end(), b); + if(it == vaild_business.end()) return -1; + return it - vaild_business.begin(); + } + + bool isValid(const string &c, const string &b) + { + if(find(vaild_business.begin(), vaild_business.end(), b) == vaild_business.end()) + return false; + if(c == "") + return false; + for(auto ch : c) + { + bool isAlpha = (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'); + bool isDigit = ch >= '0' && ch <= '9'; + bool isUnderscore = ch == '_'; + + if(!isAlpha && !isDigit && !isUnderscore) + return false; + } + return true; + } +}; diff --git "a/niuke/practice/noob/noob69 \346\234\200\345\216\211\345\256\263\347\232\204\345\255\246\347\224\237/1.cpp" "b/niuke/practice/noob/noob69 \346\234\200\345\216\211\345\256\263\347\232\204\345\255\246\347\224\237/1.cpp" new file mode 100644 index 00000000..c5f846cd --- /dev/null +++ "b/niuke/practice/noob/noob69 \346\234\200\345\216\211\345\256\263\347\232\204\345\255\246\347\224\237/1.cpp" @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#define il inline +#define endl '\n' +using namespace std; + +#define pb push_back +#define fastio \ + ios::sync_with_stdio(false); \ + cin.tie(0); + +typedef long long ll; +typedef unsigned long long ull; + +const ll N = 5e5 + 5, mod = 1e9 + 7, inf = 2e18; +const double eps = 1e-9; +const double PI = 3.1415926; + +class Student { + public: + string name; + unordered_map scores = { + {"chinese", 0}, + {"math", 0}, + {"english", 0}, + }; + int total_score = 0; + + Student(string _name, int math, int chinese, int english) : name(_name) { + scores["chinese"] = chinese; + scores["math"] = math; + scores["english"] = english; + total_score = chinese + math + english; + } +}; + +auto cmp = [](const Student &a, const Student &b) { + return a.total_score < b.total_score; // Max heap: highest score on top +}; + +priority_queue, decltype(cmp)> pq(cmp); + +il void solve() { + string name; + int chinese, math, english; + cin >> name >> chinese >> math >> english; + Student stu(name, math, chinese, english); + pq.push(stu); + +} + +int main() { + fastio; + + int t = 1; + cin >> t; + + while (t--) { + solve(); + } + + if (!pq.empty()) { + Student top_student = pq.top(); + cout << top_student.name << " " << top_student.scores["chinese"] << " " << top_student.scores["math"] << " " + << top_student.scores["english"] << endl; + } + return 0; +} diff --git "a/niuke/practice/noob/noob70 \344\270\244\347\202\271\351\227\264\350\267\235\347\246\273/1.cpp" "b/niuke/practice/noob/noob70 \344\270\244\347\202\271\351\227\264\350\267\235\347\246\273/1.cpp" new file mode 100644 index 00000000..2eb008c6 --- /dev/null +++ "b/niuke/practice/noob/noob70 \344\270\244\347\202\271\351\227\264\350\267\235\347\246\273/1.cpp" @@ -0,0 +1,24 @@ +/** + * struct Point { + * int x; + * int y; + * Point(int xx, int yy) : x(xx), y(yy) {} + * }; + */ +class Solution { +public: + /** + * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 + * + * 计算A点与B点之间的距离 + * @param point_A Point类 A点 + * @param point_B Point类 B点 + * @return double浮点型 + */ + double calculateDistance(Point point_A, Point point_B) { + // write code here + double dx = static_cast(point_A.x - point_B.x); + double dy = static_cast(point_A.y - point_B.y); + return sqrt(dx * dx + dy * dy); + } +}; diff --git "a/niuke/practice/noob/noob71 \345\255\246\347\224\237\347\273\274\345\220\210\350\257\204\344\274\260\347\263\273\347\273\237/1.cpp" "b/niuke/practice/noob/noob71 \345\255\246\347\224\237\347\273\274\345\220\210\350\257\204\344\274\260\347\263\273\347\273\237/1.cpp" new file mode 100644 index 00000000..389c954e --- /dev/null +++ "b/niuke/practice/noob/noob71 \345\255\246\347\224\237\347\273\274\345\220\210\350\257\204\344\274\260\347\263\273\347\273\237/1.cpp" @@ -0,0 +1,225 @@ +#include +using namespace std; + +// 定义学生结构体 +struct Student{ + int id; + int academic_score; + int activity_score; +}; + +// 评估函数:判断学生是否优秀 +bool isExcellent(Student student){ + // TODO: 实现优秀标准的判断逻辑 + double overall_score = student.academic_score * 70 + student.activity_score * 30; + if(overall_score < 8000) + return false; + if(student.academic_score + student.activity_score <= 140.0) + return false; + return true; //true 代表学生优秀 +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +//主函数用于读入数据调用函数,请勿修改 +int main(){ + int n; + cin >> n; + Student student; + for(int i=1;i<=n;i++){ + cin >> student.id >> student.academic_score >> student.activity_score; + if (isExcellent(student)) cout << "Excellent\n"; + else cout << "Not excellent\n"; + } + return 0; +} diff --git "a/niuke/practice/noob/noob72 \347\202\271\345\210\260\347\233\264\347\272\277\350\267\235\347\246\273/1.cpp" "b/niuke/practice/noob/noob72 \347\202\271\345\210\260\347\233\264\347\272\277\350\267\235\347\246\273/1.cpp" new file mode 100644 index 00000000..058d7ff2 --- /dev/null +++ "b/niuke/practice/noob/noob72 \347\202\271\345\210\260\347\233\264\347\272\277\350\267\235\347\246\273/1.cpp" @@ -0,0 +1,45 @@ +#include +using namespace std; + +struct point { + double x, y; + point(double A, double B) { + x = A, y = B; + } + point() = default; +}; + +struct line { + point point_A, point_B; + line(point A, point B) { + point_A = A, point_B = B; + } + line() = default; +}; + +double getDistance(point P, line L) { + // 计算点P到直线L的距离 + // 使用向量叉积公式: distance = |cross product| / |line length| + // 这个方法适用于所有情况,包括垂直线和水平线 + double dx = L.point_B.x - L.point_A.x; + double dy = L.point_B.y - L.point_A.y; + + // 计算叉积的绝对值: |(B-A) × (A-P)| + double crossProduct = fabs(dx * (L.point_A.y - P.y) - (L.point_A.x - P.x) * dy); + + // 计算线段长度: |B-A| + double lineLength = sqrt(dx * dx + dy * dy); + + // 距离 = 叉积 / 线段长度 + double distance = crossProduct / lineLength; + return distance; +} + +int main() { + int a, b, sx, sy, tx, ty; + cin >> a >> b >> sx >> sy >> tx >> ty; + point A(sx, sy), B(tx, ty), C(a, b); + line L(A, B); + printf("%.2lf", getDistance(C, L)); + return 0; +}