Skip to content

Commit

Permalink
Merge pull request #808 from river20s/main
Browse files Browse the repository at this point in the history
[river20s] Week 4
  • Loading branch information
SamTheKorean authored Jan 5, 2025
2 parents 27ece99 + 0914b69 commit 203a1c6
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
47 changes: 47 additions & 0 deletions merge-two-sorted-lists/river20s.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
/*
* [ํ’€์ด]
* 1) ๋‘ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ด๋ฏธ ์ •๋ ฌ๋œ ์ƒํƒœ โ†’ ๋งจ ์•ž์—์„œ๋ถ€ํ„ฐ ๋‘˜ ์ค‘ ๋” ์ž‘์€ ๋…ธ๋“œ๋ฅผ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ rs์— ์ด์–ด ๋ถ™์ธ๋‹ค.
* 2) ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋๋‚  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค. โ†’ ๋‚จ์€ ๋ฆฌ์ŠคํŠธ์˜ ๋…ธ๋“œ๋Š” ๊ทธ๋Œ€๋กœ rs์— ๋ถ™์ธ๋‹ค.
* 3) rs ํ—ค๋“œ ๋…ธ๋“œ์˜ next๋ถ€ํ„ฐ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
* [T.C]
* ๊ฐ ๋…ธ๋“œ๋ฅผ ํ•œ ๋ฒˆ์”ฉ ๋น„๊ต โ†’ ์—ฐ๊ฒฐ(๋˜๋Š” ์—ฐ๊ฒฐ๋งŒ)ํ•˜๋ฏ€๋กœ,
* T.C = O(n+m) (n = list1์˜ ๊ธธ์ด, m = list2์˜ ๊ธธ์ด)
* [S.C]
* list1๊ณผ list2์˜ ๋…ธ๋“œ๋ฅผ ๋‹ค์‹œ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์ถ”๊ฐ€์ ์ธ ๊ณต๊ฐ„์€ ๊ฑฐ์˜ ํ•„์š”ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ(rs์˜ head ์ •๋„?),
* S.C = O(1)
*/

public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode rs = new ListNode(0);
ListNode rsNext = rs;
// step 1
while (list1 != null && list2 != null) {
if (list1.val <= list2.val) {
rsNext.next = list1;
list1 = list1.next;
}
else {
rsNext.next = list2;
list2 = list2.next;
}
rsNext = rsNext.next;
}
// step 2
if (list1 != null) rsNext.next = list1;
if (list2 != null) rsNext.next = list2;
// step3
return rs.next;
}
}

28 changes: 28 additions & 0 deletions missing-number/river20s.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.Arrays;
class Solution {
/* [ํ’€์ด]
* 1) ๋ฐฐ์—ด nums์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.
* 2) ๋ฐฐ์—ด ์š”์†Œ์˜ ์ธ๋ฑ์Šค์™€ ๊ฐ’์„ ๋น„๊ตํ•œ๋‹ค.
* 2-1) ์ธ๋ฑ์Šค์™€ ๊ฐ’์ด ๊ฐ™๋‹ค๋ฉด ํ•ด๋‹น ๊ฐ’์€ ๋ฐฐ์—ด์— ์žˆ๋‹ค.
* 2-2) ์ธ๋ฑ์Šค์™€ ๊ฐ’์ด ๋‹ค๋ฅด๋‹ค๋ฉด, ํ•ด๋‹น ๊ฐ’์€ ๋ฐฐ์—ด์— ์—†๋‹ค.
* 3) ๋‹ค๋ฅธ ๊ฐ’์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
* [T.C]
* ๋‚ด์žฅ๋œ Arrays.sort()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n log n)์ด ๋œ๋‹ค.
* [S.C]
* ์ตœ์•…์˜ ๊ฒฝ์šฐ Arrays.sort()๋Š” ์ถ”๊ฐ€์ ์œผ๋กœ O(n) ๋งŒํผ์˜ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•œ๋‹ค.
*/

public int missingNumber(int[] nums) {
// nums ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
Arrays.sort(nums);
// ์ธ๋ฑ์Šค์™€ ์š”์†Œ ๋น„๊ต
for (int i = 0; i < nums.length; i++) {
if (nums[i] != i) {
return i;
}
}
// ๋ฐฐ์—ด์— ์—†๋Š” ๊ฐ’ ๋ฐ˜ํ™˜
return nums.length
}
}

0 comments on commit 203a1c6

Please sign in to comment.