잡다한 배똥월드

728x90
코딩 테스트 풀이 체크리스트
2시간 내에 풀었는가? X
본인의 실력으로 풀었는가? X

 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

 

 

import java.util.*;
class Solution {
    int row, col;
    Deque<Integer> side1, side2;
    Deque<Deque<Integer>> inside;
    public int[][] solution(int[][] rc, String[] operations) {
        row = rc.length;
        col = rc[0].length;
        int[][] answer = new int[row][col];
        
        side1 = new LinkedList<>();
        side2 = new LinkedList<>();
        inside = new LinkedList<>();
        
        for (int i = 0; i < row; i++) {
            inside.addLast(new LinkedList<>());
            for (int j = 0; j < col; j++) {
                if (j == 0) side1.addLast(rc[i][j]);
                else if (j == col-1) side2.addLast(rc[i][j]);
                else inside.peekLast().addLast(rc[i][j]);
            }
        }
        
        for (int i = 0; i < operations.length; i++) {
            if (operations[i].equals("Rotate")) {
                rotate();
            } else {
                shiftRow();
            }
        }
        
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (j == 0) answer[i][j] = side1.removeFirst();
                else if (j == col-1) answer[i][j] = side2.removeFirst();
                else answer[i][j] = inside.peekFirst().removeFirst();
            }
            inside.removeFirst();
        }
        
        return answer;
    }
    public void shiftRow() {
        side1.addFirst(side1.removeLast());
        side2.addFirst(side2.removeLast());
        inside.addFirst(inside.removeLast());
    }
    
    public void rotate() {
        inside.peekFirst().addFirst(side1.removeFirst());
        side2.addFirst(inside.peekFirst().removeLast());
        inside.peekLast().addLast(side2.removeLast());
        side1.addLast(inside.peekLast().removeFirst());
    }
}

 

 

 

 

 

참고 링크

 

 

 

[프로그래머스,Java] Level4: 행렬과 연산

https://school.programmers.co.kr/learn/courses/30/lessons/118670 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘..

taehoung0102.tistory.com

 

 

 

 

 

 

728x90
728x90
코딩 테스트 풀이 체크리스트
2시간 내에 풀었는가? O
본인의 실력으로 풀었는가? O

 

 

10816번: 숫자 카드 2

첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,

www.acmicpc.net

 

 

 

 

 

import java.io.*;
import java.util.HashMap;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		//가지고 있는 숫자카드를 정리할 해시맵
		HashMap<String, Integer> map = new HashMap<>();
		
		//해시맵에 넣는 과정
		//getOrDefault는 해시맵에 키에 대한 값이 있으면 그것을 리턴하고, 아니면 default값으로 설정해놓은 값을 리턴함
		int n = Integer.parseInt(br.readLine());
		String[] nums = br.readLine().split(" ");
		for (int i = 0; i < n; i++) {
			map.put(nums[i], map.getOrDefault(nums[i], 0)+1);
		}
		
		int t = Integer.parseInt(br.readLine());
		String[] test = br.readLine().split(" ");
		for (int i = 0; i < t; i++) {
			bw.write(map.getOrDefault(test[i], 0) + " ");
		}
		
		bw.flush();
		bw.close();
	}
}

 

문제 결과 메모리 시간 코드 길이
10816 맞았습니다!! 167456 KB 1144 ms 722 B

 

 

 

 

 

 

728x90
728x90
코딩 테스트 풀이 체크리스트
2시간 내에 풀었는가? O
본인의 실력으로 풀었는가? O

 

 

10799번: 쇠막대기

여러 개의 쇠막대기를 레이저로 절단하려고 한다. 효율적인 작업을 위해서 쇠막대기를 아래에서 위로 겹쳐 놓고, 레이저를 위에서 수직으로 발사하여 쇠막대기들을 자른다. 쇠막대기와 레이저

www.acmicpc.net

 

 

 

 

 

import java.io.*;
import java.util.*;
public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		//괄호 줄 입력 받아서 char 배열로 변환
		//String 배열을 쓰지 않은 이유는 '('냐 ')'냐 비교할 때 
		char[] pipe = br.readLine().toCharArray();
		//괄호 문제여서 그냥 스택을 썼지만 그냥 int 써도 괜찮음
		Stack<Character> s = new Stack<>();
		//result는 쇠파이프 조각 개수
		int result = 0;
		for (int i = 0; i < pipe.length-1; i++) {
			만약 pipe[i]가 열린 괄호이고,
			if (pipe[i] == '(') {
				//만약 pipe[i+1]이 닫힌 괄호면
				if (pipe[i+1] == ')') {
					자르는 타이밍이기 때문에 스택의 개수 더해주고 i+1은 체크할 필요 없어서 i++
					result += s.size();
					i++;
				} else { //닫힌 괄호가 아니면 파이프의 시작 부분이기 때문에 스택에 추가
					s.add('(');
				}
			} else { //닫힌 괄호면 스택에서 하나 빼주고 개수 하나 더해줘야 함
				s.pop();
				result++;
			}
		}
		//마지막 문자열은 체크하지 않기 때문에 1 더해줘야함
		System.out.println(result+1);
	}
}

 

 

문제 결과 메모리 시간 코드 길이
10799 맞았습니다!! 16440 KB 176 ms 555 B

 

 

 

 

 

 

728x90
728x90
코딩 테스트 풀이 체크리스트
2시간 내에 풀었는가? O
본인의 실력으로 풀었는가? O

 

 

10773번: 제로

첫 번째 줄에 정수 K가 주어진다. (1 ≤ K ≤ 100,000) 이후 K개의 줄에 정수가 1개씩 주어진다. 정수는 0에서 1,000,000 사이의 값을 가지며, 정수가 "0" 일 경우에는 가장 최근에 쓴 수를 지우고, 아닐 경

www.acmicpc.net

 

 

 

 

 

import java.io.*;
import java.util.*;
public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		//입력 받을 숫자 개수
		int n = Integer.parseInt(br.readLine());
		//출력할 숫자 합계
		int result = 0;
	
		//스택으로 가장 최근 입력 받은 숫자 파악하기 
		Stack<Integer> s = new Stack<>();
		
		for (int i = 0; i < n; i++) {
			int num = Integer.parseInt(br.readLine());
			//만약 num이 0이면 스택 최상위 숫자 제거
			if (num == 0) {
				s.pop();
			} else { //0이 아니면 스택에 추가
				s.add(num);
			}
		}
		//스택의 숫자를 하나씩 꺼내면서 result에 더해주기
		while (!s.isEmpty()) {
			result += s.pop();
		}
		
		System.out.println(result);
	}
}

 

문제 결과 메모리 시간 코드 길이
10773 맞았습니다!! 24032 KB 244 ms 539 B

 

 

 

 

 

 

728x90
728x90

 

코딩 테스트 풀이 체크리스트
2시간 내에 풀었는가? O
본인의 실력으로 풀었는가? O

 

 

9012번: 괄호

괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고

www.acmicpc.net

 

 

 

 

 

import java.io.*;
import java.util.*;
public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		//테스트 케이스 개수		
		int n = Integer.parseInt(br.readLine());
		
		for (int i = 0; i < n; i++) {
			//한 줄씩 입력 받아와서 char 배열로 변환
			char[] word = br.readLine().toCharArray();
			//열린 괄호를 담기 위한 스택
			Stack<Character> s = new Stack<>();
			//for문 중간에 탈출을 했는지 체크할 boolean
			boolean value = true;
			
			for (int j = 0; j < word.length; j++) {
				if (word[j] == '(') { //만약 열린 괄호라면 스택에 추가
					s.add(word[j]);
				} else { //닫힌 괄호라면
					if (!s.isEmpty()) { //스택이 비어있지 않으면 스택에서 하나 꺼내기
						s.pop();
					} else { //비어있으면 올바른 문자열이 아니기 때문에 value 값 변경 후 탈출
						value = false;
						break;
					}
				}
			}
			//만약 s가 비어있고, value도 true면 올바른 문자열이라서 YES, 그렇지 않으면 NO 출력
			bw.write((s.isEmpty() && value ? "YES" : "NO") + "\n");
		}
		
		
		bw.flush();
		bw.close();
	}
}

 

문제 결과 메모리 시간 코드 길이
9012 맞았습니다!! 14408 KB 136 ms 778 B

 

 

 

 

 

 

728x90
728x90
코딩 테스트 풀이 체크리스트
2시간 내에 풀었는가? O
본인의 실력으로 풀었는가? O

 

 

6588번: 골드바흐의 추측

각 테스트 케이스에 대해서, n = a + b 형태로 출력한다. 이때, a와 b는 홀수 소수이다. 숫자와 연산자는 공백 하나로 구분되어져 있다. 만약, n을 만들 수 있는 방법이 여러 가지라면, b-a가 가장 큰

www.acmicpc.net

 

 

 

 

 

import java.io.*;
import java.util.*;
public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		//소수인지를 담을 해시맵
		HashMap<Integer, Boolean> decimal = new HashMap<>();
		
		int num = 0;
		while ((num = Integer.parseInt(br.readLine())) != 0) {
			//소수 조합이 여러 개일 경우 차이가 큰 것을 리턴하는 것이기 때문에 작은 숫자부터 시작
			for (int i = 2; i <= num; i++) {
				//만약 i가 해시맵에 없으면 소수인지 파악 후 넣기
				if (!decimal.containsKey(i)) decimal.put(i, check(i));
				//i가 소수가 아니면 넘어가기
				if (!decimal.get(i)) continue;
				//n은 숫자에서 i를 뺀 숫자
				int n = num - i;
				//n 역시 해시맵에 없으면 소수인지 파악 후 넣기
				if (!decimal.containsKey(n)) decimal.put(n, check(n));
				//만약 n도 소수라면 bw에 입력 후 for문 종료
				if (decimal.get(n)) {
					bw.write(num + " = " + i + " + " + n + "\n");
					break;
				}
			}
		}
		
		bw.flush();
		bw.close();
	}
	//소수 파악용 함수
	public static boolean check(int num) {
		for (int i = 2; i <= Math.sqrt(num); i++) {
			if (num % i == 0) return false;
		}
		
		return true;
	}
}

 

문제 결과 메모리 시간 코드 길이
6588 맞았습니다!! 50316 KB 628 ms 915 B

 

 

 

 

 

 

728x90
728x90

📝 기본 설명

 

참고 링크 : 2021 Dev-Matching: 웹 프론트엔드 개발자(하반기)' 기출 문제 해설

 

'2021 Dev-Matching: 웹 프론트엔드 개발자(하반기)' 기출 문제 해설

프로그래머스에서는 지난 2021년 9월 4일 '2021 Dev-Matching: 프론트엔드 개발자(하반기)'의 과제 테스트가 진행되었습니다. 과제 리뷰가 제공되지 않지만, 어떻게 하면 구현을 더 잘할 수 있었을까?

prgms.tistory.com

 

 

 

 

 

위의 공식 문제 해설을 기반으로 작성한 코드입니다.

제대로 작동이 되지 않아 개인적으로 수정한 내용도 있으며, 추가한 내용도 있습니다.

 

 

 

 

 

GitHub로 이동하기

 

GitHub - b-sseung/21_DevMatching_2

Contribute to b-sseung/21_DevMatching_2 development by creating an account on GitHub.

github.com

 

 

 

 

 



📌 index.html

<html>
  <head>
    <title>커피캣 스토어</title>
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <main class="App"></main>
    <script src="src/index.js" type="module"></script>
  </body>
