728x90
1. 실패율
https://school.programmers.co.kr/learn/courses/30/lessons/42889
#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<double,int> p1, pair<double,int> p2){
if(p1.first!=p2.first) return p1.first>p2.first;
else return p1.second<p2.second;
}
vector<int> solution(int N, vector<int> stages) {
vector<int> answer;
//1. 스테이지별 도달한 사람수
map<int,int> mp;
for(int i=1;i<=N;i++){
mp[i]=0;
}
for(int s:stages){
for(int i=1;i<=s;i++){
if(i>N);
else mp[i]++;
}
}
//2. 도달했으나 클리어X
map<int,int> mp2;
for(int i=1;i<=N;i++){
mp2[i]=0;
}
for(int s:stages){
if(s>N);
else mp2[s]++;
}
//3. 실패율 저장
vector<pair<double,int>> fr;
for(int i=1;i<=N;i++){
if(mp[i]==0) {
fr.push_back({0,i});
}
// else cout<<(double)mp2[i] <<" "<<mp[i]<<"\n";
else fr.push_back({(double)mp2[i]/mp[i],i});
}
sort(fr.begin(), fr.end(),cmp);
//5. 정답 저장
for(auto it:fr){
answer.push_back(it.second);
}
//cout<<it.first << " "<<it.second<<"\n";
return answer;
}
2. 다트 게임
시간을 너무 많이썼다...
코드
#include <bits/stdc++.h>
using namespace std;
int solution(string dartResult) {
int answer = 0;
int ret=0; int num=0; int buf=0; int sum=0;
for(int i=0;i<dartResult.size();i++){
char d=dartResult[i];
if('0'<=d && d<='9') {
if(d=='1' && dartResult[i+1]=='0'){
num=10; i++;
}
else num=d-'0';
}
else if(d=='S'){
;
}
else if(d=='D'){
num=pow(num,2);
}
else if(d=='T'){
num=pow(num,3);
}
else if(d=='*'){
num*=2;
sum+=num+buf;
buf=num;
}
else if(d=='#'){
num*=(-1);
sum+=num;
buf=num;
}
if(d=='S'||d=='D'||d=='T'){
if('0'<=dartResult[i+1] && dartResult[i+1]<='9'){
sum+=num;
buf=num;
}
}
}
if(dartResult[dartResult.size()-1]!='*' && dartResult[dartResult.size()-1]!='#') sum+=num;
return sum;
}
더 좋은 코드
#include <bits/stdc++.h>
using namespace std;
int score[4];
int solution(string dartResult) {
int answer = 0; int pos=0;
for(int i=0;i<dartResult.size();i++){
char c=dartResult[i];
if('0'<=c && c<='9'){ //숫자일때
if(score[pos]==0){ //0~9
score[pos]=c-'0';
} //10일때
else score[pos]=10;
}
else{
if(c=='S'){
pos++;
}
else if(c=='D'){
score[pos++]=score[pos]*score[pos];
}
else if(c=='T'){
score[pos++]=score[pos]*score[pos]*score[pos];
}
else if(c=='*'){
score[pos-1]*=2;
if(pos-2<0);
else score[pos-2]*=2;
}
else if(c=='#'){
score[pos-1]*=-1;
}
}
}
for(int i=0;i<3;i++){
cout<<score[i]<<" ";
answer+=score[i];
}
return answer;
}
더 좋은 코드
로또의 최고 순위와 최저 순위
코드
#include <bits/stdc++.h>
using namespace std;
vector<int> solution(vector<int> lottos, vector<int> win_nums) {
vector<int> answer;
sort(lottos.begin(), lottos.end());
sort(win_nums.begin(), win_nums.end());
int znum=0; int mnum=0;
for(int i=0;i<lottos.size();i++){
if(lottos[i]==0){
znum++; continue;
}
for(int j=0;j<win_nums.size();j++){
if(lottos[i]==win_nums[j]){
mnum++;
}
else if(lottos[i]<win_nums[j]) break;
}
}
int mx=znum+mnum;
if(mx<=1) answer.push_back(6);
else answer.push_back(7-mx);
int mn=mnum;
if(mn<=1) answer.push_back(6);
else answer.push_back(7-mn);
// cout<<mnum <<" "<<znum<<"\n";
return answer;
}
더 빠른 풀이
#include <bits/stdc++.h>
using namespace std;
vector<string> solution(int n, vector<int> arr1, vector<int> arr2) {
vector<string> answer;
for(int i=0;i<n;i++){
arr1[i]=arr1[i]|arr2[i];
string str="";
for(int j=0;j<n;j++){
if(arr1[i]%2==0) str=' '+str; //0이면
else str='#'+str;
arr1[i]=arr1[i]>>1;
}
answer.push_back(str);
}
return answer;
}
비트 마스킹으로 한번에..대박쓰
소수 만들기
https://school.programmers.co.kr/learn/courses/30/lessons/12977
#include <bits/stdc++.h>
using namespace std;
const int mx=50000;
bool arr[mx];
void eratos(){
fill(arr,arr+mx,true);
arr[0]=arr[1]=false;
for(int i=2;i*i<=mx;i++){
if(arr[i]){
for(int j=i*i;j<=mx;j+=i){
arr[j]=false;
}
}
}
}
int solution(vector<int> nums) {
int answer = -1;
int n=nums.size();
sort(nums.begin(), nums.end());
int sum=0;
answer=0;
eratos();
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
for(int k=j+1;k<n;k++){
sum=nums[i]+nums[j]+nums[k];
if(arr[sum]) answer++;
}
}
}
return answer;
}
쉽다.
두개뽑아서 더하기
#include <bits/stdc++.h>
using namespace std;
vector<int> solution(vector<int> numbers) {
vector<int> answer;
//sort(numbers.begin(), numbers.end());
set<int> s;
int sum=0;
int n=numbers.size();
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
sum=numbers[i]+numbers[j];
s.insert(sum);
sum=0;
}
}
for(auto it:s){
answer.push_back(it);
}
//sort(answer.begin(), answer.end());
return answer;
}
set는 자동으로 오름차순 정렬
예산
코드
#include <bits/stdc++.h>
using namespace std;
int solution(vector<int> d, int budget) {
int answer = 0;
sort(d.begin(), d.end());
int sum=0;
for(int dd:d){
sum+=dd; answer++;
if(sum>budget){
sum-=dd;
answer--;
break;
}
else if(sum==budget) break;
}
return answer;
}
3진법 뒤집기
코드
#include <bits/stdc++.h>
using namespace std;
int solution(int n) {
int answer = 0;
vector<int> t;
while(n!=0){
t.push_back(n%3);
n/=3;
}
reverse(t.begin(), t.end());
int inc=0;
for(int i=0;i<t.size();i++){
inc=pow(3,i);
answer+=inc*t[i];
}
return answer;
}
약수의 개수
코드
#include <bits/stdc++.h>
using namespace std;
int check(int num){
if(num<=0) return 0;
int cnt=0;
for(int i=1;i<=num;i++){
if(num%i==0) cnt++;
}
return cnt;
}
int solution(int left, int right) {
int answer = 0;
for(int i=left;i<=right;i++){
if(check(i)%2==0){
answer+=i;
}
else answer-=i;
}
return answer;
}
옹알이
코드
#include <bits/stdc++.h>
using namespace std;
int solution(vector<string> babbling) {
int answer = 0;
string a[4]={"aya", "ye", "woo", "ma"};
string temp="";
for(string s:babbling){
temp="";
for(int i=0;i<s.size();i++){
temp+=s[i];
for(int i=0;i<4;i++){
if(temp==a[i]){
temp="";
}
}
}
if(temp=="") answer++;
}
return answer;
}
평행
#include <bits/stdc++.h>
using namespace std;
int solution(vector<vector<int>> dots) {
int answer = 0;
if((double)(dots[0][1]-dots[1][1])/(dots[0][0]-dots[1][0]) ==
(double) (dots[2][1]-dots[3][1])/(dots[2][0]-dots[3][0]) ||
(double)(dots[0][1]-dots[2][1])/(dots[0][0]-dots[2][0]) ==
(double) (dots[1][1]-dots[3][1])/(dots[1][0]-dots[3][0] )||
(double)(dots[0][1]-dots[3][1])/(dots[0][0]-dots[3][0]) ==
(double) (dots[1][1]-dots[2][1])/(dots[1][0]-dots[2][0])) answer++;
// cout << (double)dots[0][1]-dots[1][1]/(dots[0][0]-dots[1][0]) <<"\n";
// cout <<(double)dots[2][1]-dots[3][1]/(dots[2][0]-dots[3][0]) <<"\n";
// cout << (double)dots[0][1]-dots[2][1]/(dots[0][0]-dots[2][0]) <<"\n";
// cout <<(double) dots[1][1]-dots[3][1]/(dots[1][0]-dots[3][0] )<<"\n";
// cout << (double)dots[0][1]-dots[3][1]/(dots[0][0]-dots[3][0]) <<"\n";
// cout <<(double) dots[1][1]-dots[2][1]/(dots[1][0]-dots[2][0]) <<"\n";
return answer;
}
안전지대
#include <bits/stdc++.h>
using namespace std;
int a[104][104];
vector<pair<int,int>> loc;
int solution(vector<vector<int>> board) {
int answer = 0;
int n=board.size();
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(board[i][j]==1) loc.push_back({i,j});
}
}
for(pair<int,int>l:loc){
int i=l.first; int j=l.second;
if(i>=1&&j<=n-2){
board[i-1][j+1]=1;
}
if(i<=n-2 && j<=n-2) board[i+1][j+1]=1;
if(1<=i && 1<=j) board[i-1][j-1]=1;
if(i<=n-2 && 1<=j) board[i+1][j-1]=1;
if(j<=n-2) board[i][j+1]=1;
if(1<=i) board[i-1][j]=1;
if(i<=n-2) board[i+1][j]=1;
if(1<=j) board[i][j-1]=1;
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(board[i][j]==0) answer++;
}
}
return answer;
}
728x90
'알고리즘 > 알고리즘 문제풀이' 카테고리의 다른 글
2156번: 포도주 시식 (0) | 2023.05.22 |
---|---|
1715번: 카드 정렬하기 (0) | 2023.05.22 |
프로그래머스 문제풀이 모음 (0) | 2023.03.20 |
프로그래머스 문제풀이 모음 (4) | 2023.03.20 |
프로그래머스 문제 풀이 모음 (0) | 2023.03.19 |