제이커브(Jcurve)
카멜레온 개발자 이야기
제이커브(Jcurve)
전체 방문자
오늘
어제
  • 분류 전체보기 (26)
    • Programming skills (19)
      • Unity (8)
      • C# (1)
      • 자료구조 (3)
      • 알고리즘 (1)
      • Git (1)
      • CS(Computer Science) (4)
      • Unreal (0)
      • C++ (1)
    • Literacy Review (1)
    • Finance (1)
      • Metaverse (0)
      • 주식 (1)
      • 가상화폐 (0)
    • Certificates (0)
      • 기사 (0)
      • Coursera (0)
      • Fast campus (0)
    • Architecture (5)
      • 건축시공학 (4)
      • 철근콘크리트구조 (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 직장인자기계발
  • 패스트캠퍼스후기
  • 일상
  • 백준
  • 유니티
  • VR #메타버스 #metaverse #Eyetracking #아이트래킹 #데이터추출
  • 건축
  • 건축시공학
  • 유니티강의
  • cmatrisk
  • 자료구조 # 스택과 큐
  • 패캠챌린지
  • 건축전문가
  • c++문법
  • 건설사업관리
  • 게임개발
  • 일상 #초보 #기록# 일기
  • 주식
  • 스마트건설기술
  • Z세대
  • 건설시공관리자
  • 패스트캠퍼스
  • 싱글톤 # 유니티 #c# #Singleton
  • 직장인인강
  • ㅊ++기본문법
  • Literacy review
  • 역타공법
  • 건설현장조직구성
  • C#과 유니티로 배우는 게임 개발 올인원 패키지 Online
  • 건축공학

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
제이커브(Jcurve)

카멜레온 개발자 이야기

프로그래머스 k번째수(정렬, 스택 큐로 )
Programming skills/알고리즘

프로그래머스 k번째수(정렬, 스택 큐로 )

2023. 10. 11. 23:05

K번째수

 

https://school.programmers.co.kr/learn/courses/30/lessons/42748

 

프로그래머스

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

programmers.co.kr


 

using System;
using System.Collections.Generic;
using System.Linq;

public class Solution {
    public int[] solution(int[] array, int[,] commands) {
        
        //2차원 배열의 행 갯수만큼 answer 배열 초기화
        int[] answer= new int[commands.GetLength(0)];
        //0부터 commands 2차원 배열의 행 갯수까지 반복
        for(int n = 0; n < commands.GetLength(0); n++){
            
            //각 i,j,k원소 매횟수마다 초기화
            int i = commands[n,0];
            int j = commands[n,1];
            int k = commands[n,2];
            //i번째부터 j까지 자른 배열선언
            int[] temp = new int[j - i +1];
            //자른 배열 크기만큼 반복하여 i번째부터 숫자대입
            for(int a = 0; a< temp.Length; a++){
                temp[a] = array[a+i-1];
                
            }
            //자른 배열 정렬
            Array.Sort(temp);
            answer[n]= temp[k-1];
            
        }
        
        //k번째 숫자 구하기
        return answer;
    }
}

List를 이용해서 풀어보기

using System;
using System.Collections.Generic;
using System.Linq;

public class Solution {
    public int[] solution(int[] array, int[,] commands) {
        List<int>answer = new List<int>();
        List<int>temp = new List<int>();
        
        for(int i =0; i<commands.GetLength(0); i++){
            //min 처음 위치(index라-1로 계산)
            int min = commands[i,0] - 1;
            //max(처음 자르는 위치에서 얼마나 계산하는지)
            int max = commands[i,1] - min;
            //min부터 max까지 요소 추출
            temp = array.ToList<int>().GetRange(min, max);
            temp.Sort();
            //특정위치 수 반환
            answer.Add(temp[commands[i,2] -1]);
        }
        return answer.ToArray();
        }
    }
    제이커브(Jcurve)
    제이커브(Jcurve)
    미래지향적인 성향으로 VR/AR, XR 등 가상현실에서 살아가는 사람들에 대한 공간을 연구하는 이야기입니다. 가상현실에 대한 공부와 연구를 주로 합니다.

    티스토리툴바