Skip to content

Instantly share code, notes, and snippets.

@DongguemYoo
Last active May 8, 2020 06:19
Show Gist options
  • Save DongguemYoo/2ce8e233df5b04d572bc788c5fe58501 to your computer and use it in GitHub Desktop.
Save DongguemYoo/2ce8e233df5b04d572bc788c5fe58501 to your computer and use it in GitHub Desktop.
코딩테스트 연습 스택/큐 쇠막대기
//하루 걸린듯
//푼 내가 대단하다
//어케풀엇징
방식은 이럼
일단 두개의 큐를 준비
쇠막대기 큐
레이져 큐
쇠막대기 큐가 증가할때마다 ( ( 이 연속으로 진행될때마다
쇠막대기 갯수를 증가
레이저를 만낫을때는 레이져 숫자증가하고
레이져로 자른앞자리의 숫자만 answer에 더하기 (뒷일은 어떻게 될지 아직 모름)
쇠막대기가 닫히면 잘린 쇠막대기 숫자를 1씩 증가
예외) 레이저 카운트가 0인데 쇠막대기가 닫히면 없는 쇠막대기라서 카운트 안함
이런식으로하니까 풀리긴함
얼른 다른사람꺼 보고싶다 ㅋㅋ
using System;
using System.Collections;
using System.Linq;
public class Solution {
public int solution(string arrangement)
{
Stack stack = new Stack();
Stack LaserStack = new Stack();
int answer = 0;
int startcount = 0;
int laser=0;
char[] tmpchar = arrangement.ToCharArray();
for (int i = 0; i < tmpchar.Count(); i++)
{
if (stack.Count == 0)
{
stack.Push(tmpchar[i].ToString());
startcount++;
continue;
}
if (i < tmpchar.Count() - 1)
{
if (tmpchar[i].ToString() == "(" && tmpchar[i + 1].ToString() == ")" && LaserStack.Count == 0)
{
LaserStack.Push(tmpchar[i].ToString());
laser++;
continue;
}
}
if (stack.Peek().ToString() == tmpchar[i].ToString())
{
stack.Push(tmpchar[i].ToString());
startcount++;
continue;
}
if (LaserStack.Count == 1 && LaserStack.Peek().ToString() =="(" && tmpchar[i].ToString() == ")")
{
answer += startcount;
LaserStack.Pop();
continue;
}
else if (LaserStack.Count == 0 && tmpchar[i].ToString() == ")")
{
if(laser != 0)
answer += 1;
--startcount;
stack.Pop();
if (stack.Count == 0)
laser = 0;
continue;
}
}
return answer;
}
}
//다른사람 풀이
//와 개돌앗넹 개쉽게 풀엇넹 ㅋㅋㅋㅋㅋ
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int solution(string arrangement) {
int answer = 0;
Stack<char> stk = new Stack<char>();
bool isLazer = false;
foreach (var c in arrangement)
{
if (c == '(')
{
stk.Push(c);
isLazer = true;//와...지렷네...레이져true이거로 체크되네...진짜 똑똑하네...
}
else
{
stk.Pop();
answer += isLazer ? stk.Count : 1; //아...여기가 힌트구나...스택의 카운트만큼 더하면 앞자리 더한걸 채크할수있네...돌앗다...
isLazer = false;//와...지렷네...레이져true이거로 체크되네...진짜 똑똑하네...
//완벽하다...
()인경우도 완벽하게 할수있는게
pop으로 빼버려서 0을 더하게 되네...미쳣다미쳣엉
}
}
return answer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment