- Today
- Total
- array
- ์๋ฐ
- MST
- java
- PS
- ๊ตฌํ
- Graph
- spring
- ํ๋ก๊ทธ๋๋จธ์ค
- pytorch
- leetcode
- ๋ค์ต์คํธ๋ผ
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- Algorithm
- ๋ฐฑ์๋
- ๋ฐฑ์ค
- ๋ฒจ๋งํฌ๋
- ์๋ฃ๊ตฌ์กฐ
- database
- tree
- ๊ทธ๋ฆฌ๋
- ์์์ ๋ ฌ
- ๋ฌธ๋ฒ
- ์ธํด
- OOP
- CS
- ์๋ฐ์์ ์
- dp
- ์กธ์ ์ํ
- BFS
Partially Committed
[CH01] ๊ตฌ๊ฐํฉ ๋ณธ๋ฌธ
Algorithm || DataStructure
#01. ๊ตฌ๊ฐํฉ
Date : 2022/09/09
ํ๋ฆฐ ๋ด์ฉ์ด ์์ ์, ์ง์ ํด์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค!
1. ๋ฐฐ์ด๊ณผ ๋ฆฌ์คํธ
๋ฐฐ์ด์ ํน์ง
- ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํด์ ์์์ O(1) ์ ์ ๊ทผ ๊ฐ๋ฅ
- ์๋ก์ด ๊ฐ ์ฝ์ ๋ฐ ํน์ ์์น์ ๊ฐ ์ญ์ ๊ฐ ๋นํจ์จ์ . (๋ก๊ธฐ๊ฑฐ๋ ๋ฐ์ด์ค์ผํจ)
- ์ ์ธํ ๋ ํ๋ฒ ์ง์ ํ ๋ฐฐ์ด์ ํฌ๊ธฐ๋ ๋๋ฆฌ๊ฑฐ๋ ์ค์ผ ์ ์๋ค.
๋ฆฌ์คํธ์ ํน์ง
- ๊ฐ์ ์ ๊ทผํ๋ ค๋ฉด O(N) ์ด ํ์ํจ.
- ๋ฐ์ดํฐ์ ์ฝ์ ์ญ์ ๊ฐ O(1) ์ ๊ฐ๋ฅ
- ๊ฐ๋ณ ๊ธธ์ด์
์์ : ์ซ์์ ํฉ
https://www.acmicpc.net/problem/11720
์ซ์๋ฅผ String ์ผ๋ก ์ ๋ ฅ๋ฐ์ ๋ค์, ๊ฐ๊ฐ์ ์์๋ฅผ ๋์ ํ์ฌ ๋ํด์ ์ถ๋ ฅํด์ฃผ๋ฉด ๋๋ค.
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
String number = sc.next();
int ans = 0;
for(int i = 0 ; i < t ; i++){
ans += (number.charAt(i)-'0');
}
System.out.println(ans);
}
}
์์ : ํ๊ท ๊ตฌํ๊ธฐ
https://www.acmicpc.net/problem/1546
๊ฐ๊ฐ์ ์ ์๋ฅผ ๋ฐฐ์ด์ ์ ์ฅํ ๋ค ๋ฌธ์ ์์ ์ฃผ์ด์ง ๋๋ก ํ๊ท ์ ๊ณ์ฐํ๋ฉด ๋๋ค.
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
int[] arr = new int[1000];
for(int i = 0 ; i < tc; i++){
arr[i] = sc.nextInt();
}
long max = 0;
double avg = 0.0;
long sum = 0;
for(int i = 0 ; i < tc; i ++){
if(max < arr[i]) max = arr[i];
sum += arr[i];
}
avg = ((sum*100.0)/max)/tc;
System.out.println(avg);
}
}
2. ๊ตฌ๊ฐ ํฉ
ํฉ ๋ฐฐ์ด์ ์ด์ฉํด์ ์๊ฐ ๋ณต์ก๋๋ฅผ ์ค์ด๊ธฐ ์ํด ์ฌ์ฉํ๋ ์๊ณ ๋ฆฌ์ฆ์ด๋ค. ๋จผ์ ๋ฐฐ์ด A ์ ๋ํ ํฉ ๋ฐฐ์ด S ๋ฅผ ์ ์ํด์ผ ํ๋ค. ํฉ๋ฐฐ์ด์ S[i] = A[0] + A[1] + A[2] ... + A[i-1] + A[i] ์ ๊ฐ์ด ์ ์ํ๋ค. S ๋ฅผ ์ ์ํจ์ผ๋ก ์จ, ๊ธฐ์กด ๋ฐฐ์ด A ์์ range sum ์ ๊ตฌํด์ผ ํ๋ ๊ฒฝ์ฐ O(N) -> O(1) ์ผ๋ก ์ต์ ํํ ์ ์๋ค.
A[2]~A[4] ๊ฐ์ ๊ตฌ๊ฐํฉ์ ๊ตฌํ๋ ค๋ฉด S[4]-S[1] ์ ๊ณ์ฐํ๋ฉด ๋๊ณ ์ด๋ 15-8 = 7 ์ด๋ค.
์์ : ๊ตฌ๊ฐ ํฉ ๊ตฌํ๊ธฐ
https://www.acmicpc.net/problem/11659
์์ ๊ฐ์๋ ์ต๋ 100,000 ์ด๊ณ ํฉ์ ๊ตฌํด์ผ ํ๋ ํ์์ ์ต๋๋ 100,000 ์ด๋ ์ต์ ์ ๊ฒฝ์ฐ 100,000 * 100,000 = 10,000,000,000 ์ผ๋ก 1์ต ์ด์์ ์ฐ์ฐ์ด ํ์ํ๋ฏ๋ก ์ ํ ์๊ฐ์ธ 1์ด ๋ด์ ์ ๋๋ก ๊ฒฐ๊ณผ๋ฅผ ๋ผ ์ ์๋ค. ์ด๋ฌํ ๊ฒฝ์ฐ์ ๊ตฌ๊ฐํฉ ์๊ณ ๋ฆฌ์ฆ์ ์ด์ฉํด์ ์ต์ ํ๋ฅผ ํ๋ฉด O(M) ์ ํด๊ฒฐํ ์ ์๋ค.
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int tc = sc.nextInt(); // testcase
int[] arr = new int[n+1];
int[] s = new int[n+1];
for(int i = 1 ; i <= n ; i++){
arr[i] = sc.nextInt();
}
s[1] = arr[1];
for(int i = 2; i<=n; i++){
s[i] = s[i-1] + arr[i];
}
for(int i = 0 ; i < tc; i++){
int start = sc.nextInt();
int end = sc.nextInt();
int range_sum = s[end] - s[start-1];
System.out.println(range_sum);
}
}
}
์์ : ๊ตฌ๊ฐ ํฉ ๊ตฌํ๊ธฐ 2
https://www.acmicpc.net/problem/11660
2์ฐจ์ ๋ฐฐ์ด์์ ๋์ ํฉ์ ์ํํ๋ ๋ฌธ์ ์ด๋ค. ํต์ฌ ์์ด๋์ด๋,
dp[x][y] = dp[x-1][y] + dp[x][y-1] - dp[x][y-1] + dp[x-1][y-1] ์ธ ๊ฒ์ด๊ณ
(x1, y1) ~ (x2, y2) ๊น์ง์ ํฉ ans = dp[x2][y2] - dp[x1-1][y2] - dp[x2][y1-1] + dp[x1-1][y1-1] ๋ผ๋ ๊ฒ์ด๋ค.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int size = Integer.parseInt(st.nextToken());
int tc = Integer.parseInt(st.nextToken());
int[][] arr = new int[size+1][size+1];
int[][] dp = new int[size+1][size+1];
for(int i = 1 ; i <= size; i++){
st = new StringTokenizer(br.readLine());
for(int j = 1 ; j <= size; j++){
arr[i][j] = Integer.parseInt(st.nextToken());
}
}
dp[1][1] = arr[1][1];
for(int i = 1 ; i<= size; i++){
for(int j = 1 ; j <= size; j++){
dp[i][j] = dp[i][j-1] + dp[i-1][j] - dp[i-1][j-1] + arr[i][j];
}
}
for(int i = 0 ; i < tc; i++){
st = new StringTokenizer(br.readLine());
int x1 = Integer.parseInt(st.nextToken());
int y1 = Integer.parseInt(st.nextToken());
int x2 = Integer.parseInt(st.nextToken());
int y2 = Integer.parseInt(st.nextToken());
// (x1, y1) ~ (x2, y2)
int ans = dp[x2][y2] - dp[x1-1][y2] - dp[x2][y1-1] + dp[x1-1][y1-1];
System.out.println(ans);
}
}
}
์์ : ๋๋จธ์งํฉ
https://www.acmicpc.net/problem/10986
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken()); // ์ซ์ ๊ฐ์
int m = Integer.parseInt(st.nextToken());
long ans = 0;
long[] s = new long[n];
long[] r = new long[m];
st = new StringTokenizer(br.readLine());
s[0] = Integer.parseInt(st.nextToken());
for(int i = 1; i < n ; i++){
s[i] = s[i-1] + Integer.parseInt(st.nextToken());
}
for(int i = 0 ; i < n ;i++){
int rem = (int)(s[i]%m);
if(rem == 0) ans++;
r[rem]++;
}
for(int i = 0 ; i < m ;i++){
if(r[i]>0){
ans+=(r[i]*(r[i]-1))/2;
}
}
System.out.println(ans);
}
}
References
1. http://www.yes24.com/Product/Goods/108571508
'๐ฅ Algorithm || ๋ฌธ์ ํ์ด > PS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LEETCODE] 1523. Count Odd Numbers in an Interval Range (0) | 2022.10.09 |
---|---|
[CH01] ํฌํฌ์ธํฐ (0) | 2022.09.12 |
[์ฐ์ ์์ํ] ํ๋ฆฐํฐ (์๋ฐ) (0) | 2022.09.04 |
[ํ] ๊ธฐ๋ฅ๊ฐ๋ฐ (JAVA) (0) | 2022.09.03 |
[์คํ/ํ] ์ฌ๋ฐ๋ฅธ ๊ดํธ (JAVA) (0) | 2022.09.03 |