</html>

 

 

 

 

 


📌 api.js

const base = 'https://uikt6pohhh.execute-api.ap-northeast-2.amazonaws.com/dev';

export const request = async (url, options = {}) => {
  try {
    const fullUrl = `${base}${url}`
    const res = await fetch(fullUrl, options);

    if (res.ok) {
      return await res.json();
    }

    throw new Error('API 통신 실패');
  } catch (e) {
    new Error(e.message);
  }
}

 

 

 

 

 

📌 App.js

import ProductListPage from './ProductListPage.js';
import ProductDetailPage from './ProductDetailPage.js';
import CartPage from './CartPage.js';
import { init } from './router.js';

export default function App({ $target }) {
  //route() 함수임
  this.route = () => {
    const { pathname } = location;

    //route함수가 호출될 때마다 페이지 리셋
    $target.innerHTML = '';

    //pathname 수정 필요
    if (pathname === '/web/') {
      new ProductListPage({
        $target
      }).render();
    } else if (pathname.indexOf('/web/products/') === 0) {
      const [, , , productId] = pathname.split('/');
      new ProductDetailPage({
        $target,
        productId
      }).render();
    } else if (pathname === '/web/cart') {
      new CartPage({
        $target
      }).render();
    }
  }

  init(this.route);
  this.route();

  window.addEventListener('popstate', this.route);
}

 

 

 

 

 

 📌 Cart.js

import { routeChange } from './router.js';
import { removeItem } from './storage.js';

export default function Cart({ $target, initState }) {
  const $component = document.createElement('div');
  $component.className = 'Cart';
  this.state = initState;

  $target.appendChild($component);

  this.setState = nextState => {
    this.state = nextState;
    this.render();
  }

  this.getTotalPrice = () => {
    return this.state.reduce(
      (acc, option) => acc + ((option.productPrice + option.optionPrice) * option.quantity), 0)
  }

  this.render = () => {
    $component.innerHTML = `
      <ul>
        ${this.state.map(cartItem => `
          <li class="Cart__item">
            <img src="${cartItem.imageUrl}">
            <div class="Cart__itemDescription">
              <div>${cartItem.productName} ${cartItem.optionName} ${cartItem.quantity}개</div>
              <div>${cartItem.productPrice + cartItem.optionPrice}원</div>
            </div>
          </li>  
        `).join('')}
      </ul>
      <div class="Cart__totalPrice">
        총 상품가격 ${this.getTotalPrice()}원
      </div>
      <button class="OrderButton">주문하기</button>    
    `

    return $component;
  }

  this.render();

  $component.addEventListener('click', e => {
      if (e.target.className === 'OrderButton') {
      alert('주문 되었습니다!');
      removeItem('products_cart');
      routeChange('/web/');
    }
  })
}

 

 

 

 

 

📌 CartPage.js

import { request } from './api.js';
import { getItem } from './storage.js';
import { routeChange } from './router.js';
import Cart from './Cart.js';

export default function CartPage({ $target }) {
  const $page = document.createElement('div');
  $page.className = 'CartPage';

  $page.innerHTML = '<h1>장바구니</h1>'

  let cartComponent = null;

  this.setState = (nextState) => {
    this.state = nextState;
    this.render();
  }

  const cartData = getItem('products_cart', []);
  this.state = {
    products: null
  }

  this.render = () => {
    if (cartData.length === 0) {
      alert('장바구니가 비어있습니다.')
      routeChange('/web/');
    } else {
      $target.appendChild($page);
      if (this.state.products && !cartComponent) {
        cartComponent = new Cart({
          $target: $page,
          initState: this.state.products
        })
      }
    }
  }

  this.fetchProducts = async () => {
    const products = await Promise.all(cartData.map(async (cartItem) => {
      const product = await request(`/products/${cartItem.productId}`);
      const selectedOption = product.productOptions.find(option => option.id === cartItem.optionId);

      return {
        imageUrl: product.imageUrl,
        productName: product.name,
        quantity: cartItem.quantity,
        productPrice: product.price,
        optionName: selectedOption.name,
        optionPrice: selectedOption.price
      }
    }))

    this.setState({ products });
  }

  this.fetchProducts();
}

 

 

 

 

 

