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

블로그 메뉴

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

공지사항

인기 글

태그

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

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
saurus2

Saurus2

컴퓨터공학/LeetCode 1000

[LeetCode] 1680. Concatenation of Consecutive Binary Numbers

2022. 9. 24. 02:17

1680. Concatenation of Consecutive Binary Numbers

Medium

Given an integer n, return the decimal value of the binary string formed by concatenating the binary representations of 1 to n in order, modulo 109 + 7.

 

Example 1:

Input: n = 1
Output: 1
Explanation: "1" in binary corresponds to the decimal value 1. 

Example 2:

Input: n = 3
Output: 27
Explanation: In binary, 1, 2, and 3 corresponds to "1", "10", and "11".
After concatenating them, we have "11011", which corresponds to the decimal value 27.

Example 3:

Input: n = 12
Output: 505379714
Explanation: The concatenation results in "1101110010111011110001001101010111100".
The decimal value of that is 118505380540.
After modulo 109 + 7, the result is 505379714.

 

Constraints:

1 <= n <= 105


문제 풀이

  • 숫자 크기에 따라 이진수 값들을 연속으로 붙여준다.
  • 붙여서 만든 이진수 값을 십진수 값으로 변환한다. 
  • n의 제한값은 10^5 이기 때문에 N^2 알고리즘은 불가하다.
  • 1부터 n까지의 숫자를 이진수로 변환하고 파이썬에서는 0b가 붙기 때문에, 그 부분을 제거 하고 더해준다. 
  • 이진수를 십진수로 바꾸고 10**9 + 7 값으로 모듈러 계산을 해준 후 리턴한다.
  • PS: 시간복잡도는 NlogN, 이진수로 변환할때 lonN의 시간이 추가된다.

소스 코드

class Solution:
    def concatenatedBinary(self, n: int) -> int:
        result = ''
        for i in range(1, n+1):
            result += bin(i)[2:]
        return int(result,2) % (10**9 + 7)
저작자표시 (새창열림)

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

[LeetCode] 990. Satisfiability of Equality Equations  (0) 2022.09.26
[LeetCode] 159. Longest Substring with At Most Two Distinct Characters  (1) 2022.09.24
[LeetCode] 1272. Remove Interval  (1) 2022.09.23
[LeetCode] 557. Reverse Words in a String III  (1) 2022.09.23
[LeetCode]1338. Reduce Array Size to The Half  (0) 2022.08.20
    '컴퓨터공학/LeetCode 1000' 카테고리의 다른 글
    • [LeetCode] 990. Satisfiability of Equality Equations
    • [LeetCode] 159. Longest Substring with At Most Two Distinct Characters
    • [LeetCode] 1272. Remove Interval
    • [LeetCode] 557. Reverse Words in a String III
    saurus2
    saurus2
    Simple is Best

    티스토리툴바