Skip to content
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

10-InSange #40

Merged
merged 4 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions InSange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
| 5μ°¨μ‹œ | 2024.03.27 | DP | [RGB거리2](https://www.acmicpc.net/problem/17404) | [#5](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/19)]
| 6μ°¨μ‹œ | 2024.04.01 | μ΅œμ†Œμ‹ μž₯트리 | [ν–‰μ„± 터널](https://www.acmicpc.net/problem/2887) | [#6](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/24)]
| 7μ°¨μ‹œ | 2024.04.04 | μŠ€νƒ | [λ¬Έμžμ—΄ 폭발](https://www.acmicpc.net/problem/9935) | [#7](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/28)]
| 8μ°¨μ‹œ | 2024.04.09 | 투 포인터 | [두 μš©μ•‘](https://www.acmicpc.net/problem/2470) | [#8](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/32)]
| 9μ°¨μ‹œ | 2024.04.12 | νž™ | [Top K Frequent Words](https://leetcode.com/submissions/detail/1180988760/) | [#9](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/28)]
| 10μ°¨μ‹œ | 2024.05.02 | μŠ€νƒ | [μ˜€μ•„μ‹œμŠ€ μž¬κ²°ν•©](https://www.acmicpc.net/problem/3015) | [#10](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/40)]
=======
---
62 changes: 62 additions & 0 deletions InSange/μŠ€νƒ/3015.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <iostream>
#include <vector>
#include <stack>

using namespace std;

long long N, n, a, b, answer;
vector<int> line;
stack<pair<int, int>> st;

// a1 οΏ½οΏ½οΏ½οΏ½ bοΏ½οΏ½ Ε©οΏ½οΏ½ οΏ½οΏ½οΏ½οΏ½
// a1 οΏ½οΏ½οΏ½οΏ½ bοΏ½οΏ½ οΏ½Ϋ΄ΩΈοΏ½ οΏ½οΏ½οΏ½οΏ½ bοΏ½οΏ½
void Solve()
{
cin >> N;

for (int i = 0; i < N; i++)
{
cin >> n;
line.push_back(n);
}

for (auto num : line)
{
int cnt = 1;

while (!st.empty())
{
int cur = st.top().first;
int cur_cnt = st.top().second;
if (cur < num)
{
answer+= cur_cnt;
}
else if (cur == num)
{
answer += cur_cnt;
cnt += cur_cnt;
}
else //if(st.top().first > num)
{
answer++;
break;
}
st.pop();
}
//cout << num << ", " << answer << "\n";
st.push({num, cnt});
}

cout << answer;
}

int main()
{
cin.tie(nullptr);
ios::sync_with_stdio(false);

Solve();

return 0;
}
96 changes: 96 additions & 0 deletions InSange/νˆ¬ν¬μΈν„°/2470.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MAX_VAL = 1999999999;

int N, num, minVal, aanswer, banswer;
vector<int> acid; // οΏ½κΌΊ
vector<int> basic; // οΏ½οΏ½οΏ½βΌΊ

bool cmp(int a, int b)
{
return a > b;
}

void Input()
{
cin >> N;

for (int i = 0; i < N; i++)
{
cin >> num;
(num > 0) ? acid.push_back(num) : basic.push_back(num);
}

sort(acid.begin(), acid.end());
sort(basic.begin(), basic.end(), cmp);

minVal = MAX_VAL;
}

void Solve()
{
int a_index = 0;
int b_index = 0;
while (a_index < acid.size() && b_index < basic.size())
{
int anum, bnum;
anum = acid[a_index];
bnum = basic[b_index];

if (abs(anum + bnum) < minVal)
{
aanswer = anum;
banswer = bnum;
minVal = abs(anum + bnum);
}

(abs(anum) > abs(bnum)) ? b_index++ : a_index++;
}

int sum;
if (acid.size() > 1)
{
sum = abs(acid[0] + acid[1]);
if (sum < minVal)
{
aanswer = acid[1];
banswer = acid[0];
}
}
if (basic.size() > 1)
{
sum = abs(basic[0] + basic[1]);
if (sum < minVal)
{
aanswer = basic[0];
banswer = basic[1];
}
}
if (acid.empty())
{
aanswer = basic[0];
banswer = basic[1];
}
if (basic.empty())
{
aanswer = acid[1];
banswer = acid[0];
}

cout << banswer << " " << aanswer;
}

int main()
{
cin.tie(nullptr);
ios::sync_with_stdio(false);

Input();
Solve();

return 0;
}
34 changes: 34 additions & 0 deletions InSange/νž™/Top K Frequent Words.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>

using namespace std;

class Solution {
public:
struct compare {
bool operator()(pair<int, string>& a, pair<int, string>& b)
{
if (a.first == b.first) return a.second > b.second;
return a.first < b.first;
}
};

vector<string> topKFrequent(vector<string>& words, int k) {
unordered_map<string, int> um;
priority_queue<pair<int, string>, vector<pair<int, string>>, compare> pq;
vector<string> ans;

for (auto s : words) um[s]++;
for (pair<string, int> v : um) pq.push({ v.second, v.first });

while (k--)
{
ans.push_back(pq.top().second);
pq.pop();
}

return ans;
}
};