알고리즘

알고리즘

<백준> 9663번 자바 알고리즘

import java.util.*; public class Main { static int cnt, N; static int[] arr; public static void main(String[] args) { Scanner input = new Scanner(System.in); N = input.nextInt(); arr = new int[N]; nQueen(0); System.out.println(cnt); } private static void nQueen(int next){ if(next == N) { cnt++; return; } for(int i = 0; i < N; i++){ arr[next] = i; if(Possibility(next)){ nQueen(next + 1); } } } pr..

알고리즘

<백준> 15652번 자바 알고리즘

import java.util.*; public class Main { static int N, M; static int[] arr; static StringBuilder sb = new StringBuilder(); public static void main(String[] args) { arr = new int[8]; Scanner input = new Scanner(System.in); N = input.nextInt(); M = input.nextInt(); recursion(0); System.out.println(sb); } private static void recursion(int idx){ if(idx == M){ for(int i = 0; i < M; i++){ sb.append(arr..

알고리즘

<백준> 15651번 자바 알고리즘

import java.util.*; public class Main { static int N, M; static int[] arr; static StringBuilder sb = new StringBuilder(); public static void main(String[] args) { arr = new int[7]; Scanner input = new Scanner(System.in); N = input.nextInt(); M = input.nextInt(); recursion(0); System.out.println(sb); } private static void recursion(int idx){ if(idx == M){ for(int i = 0; i < M; i++){ sb.append(arr..

알고리즘

<백준> 15650번 자바 알고리즘

import java.util.*; public class Main { static int N, M; static int[] arr; static boolean[] visit; public static void main(String[] args) { Scanner input = new Scanner(System.in); N = input.nextInt(); M = input.nextInt(); arr = new int[N]; visit = new boolean[N + 1]; recursion(0); } public static void recursion(int idx) { if (idx == M){ for(int i = 0; i < M; i++){ System.out.print(arr[i] + " ");..

알고리즘

<리트코드> 622번 파이썬 알고리즘 [원형 큐]

def __init__(self, k: int): self.q = [None] * k self.maxlen = k self.p1 = 0 self.p2 = 0 def enQueue(self, value: int) -> bool: if self.q[self.p2] is None: self.q[self.p2] = value self.p2 = (self.p2 + 1) % self.maxlen return True else: return False def deQueue(self) -> bool: if self.q[self.p1] is not None: self.q[self.p1] = None self.p1 = (self.p1 + 1) % self.maxlen return True else: return False..

알고리즘

<리트코드> 739번 파이썬 알고리즘 [스택]

def dailyTemperatures(self, t: List[int]) -> List[int]: ans = [0] * len(t) stk = [] # store idx for i, cur in enumerate(t): while stk and cur > t[stk[-1]]: l = stk.pop() ans[l] = i - l stk.append(i) return ans

알고리즘

<리트코드> 20번 파이썬 알고리즘 [스택]

def isValid(self, s: str) -> bool: table = { "}" : "{", ")" : "(", "]" : "[" } stack = [] for c in s: if c in table: # stack 마지막과 c가 [] {} () 이런식으로 되는지 확인 if stack and stack[-1] == table[c]: stack.pop() else: return False else: stack.append(c) return True if not stack else False

알고리즘

<리트코드> 206번 파이썬 알고리즘 [연결 리스트 역순]

def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: prev, cur = None, head while cur: nxt = cur.next cur.next = prev prev = cur cur = nxt return prev

알고리즘

<리트코드> 21번 파이썬 알고리즘 [연결 리스트]

def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]: dummy = ListNode(0, head) prev, cur = dummy, head while cur and cur.next: # save ptrs nxtPair = cur.next.next scd = cur.next # reverse cur.next = nxtPair scd.next = cur prev.next = scd # update prev = cur cur = nxtPair return dummy.next # 필요한 것 처음에 무엇을 저장할 포인터로 설정 해야 할지 생각하기 이후에 reverse 코드 짜기 다음으로 업데이트 하기

알고리즘

<백준> 5525번 파이썬 알고리즘

n = int(input()) m = int(input()) s = input() ans = 0 cnt = 0 for i in range(1, m - 1): if s[i - 1] == "I": if s[i] == "O" and s[i + 1] == "I": cnt += 1 i += 2 if n == cnt: ans += 1 cnt -= 1 else: cnt = 0 print(ans) I로 시작했을 때 그뒤에 OI가 있으면 cnt += 1 그리고 cnt 가 n 과 일치할 때 ans += 1 이후에 또 판별해야되니까 cnt -= 1 하는 것이다

changha.
'알고리즘' 카테고리의 글 목록 (6 Page)