Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- Class
- hash table
- Data Structure
- implement
- two pointers
- Number Theory
- Math
- Binary Tree
- 코딩테스트
- 파이썬
- 코테
- sorting
- Binary Search
- SQL
- Method
- Stack
- array
- java
- 자바
- Counting
- Matrix
- dynamic programming
- 구현
- database
- Tree
- geometry
- greedy
- string
- bit manipulation
- simulation
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 806. Number of Lines To Write String 본문
1. Input
1) 문자열 s
2) int 배열 widths
3) widths[0]은 a의 가로 픽셀을, widths[25]는 z의 가로 픽셀을 나타낸다.
2. Output
1) 한 줄에 100 픽셀씩 쓸 수 있을 때, s에 있는 문자열을 순서대로 출력할 때 필요한 줄의 수와 마지막 줄의 픽셀 수를 순서대로 담은 int 배열
3. Constraint
1) widths.length == 26
2) 2 <= widths[i] <= 10
3) 1 <= s.length <= 1000
4) s는 영어 소문자로만 이루어져 있다.
4. Example
Input: widths = {4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10}, s = "bbbcccdddaaa" -> Output: {2,4]
설명: widths에 맞게 s를 출력해보면
bbbcccdddaa // 98 픽셀
a // 4 픽셀
2줄에 걸쳐 출력했고, 마지막 줄은 4픽셀을 차지하므로 {2, 4}를 반환한다.
5. Code
1) 첫 코드(2022/07/05)
int sum = 0;
int count = 1;
for(int i=0 ; i<s.length() ; i++){
int c = s.charAt(i) - 97;
if(sum + widths[c] <= 100)
sum += widths[c];
else{
count++;
sum = widths[c];
}
} // for i
return new int[] {count, sum};
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 824. Goat Latin (0) | 2022.12.08 |
---|---|
[LeetCode/Easy] 821. Shortest Distance to a Character (0) | 2022.12.08 |
[LeetCode/Easy] 804. Unique Morse Code Words (0) | 2022.12.07 |
[LeetCode/Easy] 796. Rotate String (0) | 2022.12.07 |
[LeetCode/Easy] 771. Jewels and Stones (0) | 2022.12.07 |