📌 index.js

import App from './App.js';

new App({$target: document.querySelector('.App')});

 

 

 

 

 

📌 ProductDetail.js

import SelectedOptions from './SelectedOptions.js';

export default function ProductDetail({ $target, initState }) {
    const $productDetail = document.createElement('div');
    $productDetail.className = 'ProductDetail';

    $target.appendChild($productDetail);

    this.state = initState;
    let selectedOptions = null;
    let isInitialized = false;

    this.setState = nextState => {
        this.state = nextState;
        this.render();

        if (selectedOptions) {
            selectedOptions.setState({
                ...this.state,
                selectedOptions: this.state.selectedOptions
            });
        }
    }

    this.render = () => {
        const { product } = this.state;

        if (!isInitialized) {
            $productDetail.innerHTML = `
                <img src="${product.imageUrl}">
                <div class="ProductDetail__info">
                    <h2>${product.name}</h2>
                    <div class="ProductDetail__price">${product.price}원~</div>
                    <select>
                        <option>선택하세요.</option>
                        ${product.productOptions.map(option => `
                            <option value="${option.id}" ${option.stock === 0 ? 'disable' : ''}>
                                ${option.stock === 0 ? '(품절) ' : ''}${product.name} ${option.name} ${option.price > 0 ? `(+${option.price}원)` : ''}
                            </option>
                        `).join('')}
                    </select>
                    <div class="ProductDetail__selectedOptions"></div>
                </div>
            `
            selectedOptions = new SelectedOptions({
                $target: $productDetail.querySelector('.ProductDetail__selectedOptions'),
                initState: {
                    product: this.state.product,
                    selectedOptions: this.state.selectedOptions
                }
            });
            isInitialized = true;
        }
    }

    this.render();

    $productDetail.addEventListener('change', (e) => {
        if (e.target.tagName === 'SELECT') {
            const selectedOptionId = parseInt(e.target.value);
            const { product, selectedOptions } = this.state;
            const option = product.productOptions.find(option => option.id === selectedOptionId)
            const selectedOption = selectedOptions.find(selectedOption => selectedOption.optionId === selectedOptionId)

            if (option && !selectedOption) {
                const nextSelectedOptions = [
                    ...selectedOptions,
                    {
                        productId: product.id,
                        optionId: option.id,
                        optionName: option.name,
                        optionPrice: option.price,
                        quantity: 1
                    }
                ];
                this.setState({
                    ...this.state,
                    selectedOptions: nextSelectedOptions
                });
            }
        }
    })
}

 

 

 

 

 

📌 ProductDetailPage.js

import { request } from './api.js';
import ProductDetail from './ProductDetail.js';

export default function ProductDetailPage({ $target, productId }) {
  this.state = {
    productId,
    product: null
  }

  const $page = document.createElement('div');
  $page.className = 'ProductDetailPage';

  $page.innerHTML = '<h1>상품 정보</h1>'

  this.setState = nextState => {
    this.state = nextState;
    this.render();
  }

  this.render = () => {
    if (!this.state.product) {
      $target.innerHTML = 'Loading...';
    } else {
      $target.innerHTML = '';
      $target.appendChild($page);
      //ProductDetail 렌더링하기

      new ProductDetail({
        $target: $page,
        initState: {
          product: this.state.product,
          selectedOptions: []
        }
      })
    }
  }

  this.fetchProduct = async () => {
    const { productId } = this.state;
    const product = await request(`/products/${productId}`);
    this.setState({
      ...this.state,
      product
    });
  }

  this.fetchProduct();
}

 

 

 

 

 

