본문 바로가기

내 맘대로 알고리즘

(59)
Leetcode[day24] - Jump Game Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. Note: 1. You must do this in-place without making a copy of the array.Minimize the total number of operations. 2. Minimize the total number of operations. Input: nums = [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its ma..
May LeetCoding Challenge[Day8] - Check If It Is a Straight Line You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a point. Check if these points make a straight line in the XY plane. Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]] Output: true Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]] Output: false 1. 알고리즘을 해결하기 위해서 생각한 방법 - 우선 주어진 예제에서는 x와 y가 각각 증가하는 숫자에 대해서 보게 되었다. - [1,1]이 [2..
May LeetCoding Challenge[Day7] - Cousins in Binary Tree In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1. Two nodes of a binary tree are cousins if they have the same depth, but have different parents. We are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree. Return true if and only if the nodes corresponding to the values x and y are cousins...
Leetcode[day23] - LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recentl..
May LeetCoding Challenge[Day6] - Majority Element Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. Input: [3,2,3] Output: 3 Input: [2,2,1,1,1,2,2] Output: 2 1. 알고리즘을 해결하기 위해서 생각한 방법 - 해당 문제는 배열이 주어질 때, 주어진 배열의 크기 n보다, 항상 [n/2] 보다 많은 수가 존재한다. - 처음 접근 방법은 Map을 사용했다. Map을 통해서 K..
Leetcode[day22] - Bitwise AND of Numbers Range Given a range [m, n] where 0 > 1; shift++; } return n
May LeetCoding Challenge[Day5] - First Unique Character in a String Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Note: You may assume the string contain only lowercase letters. s = "leetcode" return 0. s = "loveleetcode" return 2. 1. 알고리즘을 해결하기 위해서 생각한 방법 - 해당 문제는 문자열이 입력으로 주어질 때, 특정 인덱스의 문자가 중복되지 않는 문자인 지 확인하는 문제이다. - 문자를 카운팅하고, 해당 문자열이 몇 개 존재하는 지 확인해야 한다. - 문자열을 카운팅하기 가장 쉬운 자료구조인 Map을 사용했고,..
May LeetCoding Challenge[Day4] - Number Complement Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Input: 5 Output: 2 Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. 1. 알고리즘을 해결하기 위해서 생각한 방법 - 우선, 해당 문제는 5라는 값이 주어졌을 때, 이진 값으로 101이 되는데, 이 때 해당 값의 반대 되는 값을 return하면 된다. - 여기서, 해당 값의 반대 되는 값의 단어적 정의는 잘 모르겠고, complem..