모든 챌린지
쉬움 24명 해결 정답률 48.0%

Rectangle Perimeter

Compute the perimeter of a rectangle from its sides.

배우게 될 내용

  • Store values in variables
  • Apply a simple formula
  • Return a computed result

문제

번역본 보기

A rectangle has two sides of length width and two sides of length height. Its perimeter is the total length of all four sides.

Implement rectangle_perimeter(width, height).

Input

width: The width of the rectangle. height: The height of the rectangle.

Output

Return the perimeter as an integer.

제약 조건

1 ≤ width, height ≤ 1,000,000

예시

예시 1
입력:
width = 3
height = 5

예상 출력:
expected = 16
입력:
long long width = 3;
long long height = 5;

예상 출력:
long long expected = 16;
입력:
long width = 3;
long height = 5;

예상 출력:
long expected = 16;

설명

Two sides of 3 and two sides of 5: 3 + 3 + 5 + 5 = 16.

예시 2
입력:
width = 7
height = 7

예상 출력:
expected = 28
입력:
long long width = 7;
long long height = 7;

예상 출력:
long long expected = 28;
입력:
long width = 7;
long height = 7;

예상 출력:
long expected = 28;

설명

A square is a rectangle too: 4 × 7 = 28.

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

A rectangle has two equal widths and two equal heights.

힌트 2

Add all four sides, or combine the two repeated side lengths first.

힌트 3

Watch for integer arithmetic only; no special cases are needed here.

예상 복잡도

O(1) time, O(1) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제약 조건을 기준으로 풀어 주세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

# 코더 언어 시간 메모리 AI 평가 풀이
1 ゼゼロダニ Python 782 ms - 4/5 코드 보기
2 TATiến Anh Nguyễn Python 805 ms - 5/5 코드 보기
3 DDdang duong Python 818 ms - 5/5 코드 보기
4 HJhettory Jurash Python 820 ms - 5/5 코드 보기
5 JVJulien VACHAUDEZ Python 827 ms - 5/5 코드 보기
6 LMliam morales Python 831 ms - 5/5 코드 보기
7 ДЗДавид Зайдулин Python 834 ms - 4/5 코드 보기
8 RCRiccardo Corti Python 835 ms - 5/5 코드 보기
9 Nnongatomeiei Python 838 ms - 4/5 코드 보기
10 LMLaura Mahlow Python 845 ms - 5/5 코드 보기
11 JSJithin Sebastian Python 851 ms - 5/5 코드 보기
12 Ccheodo Python 857 ms - 5/5 코드 보기
13 KKKaneki Ken Python 862 ms - 5/5 코드 보기
14 ННикита Python 865 ms - 5/5 코드 보기
15 DBDaltrey Barbosa Python 871 ms - 4/5 코드 보기
16 PKPornchai Kaewkrabil Go 1,154 ms - 5/5 코드 보기
17 GTGarlic The Destroyer Rust 1,981 ms - 5/5 코드 보기
18 โสโสรัจจา สังข์แก้ว C++ 3,072 ms - 5/5 코드 보기
19 NNNhân Nguyễn Thiện C++ 3,082 ms - 4/5 코드 보기
20 YSYoo Seungmin Java 3,100 ms - 4/5 코드 보기

다른 사람의 풀이 보기

커뮤니티 풀이

스포일러: 아래에 전체 풀이 23개가 있습니다 먼저 직접 풀어보세요. 다른 개발자의 코드와 AI 리뷰도 참고할 수 있습니다. 로그인하고 문제를 풀면 토론에 참여할 수 있습니다.