📌 ProductList.js

import { routeChange } from "./router.js";

export default function ProductList({ $target, initState }) {
  this.state = initState;

  const $productList = document.createElement('ul');
  $target.appendChild($productList);

  this.setState = (nextState) => {
    this.state = nextState;
    this.render();
  }

  this.render = () => {
    if (!this.state) return;

    $productList.innerHTML = `
      ${this.state.map((product) => 
        `<li class="Product" data-product-id="${product.id}">
          <a href="/web/products/${product.id}">
            <img src="${product.imageUrl}">
            <div class="Product__info">
              <div>${product.name}</div>
              <div>${product.price}원~</div>
            </div>
          </a>
        </li>
        `
      ).join('')}
    `
  }

  this.render();

  $productList.addEventListener('click', (e) => {
    const $li = e.target.closest('li');
    const { productId } = $li.dataset;

    if (productId) routeChange(`/web/products/${productId}`);
  });

}

 

 

 

 

 

📌 ProductListPage.js

import { request } from './api.js';
import ProductList from './ProductList.js';

export default function ProductListPage({ $target }) {
  //이 때 $target은 document.querySelector('.App')를 뜻함

  const $page = document.createElement('div');
  $page.className = 'ProductListPage';

  $page.innerHTML = '<h1>상품 목록</h1>';

  this.setState = (nextState) => {
    this.state = nextState
    this.render();
  }

  const fetchProducts = async () => {
    const products = await request('/products');
    this.setState(products);
    
      const productList = new ProductList({
        $target: $page,
        initState: this.state
    });
  }

  fetchProducts();

  this.render = () => {
    $target.appendChild($page);
  }
}

 

 

 

 

 

📌 router.js

const Router_CHANGE_EVENT = 'ROUTE_CHANGE';

export const init = (onRouteChange) => {
    window.addEventListener(Router_CHANGE_EVENT, () => {
        onRouteChange();
    });
}

export const routeChange = (url, params) => {
    history.pushState(null, null, url);
    window.dispatchEvent(new CustomEvent(Router_CHANGE_EVENT, params));
}

 

 

 

 

 

📌 SelectedOptions.js

import { getItem, setItem } from './storage.js';
import { routeChange } from './router.js';

export default function SelectedOptions({ $target, initState }) {
  const $component = document.createElement('div');
  $target.appendChild($component);

  this.state = initState;

  this.getTotalPrice = () => {
    const { product, selectedOptions } = this.state;
    const { price: productPrice } = product;

    return selectedOptions.reduce(
      (acc, option) => acc + ((productPrice + option.optionPrice) * option.quantity), 0
    );
  }

  this.setState = (nextState) => {
    this.state = nextState;
    this.render();
  }

  this.render = () => {
    const { product, selectedOptions = [] } = this.state;
    if (product && selectedOptions) {
      $component.innerHTML = `
        <h3>선택된 상품</h3>
        <ul>
          ${selectedOptions.map(selectedOption => `
            <li>
              ${selectedOption.optionName} ${product.price + selectedOption.optionPrice}원
              <input type="text" data-optionId="${selectedOption.optionId}" value="${selectedOption.quantity}">
            <li>
          `).join('')}
        </ul>
        <div class="ProductDetail__totalPrice">${this.getTotalPrice()}원</div>
        <button class="OrderButton">주문하기</button>
      `
    }
  }

  this.render();

  $component.addEventListener('click', (e) => {
    const { selectedOptions } = this.state;
    if (e.target.className === 'OrderButton') {
      const cartData = getItem('products_cart', []);
      setItem('products_cart', cartData.concat(selectedOptions.map(selectedOption => ({
        productId: selectedOption.productId,
        optionId: selectedOption.optionId,
        quantity: selectedOption.quantity
      }))));
      
      routeChange('/web/cart');
    }
  })

  $component.addEventListener('change', e => {
    if (e.target.tagName === 'INPUT') {
      try {
        const nextQuantity = parseInt(e.target.value);
        const nextSelectedOptions = [...this.state.selectedOptions]
        
        if (typeof nextQuantity === 'number') {
          const { product } = this.state;

          const optionId = parseInt(e.target.dataset.optionid);
          const option = product.productOptions.find(option => option.id === optionId);
          const selectedOptionIndex = nextSelectedOptions.findIndex(selectedOption => selectedOption.optionId === optionId);

          nextSelectedOptions[selectedOptionIndex].quantity = option.stock >= nextQuantity ? nextQuantity : option.stock;
          
          this.setState({
            ...this.state,
            selectedOptions: nextSelectedOptions
          })
        }
      } catch (e) {
        console.log(e);
      }
    }
  })
}

 

 

 

 

 

