Skip to content

Conversation

@Berrnuda
Copy link
Contributor

📌 푼 문제

문제이름 문제링크
Merge Intervals https://leetcode.com/problems/merge-intervals
Min Cost to Connect all points https://leetcode.com/problems/min-cost-to-connect-all-points
Lowest Common ancestor of a Binary Search Tree https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree
Word Break https://leetcode.com/problems/word-break
Kth largest element in an Array https://leetcode.com/problems/kth-largest-element-in-an-array

📝 간단한 풀이 과정

56 Merge Intervals

n번째의 end가 n+1번째의 start보다 크면 합치는식으로 코드를 작성했습니다.
cur[1]은 현재 값의 end 값을 나타내고 intervals[i]의 시작값과 비교합니다.
겹치면 cur에 머지를하고 겹치지않으면 result 배열에 추가합니다.

var merge = function (intervals) {
  let n = intervals.length;

  intervals.sort((a, b) => a[0] - b[0]);

  let result = [];
  let cur = intervals[0];

  for (let i = 1; i < n; i++) {
    if (cur[1] >= intervals[i][0]) cur[1] = Math.max(cur[1], intervals[i][1]);
    else {
      result.push(cur);
      cur = intervals[i];
    }
  }

  result.push(cur);

  return result;
};

235 Lowest Common ancestor of a Binary Search Tree

매주 지날때마다 문제 이해하는데에만 5분정도 쓰는거같아요 ㅠ
재귀함수로 작성해야되나 싶었는데 그럴 필요가 없는거같아서 반복문으로 작성했습니다.
p,q의 값을 최상위 노드부터 비교해가며 확인해 나가는 코드입니다.

var lowestCommonAncestor = function (root, p, q) {
  while (root) {
    if (p.val < root.val && q.val < root.val) {
      root = root.left;
    } else if (p.val > root.val && q.val > root.val) {
      root = root.right;
    } else {
      return root;
    }
  }
  return null;
};

139 Word Break

dp[i]s[0:i]의 값을 저장하는 배열입니다.
i >= word.length : 단어를 비교할 길이가 충분해야하고
dp[i - word.length] : 이전 상태가 분할 가능해야하고
s.slice(i - word.length, i) === word : 단어가 맞는지 확인하는 반복문을 거칩니다.

var wordBreak = function (s, wordDict) {
  const wordSet = new Set(wordDict);
  const dp = new Array(s.length + 1).fill(false);

  dp[0] = true;

  for (let i = 1; i <= s.length; i++) {
    for (let word of wordSet) {
      if (
        i >= word.length &&
        dp[i - word.length] &&
        s.slice(i - word.length, i) === word
      ) {
        dp[i] = true;
        break;
      }
    }
  }

  return dp[s.length];
};

215 Kth largest element in an Array

저번에 못썼던 MinPriorityQueue 을 써봤습니다

image
leetcode 내에서 언어 옆에 느낌표를 눌러보면 어떤 라이브러리들이 내장되어있는지 나오는데 datastructures에 내장되어있는 함수인거같아요
최소 힙을 이용해서 푼 문제로 k만큼의 사이즈만 남겨두고 k번째로 큰 값을 출력하는 코드입니다.

var findKthLargest = function (nums, k) {
  const minPriQue = new MinPriorityQueue();
  for (let i = 0; i < nums.length; i++) {
    minPriQue.enqueue(nums[i]);
    if (minPriQue.size() > k) {
      minPriQue.dequeue();
    }
  }
  return minPriQue.front().element;
};

🙌 궁금한 점

  • 이번에도 1584 번째 문제는 풀지못했습니다.. 계속 시도해볼게요 ..

@oris8 oris8 requested review from FrogBaek and Haze-S November 26, 2024 11:33
@oris8 oris8 merged commit c424ad3 into master Dec 2, 2024
3 checks passed
JooKangsan pushed a commit that referenced this pull request Dec 3, 2024
* 150 / Evaluate Reverse Polish Notation / Meium / 12m 01s

* 56 / Merge Intervals / Medium / 12m 58s

* 235 / Lowest Common Ancestor of a Binary Search Tree / Medium / 14m 45s

* 139 / Word Break / Medium / 18m 22s

* 215 / Kth Largest Element in an Array / Medium / 20m 19s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants