Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions leetcode2/1easy/최원준/Q3364.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package Leetcode;

/*
1. 아이디어 :
브루트포스로 풀 수 있습니다

2. 시간복잡도 :
O( n*n )

3. 자료구조/알고리즘 :
- / 브루트포스
*/

import java.util.List;

public class Q3364 {
class Solution {
public int minimumSumSubarray(List<Integer> nums, int l, int r) {
int n = nums.size();
int ans = Integer.MAX_VALUE;
for (int i=0; i<n; i++) {
int total = 0;
for (int j=i; j<Math.min(n, i+r); j++) {
total += nums.get(j);
if (l <= j-i+1 && j-i+1 <= r && total >0) ans = Math.min(ans, total);
}
}

return ans == Integer.MAX_VALUE? -1: ans;
}
}
}
56 changes: 56 additions & 0 deletions leetcode2/2medium/최원준/Q1807.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package Leetcode;

/*
1. 아이디어 :
knowledge를 map에 저장합니다. 같은 key가 들어오면 ?로 저장합니다.
s를 순회하면서 괄호가 열리는지 닫히는지 확인
- 열리면 isOpen을 true로 바꿉니다.
- 닫히면 isOpen을 false로 바꿉니다. curr을 map에서 key로 사용하여 value를 가져옵니다.

열린 상태에서는 curr에 추가, 닫힌 상태에서는 ans에 추가합니다.

2. 시간복잡도 :
O( n * m )

3. 자료구조/알고리즘 :
해시맵 / -
*/

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Q1807 {
class Solution {
public String evaluate(String s, List<List<String>> knowledge) {
Map<String, String> map = new HashMap<>();
for (List<String> k : knowledge) {
String key = k.get(0), val = k.get(1);
map.put(key, map.containsKey(key)? "?" : val);
}

StringBuilder ans = new StringBuilder();
StringBuilder curr = new StringBuilder();

boolean isOpen = false;
for (char c : s.toCharArray()) {
if (c=='(') {
isOpen = true;
} else if (c==')') {
isOpen = false;
String key = curr.toString();
ans.append(map.containsKey(key) ? map.get(key) : "?");
curr = new StringBuilder();
} else {
if (!isOpen) {
ans.append(c);
} else {
curr.append(c);
}
}
}

return ans.toString();
}
}
}
50 changes: 50 additions & 0 deletions leetcode2/3hard/최원준/Q741.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package Leetcode;

/*
1. 아이디어 :
문제를 0,0에서 시작하는 두명으로 해석할 수 있습니다.
백트래킹을 통해 두 사람을 동시에 이동시킵니다.
메모이제이션을 통해 중복된 계산을 피합니다.

2. 시간복잡도 :
O( n^5 )

3. 자료구조/알고리즘 :
배열 / 백트래킹
*/

import java.util.Arrays;

public class Q741 {
class Solution {
int[][] grid;
int n;
int[][][][] dp;
public int cherryPickup(int[][] grid) {
this.grid = grid;
n = grid.length;
dp = new int[n+1][n+1][n+1][n+1];
for (int i=0; i<n+1; i++) for (int j=0; j<n+1; j++) for (int k=0; k<n+1; k++) {
Arrays.fill(dp[i][j][k], -Integer.MAX_VALUE);
}
return Math.max(0, backtrack(0,0,0,0));
}

public int backtrack(int x1, int y1, int x2, int y2) {
if (dp[x1][y1][x2][y2] != -Integer.MAX_VALUE) return dp[x1][y1][x2][y2]; // cache
if (x1 >= n || y1 >= n || x2 >= n || y2 >= n) return -Integer.MAX_VALUE; //out of bound
if (grid[x1][y1] == -1 || grid[x2][y2] == -1) return -Integer.MAX_VALUE; //blocked

if (x1 == n-1 && y1 == n-1) return grid[x1][y1];

int total = 0;
total += (x1 == x2 && y1 == y2) ? grid[x1][y1] : grid[x1][y1] + grid[x2][y2]; // 중복 계산
total += Math.max(
Math.max(backtrack(x1+1,y1,x2+1,y2), backtrack(x1+1,y1,x2,y2+1)),
Math.max(backtrack(x1,y1+1,x2+1,y2), backtrack(x1,y1+1,x2,y2+1)) );

dp[x1][y1][x2][y2] = total;
return total;
}
}
}