📌 storage.js

const storage = localStorage;

export const getItem = (key, defaultValue) => {
  try {
    const value = storage.getItem(key);
    return value ? JSON.parse(value) : defaultValue
  } catch {
    return defaultValue
  }
}

export const setItem = (key, value) => {
  try {
    storage.setItem(key, JSON.stringify(value));
  } catch {
    //ignore
  }
}

export const removeItem = (key) => {
  try {
    storage.removeItem(key);
  } catch {
    //ignore
  }
}

 

 

 

 

 

 

728x90
728x90
코딩 테스트 풀이 체크리스트
2시간 내에 풀었는가? O
본인의 실력으로 풀었는가? O

 

 

4949번: 균형잡힌 세상

하나 또는 여러줄에 걸쳐서 문자열이 주어진다. 각 문자열은 영문 알파벳, 공백, 소괄호("( )") 대괄호("[ ]")등으로 이루어져 있으며, 길이는 100글자보다 작거나 같다. 각 줄은 마침표(".")로 끝난다

www.acmicpc.net

 

 

 

 

 

import java.io.*;
import java.util.*;
public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		String s = "";

		//br로 한 줄씩 입력 받는 것을 s에 넣고, 문장이 담긴 s가 .이 아닐 때까지 반복
		while (!(s = br.readLine()).equals(".")) {
			//s를 char 배열로 변환 => String 배열로 변환해도 되지만 시간을 단축시키려면 char을 활용하는 편이 좋음
			char[] arr = s.toCharArray();
			//여는 괄호를 담을 스택. 닫는 괄호가 나올 때 제일 위에 있는 괄호를 꺼내는 용도
			Stack<Character> stack = new Stack<>();
			//균형이 맞는지 파악용
			boolean check = true;
			
			for (int i = 0; i < arr.length; i++) {
				//만약 여는 괄호면 그냥 stack에 담기
				if (arr[i] == '(' || arr[i] == '[') stack.add(arr[i]);
				
				//만약 닫는 괄호라면
				if (arr[i] == ')' || arr[i] == ']') {
					//스택이 비어있지 않고, 닫는 소괄호면 스택 최상위가 여는 소괄호이거나 닫는 대괄호면 스택 최상위가 여는 대괄호라면
					if (!stack.isEmpty() && ((arr[i] == ')' && stack.peek() =='(') || (arr[i] == ']' && stack.peek() =='['))) {
						//스택 최상위 값 제거
						stack.pop();
					} else {
						//그렇지 않으면 균형이 맞지 않으므로 check에는 false를 담고 for문 끝내기
						check = false;
						break;
					}
				}
			}
			//마지막 확인으로 스택이 비어있지 않으면 균형이 맞지 않기 때문에 확인
			if (!stack.isEmpty()) check = false;
			//check가 true면 yes를, false면 no를 출력
			bw.write((check ? "yes" : "no") + "\n");
		}
		
		bw.flush();
		bw.close();
	}
}

 

문제 결과 메모리 시간 코드 길이
4949 맞았습니다!! 19916 KB 240 ms 919 B

 

 

 

 

 

 

728x90

+ Recent posts