freedom.9
Senior Member
Đi soi solution gặp mấy cách code này muốn đấm thậtBị đấm thì k biết đường đấm lại à,
Viết vậy còn dễ hiểu chán. Pythonista thì phải viết như vậy nhóe,![]()
![canny :canny: :canny:](https://data.voz.vn/styles/next/xenforo/smilies/popopo/canny.png?v=01)
via theNEXTvoz for iPhone
Đi soi solution gặp mấy cách code này muốn đấm thậtBị đấm thì k biết đường đấm lại à,
Viết vậy còn dễ hiểu chán. Pythonista thì phải viết như vậy nhóe,![]()
// Time: O(nlogn), space: O(1)
public long maximumHappinessSum(int[] happiness, int k) {
int n = happiness.length;
long sum = 0;
int count = 0;
Arrays.sort(happiness);
for (int i = n - 1; i >= n - k; i--) {
sum += Math.max(happiness[i] - count, 0);
count++;
}
return sum;
}
Đù đọc củng cố lại hay phếtTôi thấy có tài liệu này viết ngắn ngọn, đi trực tiếp vào vấn đề ae ạ.
Đọc lại để hệ thống kiến thức khá ok
http://www2.compute.dtu.dk/courses/02282/2021/nca/CPbook.pdf
cuốn này một trong những cuốn nhập môn của ae cày cp mà bácTôi thấy có tài liệu này viết ngắn ngọn, đi trực tiếp vào vấn đề ae ạ.
Đọc lại để hệ thống kiến thức khá ok
http://www2.compute.dtu.dk/courses/02282/2021/nca/CPbook.pdf
làm contest với blind testcase cảm giác còn phê hơn đó fenView attachment 2484655\
ngồi rảnh háng làm đc 1 bài medium, submit lần đầu TLE, sửa lại tí thì đúng sướng rùng mình![]()
cũng định chơi contesst mấy lần ấy mà cuối tuần lười quá là lười fen ạlàm contest với blind testcase cảm giác còn phê hơn đó fen
class Solution {
public:
vector<int> kthSmallestPrimeFraction(vector<int>& arr, int k) {
vector<tuple<float, int, int>> v;
for(int i=0;i<arr.size()-1;i++){
for(int j=i+1;j<arr.size();j++){
v.push_back(make_tuple((float)arr[i]/(float)arr[j],arr[i], arr[j]));
}
}
sort(v.begin(), v.end());
return {get<1>(v[k-1]),get<2>(v[k-1])};
}
};
public int[] kthSmallestPrimeFraction(int[] arr, int k) {
TreeMap<Double, int[]> minHeap = new TreeMap<>();
int len = arr.length;
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
minHeap.put((double) arr[i] / arr[j], new int[]{arr[i], arr[j]});
}
}
while (k > 1) {
minHeap.pollFirstEntry();
k--;
}
return minHeap.firstEntry().getValue();
}
struct cmp {
bool operator()( pair<int, int> a, pair<int, int> b){
return double(a.first)/a.second > double(b.first)/b.second;
}
};
vector<int> kthSmallestPrimeFraction(vector<int>& arr, int k) {
int n = arr.size();
priority_queue<pair<int, int>, vector<pair<int,int>>, cmp> q;
for(int i = 0; i < n; ++i) {
for(int j = i + 1; j < n; ++j) {
q.push({arr[i], arr[j]});
}
}
while(--k) q.pop();
auto r = q.top();
return {r.first, r.second};
}
vector<int> kthSmallestPrimeFraction(vector<int>& arr, int k) {
int n = arr.size();
priority_queue<pair<double, pair<int, int>>> q;
for(int i = 0; i < n; ++i) {
for(int j = i + 1; j < n; ++j) {
double d = -1* double(arr[i])/arr[j];
q.push({d, {arr[i], arr[j]}});
}
}
while(--k) q.pop();
auto r = q.top().second;
return {r.first, r.second};
}
var kthSmallestPrimeFraction = function(arr, k) {
let fractions = [];
for (let i = 0; i < arr.length - 1; i++) {
for (let j = i + 1; j < arr.length; j++) {
fractions.push([arr[i]/arr[j], arr[i], arr[j]]);
}
}
fractions.sort((a, b) => a[0] - b[0]);
return [fractions[k-1][1], fractions[k-1][2]];
};
function kthSmallestPrimeFraction(arr: number[], k: number): number[] {
const pq = new MinPriorityQueue();
const j = arr.length - 1;
for (let i = 0; i < j; i++) {
pq.enqueue([i, j], arr[i] / arr[j]);
}
for (let i = 0; i < k - 1; i++) {
const [f,s] = pq.dequeue()!.element;
pq.enqueue([f, s - 1], arr[f] / arr[s - 1]);
}
const [f,s] = pq.dequeue()!.element;
return [arr[f], arr[s]];
}
class Solution {
public int[] kthSmallestPrimeFraction(int[] arr, int k) {
int[] res = new int[2];
double minK = 0;
List<int[]> lists = new ArrayList<>();
PriorityQueue<Double> priorityQueue = new PriorityQueue<>();
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
double result = arr[i] / (double) arr[j];
priorityQueue.offer(result);
lists.add(new int[] { arr[i], arr[j] });
}
}
while (k > 0) {
minK = priorityQueue.poll();
k--;
}
for (int[] a : lists) {
if (minK == (a[0] / (double) a[1])) {
res = a;
}
}
return res;
}
}