saurus2
Saurus2
saurus2
전체 방문자
오늘
어제
  • 분류 전체보기
    • 개발
      • AJAX
    • ML Ops
    • Profile
    • 음식점
    • 배낭여행
    • 컴퓨터공학
      • 알고리즘 공부
      • C++
      • Sever 스터디
      • Java spring
      • 알고리즘 _ 문제해결
      • 딥러닝
      • Java 정리
      • Python
      • LeetCode 1000
      • Machine Learning Study
      • Sign language Detection Pro..
      • LeetCode Solutions
    • 비콘
    • 데일리 리포트
    • 유학일기
      • 영어 공부
      • Daily
    • AI Master Degree
      • Data Mining
      • AI and Data engineering
      • Math Foundations for Decisi..
      • Natural Language Processing

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • 개발자
  • 알고리즘문제해결
  • LeetCode
  • Python
  • 온라인저지
  • 백준
  • DFS
  • BFS
  • c++
  • 문제해결능력
  • 취업준비
  • 딕셔너리
  • two pointer
  • 리트코드
  • 알고리즘
  • 딥러닝
  • 릿코드
  • 취준
  • 파이썬
  • 개발자 취업준비

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
saurus2

Saurus2

컴퓨터공학/LeetCode 1000

[LeetCode] 1165. Single-Row Keyboard

2022. 12. 2. 03:54

1165. Single-Row Keyboard

Easy

There is a special keyboard with all keys in a single row.

Given a string keyboard of length 26 indicating the layout of the keyboard (indexed from 0 to 25). Initially, your finger is at index 0. To type a character, you have to move your finger to the index of the desired character. The time taken to move your finger from index i to index j is |i - j|.

You want to type a string word. Write a function to calculate how much time it takes to type it with one finger.

 

Example 1:

Input: keyboard = "abcdefghijklmnopqrstuvwxyz", word = "cba"
Output: 4
Explanation: The index moves from 0 to 2 to write 'c' then to 1 to write 'b' then to 0 again to write 'a'.
Total time = 2 + 1 + 1 = 4. 

Example 2:

Input: keyboard = "pqrstuvwxyzabcdefghijklmno", word = "leetcode"
Output: 73

 

Constraints:

  • keyboard.length == 26
  • keyboard contains each English lowercase letter exactly once in some order.
  • 1 <= word.length <= 104
  • word[i] is an English lowercase letter.

문제 풀이

  • 키보드 배열과 단어하나가 주어진다.
  • 주어진 단어를 키보드 하나하나 입력할때 주어진 키보드 배열에서 인덱스를 옮기면서 얼마나 걸리는지 구해야한다.
  • 키보드 배열은 임의로 주어지기 때문에 딕셔너리에 인덱스와 함께 저장한다.
  • 그리고 첫 글자는 인덱스를 바로 더하고 현재 인덱스로 저장한다. 
  • 그리고 다음 인덱스부터 현재 위치에서 다음 알파벳의 위치의 거리 차이를 계산하여 정답에 더한다.
  • 이때도 마찬가지로 현재 위치를 갱신한다.

소스 코드

class Solution:
    def calculateTime(self, keyboard: str, word: str) -> int:
        ans = 0
        d = dict()
        cur = 0
        for i, k in enumerate(keyboard):
            d[k] = i
        ans += d[word[0]]
        cur = d[word[0]]
        for i in range(1, len(word)):
            ans += abs(cur - d[word[i]])
            cur = d[word[i]]
        return ans
저작자표시 (새창열림)

'컴퓨터공학 > LeetCode 1000' 카테고리의 다른 글

[LeetCode] 2256. Minimum Average Difference  (0) 2022.12.07
[LeetCode] 328. Odd Even Linked List  (0) 2022.12.07
[LeetCode] 1099. Two Sum Less Than K  (1) 2022.12.02
[LeetCode] 1704. Determine if String Halves Are Alike  (0) 2022.12.02
[LeetCode] 1207. Unique Number of Occurrences  (0) 2022.12.01
    '컴퓨터공학/LeetCode 1000' 카테고리의 다른 글
    • [LeetCode] 2256. Minimum Average Difference
    • [LeetCode] 328. Odd Even Linked List
    • [LeetCode] 1099. Two Sum Less Than K
    • [LeetCode] 1704. Determine if String Halves Are Alike
    saurus2
    saurus2
    Simple is Best

    티스토리툴바