Use var as the type of any local variable declaration (even in a for statement), and the type will be inferred from the initializing expression (any further assignments to the variable are not involved in this type inference).
For example: var x = 10.0; will infer double, and var y = new ArrayList<String>(); will infer ArrayList<String>.
Note that this is an annotation type because var x = 10; will be desugared to @var int x = 10;
Complete documentation is found at the project lombok features page for @var .
var 사용법에 적힌 저 'will be desugared to '의 뜻이 무엇일까 찾아보니 간결하게 표기된 문장(슈가링된 문장)을 다시 원래대로 돌린다는 뜻이었다. (흥미로웠지만 문제를 해결할만한 단서는 아니었다. )
Use var as the type of any local variable declaration (even in a for statement), and the type will be inferred from the initializing expression (any further assignments to the variable are not involved in this type inference).
For example: var x = 10.0; will infer double, and var y = new ArrayList<String>(); will infer ArrayList<String>.
Note that this is an annotation type because var x = 10; will be desugared to @var int x = 10;
Complete documentation is found at the project lombok features page for @var .
var 사용법에 적힌 저 'will be desugared to '의 뜻이 무엇일까 찾아보니 간결하게 표기된 문장(슈가링된 문장)을 다시 원래대로 돌린다는 뜻이었다. (흥미로웠지만 문제를 해결할만한 단서는 아니었다. )
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 15916 100 15916 0 0 39592 0 --:--:-- --:--:-- --:--:-- 39592
=> Downloading nvm from git to '/Users/seungmikim/.nvm'
=> Cloning into '/Users/seungmikim/.nvm'...
remote: Enumerating objects: 356, done.
remote: Counting objects: 100% (356/356), done.
remote: Compressing objects: 100% (303/303), done.
remote: Total 356 (delta 39), reused 164 (delta 27), pack-reused 0
Receiving objects: 100% (356/356), 222.15 KiB | 1.30 MiB/s, done.
Resolving deltas: 100% (39/39), done.
* (HEAD detached at FETCH_HEAD)
master
=> Compressing and cleaning up git repository
=> Appending nvm source string to /Users/seungmikim/.zshrc
=> Appending bash_completion source string to /Users/seungmikim/.zshrc
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
=> You currently have modules installed globally with `npm`. These will no
=> longer be linked to the active version of Node when you install a new node
=> with `nvm`; and they may (depending on how you construct your `$PATH`)
=> override the binaries of modules installed with `nvm`:
/usr/local/lib
├── corepack@0.10.0
=> If you wish to uninstall them at a later point (or re-install them under your
=> `nvm` Nodes), you can remove them from the system Node as follows:
$ nvm use system
$ npm uninstall -g a_module
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
(base) ~ nvm --version
zsh: command not found: nvm
매운 것을 좋아하는 Leo는 모든 음식의 스코빌 지수를 K 이상으로 만들고 싶습니다. 모든 음식의 스코빌 지수를 K 이상으로 만들기 위해 Leo는 스코빌 지수가 가장 낮은 두 개의 음식을 아래와 같이 특별한 방법으로 섞어 새로운 음식을 만듭니다.
섞은 음식의 스코빌 지수 = 가장 맵지 않은 음식의 스코빌 지수 + (두 번째로 맵지 않은 음식의 스코빌 지수 * 2)
Leo는 모든 음식의 스코빌 지수가 K 이상이 될 때까지 반복하여 섞습니다. Leo가 가진 음식의 스코빌 지수를 담은 배열 scoville과 원하는 스코빌 지수 K가 주어질 때, 모든 음식의 스코빌 지수를 K 이상으로 만들기 위해 섞어야 하는 최소 횟수를 return 하도록 solution 함수를 작성해주세요.
제한 사항
scoville의 길이는 2 이상 1,000,000 이하입니다.
K는 0 이상 1,000,000,000 이하입니다.
scoville의 원소는 각각 0 이상 1,000,000 이하입니다.
모든 음식의 스코빌 지수를 K 이상으로 만들 수 없는 경우에는 -1을 return 합니다.
입출력 예scovilleKreturn
[1, 2, 3, 9, 10, 12]
7
2
입출력 예 설명
스코빌 지수가 1인 음식과 2인 음식을 섞으면 음식의 스코빌 지수가 아래와 같이 됩니다. 새로운 음식의 스코빌 지수 = 1 + (2 * 2) = 5 가진 음식의 스코빌 지수 = [5, 3, 9, 10, 12]
스코빌 지수가 3인 음식과 5인 음식을 섞으면 음식의 스코빌 지수가 아래와 같이 됩니다. 새로운 음식의 스코빌 지수 = 3 + (5 * 2) = 13 가진 음식의 스코빌 지수 = [13, 9, 10, 12]
모든 음식의 스코빌 지수가 7 이상이 되었고 이때 섞은 횟수는 2회입니다
풀이
import heapq
def solution(scoville, K):
answer = 0
scoville.sort()
while scoville:
f1=heapq.heappop(scoville)
if f1<K:
f2=heapq.heappop(scoville)
heapq.heappush(scoville,f1+(f2*2))
answer+=1
return answer
파이썬 기본 라이브러리인 heapq를 이용하였다. 시간의 효율성 통과를 위해 미리 sort()한 수 앞에서부터 하나씩 꺼내 주어진 원하는 스코빌 지수보다 낮은 수가 들어오면 하나를 더 꺼내 새 음식을 만들어 ( 식에 맞게 계산해) 다시 그 힙에 heappush()하였다. 이 연산을 할 때마다 answer을 하나씩 증가해 주었다.
효율성은 통과하지만 몇몇 테스트케이스에서 런타임 에러가 났다.
테스트 1, 테스트 3, 테스트 8, 테스트 14 실패
제한사항 부분을 하나 놓친 것 때문이다.
모든 음식의 스코빌 지수를 K 이상으로 만들 수 없는 경우에는 -1을 return 합니다.
import heapq
def solution(scoville, K):
answer = 0
scoville.sort()
while scoville:
f1=heapq.heappop(scoville)
if f1<K and scoville:
f2=heapq.heappop(scoville)
heapq.heappush(scoville,f1+(f2*2))
answer+=1
new_food=f1+(f2*2)
if new_food<K:
return -1
return answer
생각해 보니 while문 안에 if 문에서 scoville이 비어있는 경우를 체크하지 못한 것 같아서 그 조건을 추가하고, 루프를 다 돌았을 경우 new_food 변수에 마지막 음식의 스코빌지수가 담기는데 이 수가 기준 스코빌 지수보다 작으면 모든 음식의 스코빌 지수를 K이상으로 만들 수 없으므로 -1을 리턴한다.